+ 1

What is the value of x?

int x; for(x=0;x<=6;x++) {} please explain

31st Dec 2016, 7:14 AM
Ansar
7 Réponses
+ 3
value of x will be 7 because when value of x is 6 the condtion x<=6 will be true then loop statement will execute, then x value will be incremented by 1 i.e x=6+1=7 now loop will terminate because Condtion x<=6 is false becuase x=7 hence value of x after loop execution will be 7
31st Dec 2016, 7:26 AM
Ravi Kumar
Ravi Kumar - avatar
+ 3
No: it's 7... Because: Even if the loop body is empty, the structure of it still the for loop: - initialization : x=0 - (loop start) - if condition : x<=6 - loop body : {} - end loop instruction : x++ - go to (loop start) - else go (next) - (next) x was setting with zero, and at each iteration grow to 6, obiouvsly, but examine the 6 case in details: The condition (x<=6) is true, so loop body AND end loop instructions are executed... so, x++ is executed, and now x value is 7, and we go again to loop start, where testing condition's no more true, so else statement is executed, and the value of x still remain 7.
31st Dec 2016, 7:37 AM
visph
visph - avatar
+ 2
at the end of the loop it is 7
31st Dec 2016, 7:24 AM
Vaibhav Tandon
Vaibhav Tandon - avatar
+ 1
thanks bro
31st Dec 2016, 7:26 AM
Ansar
+ 1
The loop repeats itself, increasing the value of x, until the condition becomes false. The condition in this case is x <= 6, which means that this for loop will stop once the value of x becomes 7, therefore, at the end of the loop, x = 7
31st Dec 2016, 7:27 AM
Dao
Dao - avatar
0
Answer is 7. for loop fails at the condition where x goes higher than 6. integer value after 6 is 7. So , Anwer is 7
31st Dec 2016, 7:31 AM
Shiv Kumar
Shiv Kumar - avatar
- 1
I think 6
31st Dec 2016, 7:16 AM
Ansar