0
Interesting output in Java code. Why 103? X+x+100 a prioro can't be odd. I had predicted output 104, but not 103.
7 Respuestas
+ 3
Look at the loop definition, in there <x> is given value of 1. The loop condition says to repeat while <x> is less then 10.
for(x = 1; x < 10; x++)
In loop body, <x> value is incremented by its own value, plus 100. So in the first repetition ...
x = 1 , initialized in loop definition. And then ...
x = 1 + 1 + 100 , so by now <x> is 102
Next, the `x++` in the loop definition increment <x> by one, so after that is done, <x> will be 103.
Then loop checks <x> value against the loop condition (x < 10). This time it sees that the condition is no longer true, meaning the loop should stop.
And there you have it. <x> value be 103 after the loop completed.
+ 4
Oleg
I didn't understand your last reply ...
+ 3
First x=1
In first loop:
x<10 become true
next for loop statements
x=x+x+100
x=1+1+100
x=102
Next x value increment by for loop
x++ => x=x+1
x=102+1=103
The second loop became false.
Now print the value of x is 103.
+ 2
Am I right understand that x++ (incrementation in "For") occur even when !x<10 (or another words >10)?
+ 2
Thanks Ipang. I stopped my car (I was on the road). Read carefully your description and I understand the logic.
+ 2
Oleg
Oh alright then, I'm glad if it helps 👌
Thanks Oleg ...
+ 1
first iteration x is 1
so x = x+x+100 evaluate to
x = 1+1+100
x = 102.
check for condition x<100; 102<10. false.
evaluate increment inside for
x++ > 102++
103