Wrox Programmer Forums
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 8th, 2014, 05:13 AM
Registered User
 
Join Date: Oct 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Nested while loop

For a school asignment i need to make several patterns using nested for/while/dowhile loops.

But i really can't find what i'm doing wrong, nor is the school of any help..
I was hoping people here could assist me understanding the language. Because so far it hardly makes any sence to me.

This is the code so far:
Code:
    <?php
    $row2 = 1;
    $col2 = 1;


    for ($row = 1; $row <= 5; $row++)
    {
        for ($col = 1; $col <= 10; $col++)
        {
            echo "*";
        }

        echo "<br />";
    }

    echo "<br />";

    while ($row2 <= 5) 
        {
        while ($col2<=10) {
            echo "*";
            $col2++;
        }
        echo "<br />*";
        $row2++;
    }

    ?>
The nested for loop works fine!! (proud of myself... ), but the while loop isn't working and i can't figure out why.

this was supposed to be the result:
**********
**********
**********
**********
**********
but instead i get this:

**********
*
*
*
*
*

Anybody willing to help me?:)

Last edited by Pluchies; October 8th, 2014 at 05:33 AM..
 
Old October 8th, 2014, 06:14 AM
Registered User
 
Join Date: Oct 2014
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default same with Do/while loop.

so i kept going with my assignment, and i managed to get the same WRONG result twice..

Code:
    do
    {
        do
        {
            echo "*";
            $col3++;
        } while ($col3 <= 10);

        echo "<br />";
        $row3++;
    } while ($row3 <= 5);
    ?>
 
Old October 14th, 2014, 02:02 AM
Registered User
 
Join Date: Oct 2014
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just my thought. Since you increment the value in $col2 to 11 in
while ($col2<=10) {
echo "*";
$col2++;
}

So, the $col2 will have a value of 11. And if go to the next iteration in
while ($row2 <= 5)
{
while ($col2<=10) {
echo "*";
$col2++;
}
echo "<br />*";
$row2++;
}

the $col2<=10 condition is always false, and it will never go into that loop. My guess, you need to add as follow to "reset" $col2 to 1:
while ($row2 <= 5)
{
while ($col2<=10) {
echo "*";
$col2++;
}
echo "<br />*";
$col2=1; <---- add this
$row2++;
}


Quote:
Originally Posted by Pluchies View Post
For a school asignment i need to make several patterns using nested for/while/dowhile loops.

But i really can't find what i'm doing wrong, nor is the school of any help..
I was hoping people here could assist me understanding the language. Because so far it hardly makes any sence to me.

This is the code so far:
Code:
    <?php
    $row2 = 1;
    $col2 = 1;


    for ($row = 1; $row <= 5; $row++)
    {
        for ($col = 1; $col <= 10; $col++)
        {
            echo "*";
        }

        echo "<br />";
    }

    echo "<br />";

    while ($row2 <= 5) 
        {
        while ($col2<=10) {
            echo "*";
            $col2++;
        }
        echo "<br />*";
        $row2++;
    }

    ?>
The nested for loop works fine!! (proud of myself... ), but the while loop isn't working and i can't figure out why.

this was supposed to be the result:
**********
**********
**********
**********
**********
but instead i get this:

**********
*
*
*
*
*

Anybody willing to help me?:)





Similar Threads
Thread Thread Starter Forum Replies Last Post
nested loop riteshdubey5 XSLT 1 August 29th, 2012 03:46 AM
Nested Loop Numbering Vips XSLT 1 December 27th, 2010 06:24 AM
nested loop adnan2 Java Basics 1 July 13th, 2007 04:21 PM
Nested Do Until Loop goels Access VBA 2 September 30th, 2004 08:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.