Thread: Help with nested loops(im a new programmer)

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    70

    Question Help with nested loops(im a new programmer)

    Hello,i just recently started trying to learn C++ and i got to nested loops and it got me confused, so if you could give me example on nested loops and explain what it does and how it does it,if you can, that would be most appreciated thank you.
    Last edited by cuo741; 06-28-2010 at 10:57 PM.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by cuo741 View Post
    Hello,i just recently started trying to learn C++ and i got to nested loops and it got me confused, so if you could give me example on nested loops and explain what it does and how it does it,if you can, that would be most appreciated thank you.
    Do you understand what the not-nested loop does?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    The Autodidact Dante Wingates's Avatar
    Join Date
    Apr 2010
    Location
    Valhalla
    Posts
    56

    Cool

    As far as I know a nested loop is nothing more than a loop within another loop...

    Code:
    for(int i = 0; i < 10; i++)
          printf("%d ", i);
    this loop will print values from 0 to 9.

    now a loop within that loop

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <iostream>
    
    int main(void)
    {
        for(int i = 0; i < 10; i++){
            printf("%d: ", i);
            for(int j = 0; j < 10; j++)
                printf("%d ", j);
            std::cout.put(0xA);
        }
        
        getchar();
        return(0);
    }
    The first loop will execute 10 times. This will execute the inner loop each time the first loop executes, and the inner loop prints the values from 0 to 10 on the screen each time it is executed. In other words it will print 0 to 10 ten times. When control reaches the inner loop, it enters in a loop and only passes control to the first for loop once the variable j is not lower than ten, and then it repeats.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Why are you using magic numbers?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 41
    Last Post: 05-12-2010, 06:47 PM
  2. dynamic array
    By mouse666666 in forum C Programming
    Replies: 36
    Last Post: 04-11-2010, 02:27 AM
  3. Problems with Nested Structures and Arrays of Structures
    By Ignoramus in forum C Programming
    Replies: 4
    Last Post: 03-02-2010, 01:24 AM
  4. Nested array vs. tree
    By KONI in forum Tech Board
    Replies: 1
    Last Post: 06-07-2007, 04:43 AM
  5. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM