0

Please explain this even number generator ?

public class Program { public static void main(String[] args) { for(int x=0; x<=10; x++) System.out.println(x++); } }

25th Jul 2020, 12:19 PM
Mons Joseph
Mons Joseph - avatar
4 Respostas
+ 3
First 0 is printed and then it gets incremented by due to x++ in print statement and then it gets incremented by 1 again inside for loop ,so now x is 2 and it is printed and so on
25th Jul 2020, 12:24 PM
Abhay
Abhay - avatar
+ 1
Every loop prints in a newline
25th Jul 2020, 12:32 PM
George Onyango
+ 1
Mons Joseph because '0' is also considered as an even number. Now, increment operator is used to increment value by 1 and it is of two types - post increment operator and pre increment operator. Over here , we are using post increment operator in which the value is first used and then incremented. Like over here, in this code first the value is printed and then incremented by one. Hope it helps...
25th Jul 2020, 5:17 PM
Smriti Kaur
Smriti Kaur - avatar
0
# Abay if so, why is the first output 0 and not 2?
25th Jul 2020, 4:16 PM
Mons Joseph
Mons Joseph - avatar