Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,077 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,588 people online right now. Registration is fast and FREE... Join Now!




For Loop Help!

 
Reply to this topicStart new topic

For Loop Help!, Need help understanding 'For' Loops!

Thegnome
13 Apr, 2008 - 02:16 AM
Post #1

New D.I.C Head
*

Joined: 13 Apr, 2008
Posts: 2

Okay, well I seem to be having a hard time understanding 'For' Loops. I've recently just picked up programming again after taking classes on Java, and decided I wanted to go for C# this time. I've enjoy it alot and cant seem to put this book I am reading down. However, I seem to be stuck on this ONE 'FOR' loop. From the book it asks you to figure out how many times the Loop will run as kinda like a test your knownledge. Well I ran over the math and looked at the simple loop a million times. I cant figure out how the loop is supposed to run 8 times ( i cheated i looked in the back of the book), and also ran the program. Can someone please help explain how this program would loop 8 times befor stopping? Cause if the program runs once wont both of the loop INT be 0 and never go above that? I need some help and explaining, thanks everyone who replys. wub.gif

CODE
  
            int p = 2;
            for (int q = 2; q < 32; q = q * 2)
            {
                while (p < q)
                {
                    p = p * 2;
                }
                q = p - q;
            }


This post has been edited by Thegnome: 13 Apr, 2008 - 02:17 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: For Loop Help!
13 Apr, 2008 - 03:30 AM
Post #2

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
If this is what your book gives as an example of a for loop, get a new book. This is a puzzle, not a learning tool. For loops should NEVER have their variable manipulated outside the declaration. If you want to do that, you use a while.

Anyway, you have a computer program, make it tell you. Here's your code, with some prints added.
CODE

int loopCount = 0;
int p = 2;
for (int q = 2; q < 32; q = q * 2) {
    loopCount++;
    Console.WriteLine(loopCount + "\tfor begin: q=="+q);
    while (p < q) {
        Console.WriteLine(loopCount + "\twhile: p=="+p);
        p = p * 2;
    }
    q = p - q;
    Console.WriteLine(loopCount + "\tfor end: q=="+q);
}


CODE

1    for begin: q==2
1    for end: q==0
2    for begin: q==0
2    for end: q==2
3    for begin: q==4
3    while: p==2
3    for end: q==0
4    for begin: q==0
4    for end: q==4
5    for begin: q==8
5    while: p==4
5    for end: q==0
6    for begin: q==0
6    for end: q==8
7    for begin: q==16
7    while: p==8
7    for end: q==0
8    for begin: q==0
8    for end: q==16


Looks like 8.

Hope this helps.

User is online!Profile CardPM
+Quote Post

Thegnome
RE: For Loop Help!
13 Apr, 2008 - 04:49 AM
Post #3

New D.I.C Head
*

Joined: 13 Apr, 2008
Posts: 2

Yeah, well I am not quite sure what you mean by should NEVER have their variable manipulated outside the declaration, well I kinda get it. But I am still so unsure why the Answer comes out to 8 but I dont care anymore I think I can skip it now that I know the answer is 8, I ran the program how you stated still doesn't quite make since how it goes from 0 to 8 cause anytime you * something by 0 it is 0 so whatever. HAHA
User is offlineProfile CardPM
+Quote Post

baavgai
RE: For Loop Help!
13 Apr, 2008 - 10:05 AM
Post #4

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,019



Thanked: 105 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions
QUOTE(Thegnome @ 13 Apr, 2008 - 08:49 AM) *

Yeah, well I am not quite sure what you mean by should NEVER have their variable manipulated outside the declaration, well I kinda get it.



The reason is, the "for" loop defines all three mechanisms of a loop in one place: initial counter, continue looping condition, and incrementor. You can manipulate the declaration to do other things, but that's really the point of it.

The little mess you have here would actually be better served by a while loop, you'll be able to see what's going on clearer. Remember, a for loop can always be expressed in a while loop ( and the other way around, which is kind of messy and basically what you have here. )

