+ 2

I am little bit confused about this equation. How this code result to 6 ?

int i; for(i = 1; i <=5; i++); System.out.print(i);

11th Apr 2018, 1:42 PM
Jemwel Godinez
Jemwel Godinez - avatar
6 Respuestas
+ 2
Your reasoning is found in this: i <=5; ^Varible 'i' starts off at 0 and is ending at 5 (less than-equal to). 0-5 is 6 spaces. If you did i < 5 then it would display 5 numbers (0-4) rather than 6 numbers. Also, you're declaring variable 'i' twice, so you'll prob run into compile issues. Remove 'int i' from before the loop.
11th Apr 2018, 1:47 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 4
@Xan "Varible 'i' starts off at 0 and is ending at 5 (less than-equal to). 0-5 is 6 spaces. If you did i < 5 then it would display 5 numbers (0-4) rather than 6 numbers. " Also, put a check next to Xan's answer instead of placing your own comment as the correct answer to the question/thread.
11th Apr 2018, 1:53 PM
Fata1 Err0r
Fata1 Err0r - avatar
+ 3
6 is never output. I think you mean why is System.out.print(i) called 6 times. it's the <= which means Less Than OR Equal. 5 is less than or equal to 5. Also, no need to declare i twice.
11th Apr 2018, 1:46 PM
Emma
+ 2
https://code.sololearn.com/c5tlY2u9gyI8/?ref=app I think Kent it was challenge question so if you want output 6: then this is may be coding question
11th Apr 2018, 2:38 PM
Nitish kumar jha
Nitish kumar jha - avatar
+ 1
sorry...i edit one that need corrections.. thank you for your answers..i understand it now...👍👍
11th Apr 2018, 1:47 PM
Jemwel Godinez
Jemwel Godinez - avatar
0
I believe Jakob is incorrect. I is 6 by the time it is printed because the i++ in the loop adds 1 once after the final i<=5 evaluation. The i=1 part of the for loop resets i to 1 when the for loop starts the first time
12th Apr 2018, 3:27 PM
Phil
Phil - avatar