TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Need help on nested loop! (http://www.talkphp.com/absolute-beginners/2795-need-help-nested-loop.html)

pabin 05-11-2008 03:22 PM

Need help on nested loop!
 
Could somebody overhere expalin how nested loop works because i am quite confuse on this issue?:-/

delayedinsanity 05-11-2008 08:31 PM

Nested:

1) A process or activity self-contained within a larger entity, that retains its identity and characteristics for reporting and analysis.

2) Nested is the seventh studio album by Bronx-born singer, songwriter, and pianist Laura Nyro, released in 1978 by Columbia Records.

What in particular about it are you having trouble with?
-m

Village Idiot 05-11-2008 11:55 PM

A nested loop is a loop inside a loop. For instance, if you are reading posts on a forum sequentially, you would read:
Topic 1
Post 1
Post 2
Post 3
Topic 2
Post 1
Post 2
Post 3
So on the so forth.

If you want a purely programmatic example, run this code.
PHP Code:

<?php
for($i=0;$i<10;$i++)
{
    echo 
"Main Level $i - ";
    for(
$j=0;$j<10;$j++)
    {
        echo 
"Sub-Level $j - ";
    }
    echo 
"<br />";
}
?>

Here is what it will output:
http://justanotherportfolio.com/nest.php

sketchMedia 05-12-2008 12:00 AM

A nested loops is a loop that has a loop within it. First of all the outer loop's first iteration triggers the inner loop then the inner loop continues until completion, then the outer loop cycles again thus causing the inner again to trigger again.

put into a simple example:

PHP Code:

for($i=0;$i 10$i++)
{
    echo 
'Line '$i 1' ';
    for(
$j=0$j 5$j++)
    {
        echo 
$j;
    }
    echo 
'<br />';


outputs :
Code:

Line 1 01234
Line 2 01234
Line 3 01234
Line 4 01234
Line 5  01234
Line 6 01234
Line 7 01234
Line 8 01234
Line 9 01234
Line 10  01234

basically the first loop out puts : 'Line 1' then the nested loop starts and echos numbers 0-4, then the first loop echos 'Line 2' then triggers the nested loop again and the cycle continues until the first loop finishes.

hope i made that helps

sketchMedia 05-12-2008 10:27 AM

damn village beat me! lol the code looks similar aswell, anyway both posts should help you understand


All times are GMT. The time now is 10:47 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0