# Maximum PC

 It is currently Tue Apr 21, 2015 1:32 am

 All times are UTC - 8 hours

 Page 1 of 1 [ 11 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Can any one please explain how a nested for loop works in c+Posted: Fri Oct 31, 2008 6:28 pm
 Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
i looked at the book and searched google, but still i have no clue how it works. I know how one for loop works; it reads the initial value once, then it checks the condition, if its true it runs the statement block and it then runs the increment (if it has one).

Top

 Post subject: Posted: Fri Oct 31, 2008 10:06 pm
 Willamette

Joined: Fri Jul 06, 2007 9:29 am
Posts: 1447
Well, it's been a long time since I've dealt with C, but as you know, a For loop will run everything inside it at least once. If you want to do something multiple times for each of the For iterations, you use a nested For. It can be good for displaying or dealing with 2d and 3d tables of information (rows and columns).

The simplest way of showing this is a table of 5x5 squares. Each of those squares has a 2 position ID that represents the position it's in. First part of the ID represents the column. Second ID represents the row. Start off from 1, counting to 5 to the right. 1 counting to 5 to the bottom. So the first square is 1,1. The next to the right is 2, 1. The next down is 1,2. And so on.

Sort of like this:

1,1 - 2,1 - 3,1 - 4,1 - 5,1
1,2 - 2,2 - 3,2 - 4,2 - 5,2
1,3 - 2,3 - 3,3 - 4,3 - 5,3
1,4 - 2,4 - 3,4 - 4,4 - 5,4
1,5 - 2,5 - 3,5 - 4,5 - 5,5

Now, let's say you want to display every value in that table. You can do a printf for each cell in there (25 lines). Or you can use a 2 layer nested For loop (3 lines or so). Layer one goes through each row down. The second layer goes through each column across.

pseudo code
for i = 1 to 5, i++
for j = 1 to 5, j++
printf(i,j)

You can tweak the conditions to display different ways (say for example Even or Odd cells only, or in reverse, etc). But anytime you want to do a mass thing to a matrix table, a nested for can be handy.

This isn't the only thing it's useful for, but it's a good example.

Top

 Post subject: Posted: Sat Nov 01, 2008 4:55 am
 Team Member Top 1000

Joined: Thu Sep 11, 2008 4:10 pm
Posts: 868
I'm in Computer Science 1 at my college, and didn't know you could write it the first way. What I mean is usually for the for loop, I'd write:

for(i=0, i <6, i++) etc.

Or do less than equals to 5.

AJ

Top

 Post subject: Posted: Sat Nov 01, 2008 8:56 am
 7yrs+11,000 Posts

Joined: Tue Jul 27, 2004 5:44 pm
Posts: 11248
Location: The kitten above is not on fire.
Nested loops work like normal ones.

Code:
for (i = 0; i < 10; i++)
{
for (y = 0; y < 10; y++)
{
cout << y << endl;
}
}

That particular code would print out the sequence 1-10 10 times.

When the condition of a loop is met, it executes all the code between it's inherit brackets (assuming you are not using an exit function) - this can be anything, including new loops.

When it reaches it reaches the end, it does the same thing over and over until the condition is false, thus the name "loop".

Edit: This may help you more than me: http://mathbits.com/mathbits/compsci/looping/nested.htm

Top

 Post subject: Posted: Fri Nov 21, 2008 9:57 pm
 Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
gamerfreak wrote:
Nested loops work like normal ones.

Code:
for (i = 0; i < 10; i++)
{
for (y = 0; y < 10; y++)
{
cout << y << endl;
}
}

That particular code would print out the sequence 1-10 10 times.

When the condition of a loop is met, it executes all the code between it's inherit brackets (assuming you are not using an exit function) - this can be anything, including new loops.

When it reaches it reaches the end, it does the same thing over and over until the condition is false, thus the name "loop".

Edit: This may help you more than me: http://mathbits.com/mathbits/compsci/looping/nested.htm

Interesting ...... so basically the outer loop controls how many times the inner loop runs?

Top

 Post subject: Posted: Sat Nov 22, 2008 7:21 am
 SON OF A GUN

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
xxmainframexpxx wrote:
Interesting ...... so basically the outer loop controls how many times the inner loop runs?
Sort of.

Think of it like this. The inner loop will run to its LCV (loop control variable or value) value for each iteration of the outer loop.

Code:
for (i = 0; i < 10; i++)
{
for (y = 0; y < 5; y++)
{
cout << y << endl;
}
}

Modifying gamer's example to have different numbers:

So when the code first hits that line, you will get this:
i = 0, y = 0
i = 0, y = 1
i = 0, y = 2
i = 0, y = 3
i = 0, y = 4
i = 0, y = 5

Now, we go back to the outer loop and increment i.
i = 1, y = 0
i = 1, y = 1
i = 1, y = 2
i = 1, y = 3
i = 1, y = 4
i = 1, y = 5

And again... till i reaches 10.

Top

 Post subject: Posted: Sun Nov 23, 2008 10:55 am
 Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
Wouldn't it stop at:

y=0
y=1
y=2
y=3
y=4

since it breaks at 5? Also, so that code would print out y = 1 through 5 ten times?

Top

 Post subject: Posted: Sun Nov 23, 2008 4:20 pm
 SON OF A GUN

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Okay, you have me. I was copy and paste happy.

Top

 Post subject: Posted: Sun Nov 23, 2008 5:51 pm
 Little Foot

Joined: Sat Feb 11, 2006 11:04 am
Posts: 174
CrashTECH wrote:
Okay, you have me. I was copy and paste happy.

but would it print out 1-5 ten times?

Top

 Post subject: Posted: Sun Nov 23, 2008 8:08 pm
 SON OF A GUN

Joined: Mon Nov 01, 2004 5:41 am
Posts: 11605
Yes.

Edit: Well, 0 through 4 as I wrote it.

Top

 Post subject: Posted: Sat Nov 29, 2008 5:26 pm
 8086

Joined: Sun Jan 06, 2008 2:40 pm
Posts: 37
Location: Massachusetts
Loops are indepentent of eachother. A loop will run until its condition is rendered false. This behavior is the same with nesting

Code:
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}

This runs 10 times

Code:
for (int j = 0; j < 10; j++)
{
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
}

The inner loop hasn't changed a bit, it will still run 10 times.

BUT when the loop is finished, the outer loop calls the innterloop again because IT needs to loop.

I hope this helps.

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 11 posts ]

 All times are UTC - 8 hours

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

Search for:
 Jump to:  Select a forum ------------------ Maximum FAQs    Forum Rules, Posting Guidelines & Announcements    The Good, The Bad & The Banned    FAQs Help/Do It Yourself    PC Building Lab    The Help Desk    PC Modding    Education & Certification Hardware    Nuts & Bolts    Portable Talk    Appraisals, Deals & Bargains (oh my!) OS/Software/Programming    Windows Parlor    Alt.OS.Abode    Games Arena    Programmers' Paradise Networking/Internet    Internet Truckstop    Network Nook In/Out    Magazine and Book Feedback    Forum & Website Feedback    Dog Pound Team Maximum PC Folding at Home    Team Maximum PC - Folding at Home - FIND CURES TO DISEASES    Team MPC - Folding Gauntlets
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group