retagged by
317 views

1 Answer

Best answer
8 votes
8 votes

Initially variable i is only declared. So, it will contain some garbage value.

 

NOTE: If int variable is declared only (not initialized), it will contain some garbage value.

 

After that, let’s come to for loop : 

1. The portion 1 of the for loop (which is generally called as initialization part), will update the value of i and

set it to 1.

2. Then, portion 2 of the for loop (which is generally called as condition checking) will again update

the value of i to -1.

3. Now, it will go inside for loop (which is generally called a content of for loop), where it will find

if condition. As the value of i = -1 < 5. So, it will come out of for loop.

 

NOTE :   As curly braces are missing in the for loop syntax, then the statement which is immediately

               following the for loop, will be treated as the content of for loop. 

Then it prints the value of i, which will print -1.

Output : -1

edited by

Related questions

0 votes
0 votes
1 answer
1
0 votes
0 votes
0 answers
2
Sgm asked Dec 28, 2022
390 views
Any one please let me know if there is any other way to this output?
4 votes
4 votes
3 answers
3