7 Replies - 1125 Views - Last Post: 11 September 2009 - 10:46 PM Rate Topic: -----

#1 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Conditional Expression In The For Loop

Post icon  Posted 10 September 2009 - 07:04 PM

Well...here is another code, which is a bit confusing or maybe I have spent too much timing reading the book that my brain is totally stuffed and no time to digest the information.

Here it is

using System;

class forDemo {
  static void Main () {
	 int i, j;
	 bool done = false;
   
	 for(i = 0; j = 100; !done; i++, j--) {
		
		if(i*i >= j) done = true;
	   
		Console.WriteLine("i, j: " + i + " " + j);


I just don't know how this code works. For my understanding, done is initialized to be false at the start up of the program, but when it comes or enters into the FOR loop, since !done, which means done is not false(because ! = NOT), that means it is true, so the program runs. The conditional expression determines if the FOR loop will continue and if the conditional expression, which is done in the middle of the FOR loop, is false, there is no way that the block of the statements within the body of the FOR loop will continue and the iteration will never takes place. BUT I JUST DON'T UNDERSTAND WHY in the middle of the if(i*i >= j) done = true, why do we have to set done = true and what does it do in there?

Is This A Good Question/Topic? 0
  • +

Replies To: Conditional Expression In The For Loop

#2 crepitus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 383
  • Joined: 08-September 09

Re: Conditional Expression In The For Loop

Posted 10 September 2009 - 09:22 PM

i	   j	   (i*i >= j)
0	   100	 (0*0 >= 100) is false, done remains false
1	   99	  (1*1 >= 99) is false, done remains false
2	   98	  (2*2 >= 98) is false, done remains false
3	   97	  (3*3 >= 97) is false, done remains false
4	   96	  (4*4 >= 96) is false, done remains false
5	   95	  (5*5 >= 95) is false, done remains false
6	   94	  (6*6 >= 94) is false, done remains false
7	   93	  (7*7 >= 93) is false, done remains false
8	   92	  (8*8 >= 92) is false, done remains false
9	   91	  (9*9 >= 91) is false, done remains false
10	  90	  (10*10 >= 90) is true, done is set to true

The loop runs next time, tests the expression (!done) which is false, and the loop ends.

This post has been edited by crepitus: 10 September 2009 - 09:23 PM

Was This Post Helpful? 0
  • +
  • -

#3 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 428
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: Conditional Expression In The For Loop

Posted 10 September 2009 - 09:54 PM

To clarify it a bit more, this:

if(i*i >= j) done = true;



can be written like this:
if(i * i >= j) 
{
	done = true;
}


Was This Post Helpful? 0
  • +
  • -

#4 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Conditional Expression In The For Loop

Posted 10 September 2009 - 09:57 PM

Quick question so just wanna confirm that the initial bool of (!done) is true within the for loop because of the initial value of done set to false before it enters the for loop?
Was This Post Helpful? 0
  • +
  • -

#5 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

Re: Conditional Expression In The For Loop

Posted 11 September 2009 - 08:23 AM

View Postkenryuakuma, on 10 Sep, 2009 - 10:57 PM, said:

Quick question so just wanna confirm that the initial bool of (!done) is true within the for loop because of the initial value of done set to false before it enters the for loop?


That is right. The ! operator give the reverse value. So if done starts off as false !done would be true. And the opposite is true. If done starts of as true !done would be false.
Was This Post Helpful? 1
  • +
  • -

#6 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 528
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Conditional Expression In The For Loop

Posted 11 September 2009 - 10:18 AM

So, this is two posts you've made regarding convoluted code that you are reading about in a book.

Can you tell me what book this is?

Also, can you burn this book?

I'm joking....






...kinda.
Was This Post Helpful? 0
  • +
  • -

#7 kenryuakuma  Icon User is offline

  • D.I.C Regular

Reputation: 2
  • View blog
  • Posts: 390
  • Joined: 14-December 08

Re: Conditional Expression In The For Loop

Posted 11 September 2009 - 08:31 PM

Welll...Thanks for all your helps, and I will keep posting once I encounter any problem within the book. As for this book, it is the book named "The Complete Reference C# 3.0", written by Herbert Schildt.

To be honest, every CS class requires you all at least a little bit background of programming. The instructors expect you to have a little knowledge of C. But I have never learned any C language. I learn this the hard way by reading a lot of books and stuff. Programming is the subject I am most afraid of because of its difficulty. You only take to programming once you get a little bit of success from it. Before I prepare for CS class, I have to have at least some knowledge such as how the codes work. I hope you guys could lend me a hand if I have any question.
Was This Post Helpful? 0
  • +
  • -

#8 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 528
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Conditional Expression In The For Loop

Posted 11 September 2009 - 10:46 PM

Quote

I hope you guys could lend me a hand if I have any question.


Always happy to help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1