### Thread: Help on for loop condition

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
11
Rep Power
0

#### Help on for loop condition

Hi

I was reading The complete reference and below is code for one of the programs there.

Code:
```#include <stdio.h>

int sqrnum(int num);
int prompt(void);

int main(void)
{
int t;

printf("The value of t is %d \n",t); //This is added by me

sqrnum(t);

return 0;
}

int prompt(void)
{
printf("Enter a number: ");
return 0;
}

{
int t;

scanf("%d", &t);
return t;
}

int sqrnum(int num)
{
printf("%d\n", num*num);
return num*num;
}```
The above FOR loop exits when the entered value is 0. For better understanding I have printed it to check it the value of t is defaulted to 0 but below is the output

Code:
```The value of t is 692
Enter a number: 2
4
Enter a number: 692
478864
Enter a number: 0```
<exists>

Could some one please help me in understanding how the loop exists when entered value is 0

Thanks
2. Zero is false, non-zero is true. When readnum returns zero and assigns it to t, that evaluates to zero which is false. False causes you to exit the for loop.
3. No Profile Picture
Contributing User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
40
Rep Power
19
Syntax:

for( expression1 ; expression2 ; expression3 ) statement...

expression1 is evaluated before the first iteration.
After each iteration, expression3 is evaluated.
Both expression1 and expression3 may be ommited.
If expression2 is ommited, it is assumed to be 1. statement is executed repeatedly until the value of expression2 is 0.
The test on expression2 occurs before each execution of statement.

0 == false --> exit
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Jun 2013
Posts
11
Rep Power
0
Thank you for the reply

I wasn't aware that the test operator assigns the lvalue to the variable AND ALSO RETURNS THE LVALUE .

Was under the understanding that assignment operator on successful assignment (even with successful assignment if 0) returns true some what like unix flavour but now things look clear :)