So this:
CODE

for (int q = 2; q < 32; q = q * 2) {
//...
}


Can be this:
CODE

int q = 2
while(q < 32) {
//...
    q = q * 2
}


Now, let's comment the loop one more time, only use the while to help clarify ( because we can print the exit condition):
CODE

int loopCount = 0;
int p = 2;
int q = 2;
while(q < 32) {
    // we increment our counter here, it will always start with 1
    loopCount++;
    
    Console.WriteLine(loopCount + "\tq = \t"+q);
    Console.WriteLine(loopCount + "\t(p < q)\t"+(p < q));
    while (p < q) {
        p = p * 2;
        Console.WriteLine(loopCount + "\t(p * 2)\t"+p);
    }
    q = p - q;
    Console.WriteLine(loopCount + "\t(p - q)\t"+q);
    q = q * 2;
    Console.WriteLine(loopCount + "\t(q * 2)\t"+q);
    Console.WriteLine(loopCount + "\t(q < 32)\t"+(q < 32));
    Console.WriteLine("");
}



CODE

1    q =     2
1    (p < q)    false
1    (p - q)    0
1    (q * 2)    0
1    (q < 32)    true

2    q =     0
2    (p < q)    false
2    (p - q)    2
2    (q * 2)    4
2    (q < 32)    true

3    q =     4
3    (p < q)    true
3    (p * 2)    4
3    (p - q)    0
3    (q * 2)    0
3    (q < 32)    true

4    q =     0
4    (p < q)    false
4    (p - q)    4
4    (q * 2)    8
4    (q < 32)    true

5    q =     8
5    (p < q)    true
5    (p * 2)    8
5    (p - q)    0
5    (q * 2)    0
5    (q < 32)    true

6    q =     0
6    (p < q)    false
6    (p - q)    8
6    (q * 2)    16
6    (q < 32)    true

7    q =     16
7    (p < q)    true
7    (p * 2)    16
7    (p - q)    0
7    (q * 2)    0
7    (q < 32)    true

8    q =     0
8    (p < q)    false
8    (p - q)    16
8    (q * 2)    32
8    (q < 32)    false


It occurs to me that even this could be a little clearer. Play around with it if you like. Commenting code you don't follow is a very good habit to be in.

Good luck

User is online!Profile CardPM
+Quote Post

newProgram
RE: For Loop Help!
28 Oct, 2008 - 11:16 PM
Post #5

New D.I.C Head
*

Joined: 28 Oct, 2008
Posts: 30

QUOTE(Thegnome @ 13 Apr, 2008 - 03:16 AM) *

Okay, well I seem to be having a hard time understanding 'For' Loops. I've recently just picked up programming again after taking classes on Java, and decided I wanted to go for C# this time. I've enjoy it alot and cant seem to put this book I am reading down. However, I seem to be stuck on this ONE 'FOR' loop. From the book it asks you to figure out how many times the Loop will run as kinda like a test your knownledge. Well I ran over the math and looked at the simple loop a million times. I cant figure out how the loop is supposed to run 8 times ( i cheated i looked in the back of the book), and also ran the program. Can someone please help explain how this program would loop 8 times befor stopping? Cause if the program runs once wont both of the loop INT be 0 and never go above that? I need some help and explaining, thanks everyone who replys. wub.gif

CODE
  
            int p = 2;
            for (int q = 2; q < 32; q = q * 2)
            {
                while (p < q)
                {
                    p = p * 2;
                }
                q = p - q;
            }



QUOTE

i will show you a very simple code using for loop. this program will print "a" 8 times on the screen.

CODE

for(int i = 0; i<9; i++)
{
   Console.WriteLine("a");
}


as you can see the first part of the for loop "int i=0" you will initialize an intiger and its value. second "i<9" is the condition that if the value of "i" is nine the program will exit the loop. the third one "i++" is the incremetation or decrementation that add a value on the variable.


User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 07:28PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month