loops within loops. HELP!!
Results 1 to 9 of 9

Thread: loops within loops. HELP!!

  1. #1
    Member
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    49

    loops within loops. HELP!!

    Hi friends.

    I need help with something very basic.

    I am currently reading a book on php and I just need to ask about how loops within loops work.

    I am aware of the fact that 'inner for statements' run 1st and then the outer for runs afterwards.

    But what I am not sure of is:

    1) do inner 'if' run before outer 'ifs'?
    2) do inner 'whiles' run before outer 'whiles'?
    3) do inner 'do... whiles' run before outer 'do... whiles'?

    etc ,,, etc.

    or is this just an inner 'for' statement thing??

    Paul.

  2. #2
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,564
    Actually, the outer loop will run first. On the first iteration of the outer loop, the inner loop will run, and keep running, until it terminates. Then, the next iteration of the outer loop will run, and on this iteration, the inner loop will run, and keep running, until it terminates. Then, the next iteration of the outer loop will run, ..., until the outer loop itself terminates. This applies whether the loops are for loops, while loops, or do while loops.

    Quote Originally Posted by Paul help!
    1) do inner 'if' run before outer 'ifs'?
    Likewise, the outer if statement will run before the inner if statement. It is simply a consequence of the flow of control proceeding statement by statement from the "top" to the "bottom" except where this flow is modified by control statements such as if statements and loops.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  3. #3
    Member
    Join Date
    Aug 2013
    Location
    Manchester, England.
    Posts
    49
    thanks laserlight.

  4. #4
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,884
    You may find it helpful to actually write a nested loop and run it:
    PHP Code:
    for($i=0; $<10$i++) {
      echo 
    "OUTER loop, iteration #$iPHP_EOL;
      for(
    $j=0$j<5$j++) {
        echo 
    "\tINNER loop, iteration $jPHP_EOL;
      }

    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  5. #5
    Banned
    Join Date
    Sep 2014
    Posts
    4
    What is the use of it?

  6. #6
    PHP Witch laserlight's Avatar
    Join Date
    Apr 2003
    Location
    Singapore
    Posts
    13,564
    What is the use of loops within loops? Well, they are useful for expressing logic that involves some kind of iterations within iterations, but that's just another way for saying "loops within loops". Think of say, accessing arrays of arrays.
    Use Bazaar for your version control system
    Read the PHP Spellbook
    Learn How To Ask Questions The Smart Way

  7. #7
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,889
    Like when you read a book. You read it page by page; on each page you read line by line; in each line you read word by word ....
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  8. #8
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,884
    The most common example I've ever seen of a loop within a loop is when you want to parse data in a table or spreadsheet. The outer loop loops through all the rows and the inner one loops through all the cells in a particular row.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  9. #9
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    7,715
    Quote Originally Posted by sneakyimp View Post
    The most common example I've ever seen of a loop within a loop is when you want to parse data in a table or spreadsheet. The outer loop loops through all the rows and the inner one loops through all the cells in a particular row.
    Great example.

    "Sometimes, though, when you're a man, you get alone in your room with a computer and loop inside a loop. It's for fun...."
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •