# Conditional Expression In The For Loop

Page 1 of 1

## 7 Replies - 1494 Views - Last Post: 11 September 2009 - 10:46 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=125109&amp;s=7f5e031b217062fc734f9eeb32f3e693&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 408
• Joined: 14-December 08

# Conditional Expression In The For Loop

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

• D.I.C Regular

Reputation: 85
• 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

### #3 janne_panne

• WinRT Dev

Reputation: 428
• 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;
}

```

### #4 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 408
• 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?

### #5 SixOfEleven

• Planeswalker

Reputation: 1055
• Posts: 6,643
• Joined: 18-October 08

## Re: Conditional Expression In The For Loop

Posted 11 September 2009 - 08:23 AM

kenryuakuma, 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.

### #6 MentalFloss

• .

Reputation: 577
• Posts: 1,500
• 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.

### #7 kenryuakuma

• D.I.C Regular

Reputation: 4
• Posts: 408
• 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.

### #8 MentalFloss

• .

Reputation: 577
• Posts: 1,500
• 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.