For Loop Help!

Need help understanding 'For' Loops!

Page 1 of 1

4 Replies - 1453 Views - Last Post: 29 October 2008 - 12:16 AM Rate Topic: -----

#1 Thegnome   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-April 08

For Loop Help!

Posted 13 April 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:

  
			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 April 2008 - 03:17 AM


Is This A Good Question/Topic? 0
  • +

Replies To: For Loop Help!

#2 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7505
  • View blog
  • Posts: 15,553
  • Joined: 16-October 07

Re: For Loop Help!

Posted 13 April 2008 - 04:30 AM

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.
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);
}



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.
Was This Post Helpful? 0
  • +
  • -

#3 Thegnome   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 13-April 08

Re: For Loop Help!

Posted 13 April 2008 - 05: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. 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
Was This Post Helpful? 0
  • +
  • -

#4 baavgai   User is online

  • Dreaming Coder
  • member icon


Reputation: 7505
  • View blog
  • Posts: 15,553
  • Joined: 16-October 07

Re: For Loop Help!

Posted 13 April 2008 - 11:05 AM

View PostThegnome, on 13 Apr, 2008 - 08:49 AM, said:

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:
for (int q = 2; q < 32; q = q * 2) {
//...
}



Can be this:
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):
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("");
}




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
Was This Post Helpful? 0
  • +
  • -

#5 newProgram   User is offline

  • D.I.C Head

Reputation: 4
  • View blog
  • Posts: 160
  • Joined: 28-October 08

Re: For Loop Help!

Posted 29 October 2008 - 12:16 AM

View PostThegnome, on 13 Apr, 2008 - 03:16 AM, said:

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:

  
			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.

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.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1