for loop in java | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

for loop in java

for(int x=0; x<=10; x=x+2) { System.out.println(x); } how this condition of for loop print even number .I dont understand how x=x+2 print even number.

21st Sep 2018, 5:13 AM
Sanaa Hareb
Sanaa Hareb - avatar
10 Answers
+ 5
it works like this...... x will start at 0 so first it will print 0, then the x=x+2 statement will execute. so x = 0 + 2. now x is 2. it will print 2 now. and everytime x will be increased by 2 and always print even numbers only.
21st Sep 2018, 5:27 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 3
As x starts from zero which is an even no. Also if you even no. in an even no. the sum is even. Here in for loop you are adding 2 in zero then in 2 and so on till 10. So the outputs are even
21st Sep 2018, 11:53 AM
Kapil Bansal
Kapil Bansal - avatar
+ 3
x equals its current value of x then increment by 2 to its value; x=0 x=0+2 x=2+2 x=4+2 x=6+2 ect ect
21st Sep 2018, 8:57 PM
D_Stark
D_Stark - avatar
+ 1
ok thanks, now its clear for me
21st Sep 2018, 6:01 AM
Sanaa Hareb
Sanaa Hareb - avatar
+ 1
here ,fill the blank with the with correct answer to form a loop x=0,____x<20___,____(x) x+=2
21st Sep 2018, 7:21 PM
inyang bryan
inyang bryan - avatar
0
please I need help
21st Sep 2018, 7:16 PM
inyang bryan
inyang bryan - avatar
0
it works like this...... x will start at 0 so first it will print 0, then the x=x+2 statement will execute. so x = 0 + 2. now x is 2. it will print 2 now. and everytime x will be increased by 2 and always print even numbers only.
23rd Sep 2018, 1:21 PM
Rajesh Kumar
Rajesh Kumar - avatar
0
it work like this. since 1st value of x=0 and that is given x=x+2; so un this case x print 2 that is even and next value of x become 2 so next again print 4 which is even no like that ...it will always print even number.
23rd Sep 2018, 1:26 PM
Rajesh Kumar
Rajesh Kumar - avatar
0
thanks again Rajesh, I appreciate .
23rd Sep 2018, 1:33 PM
inyang bryan
inyang bryan - avatar
- 1
Very nice
21st Sep 2018, 12:12 PM
Evans Dzakpata
Evans Dzakpata - avatar