Easy Tutorial
For Competitive Exams

What is the output of this program?
class comma_operator {
public static void main(String args[])
{
int sum = 0;
for (int i = 0, j = 0; i < 5 & j < 5; ++i, j = i + 1)
sum += i;
System.out.println(sum);
}
}

5
6
14
compilation error
Explanation:
Using comma operator , we can include more than one statement in the initialization and iteration portion of the for loop.
Therefore both ++i and j = i + 1 is executed i gets the value – 0,1,2,3,4 & j gets the values -0,1,2,3,4,5.
output:
$ javac comma_operator.java
$ java comma_operator
6
Share with Friends
Privacy Copyright Contact Us