Menu
Question Index
...

What will be the output of the following program?

class WhileExample
{
    public static void main(String s[])
    {
        int n = 0;

        int sum = 0;

        while( n <= 6 )
        {
            sum += n--;
        }

        System.out.println("sum = " + sum);
    }
}


sum = 21
sum = 28
Some other output
n = 0
n = 1
n = 2
n = 3
n = 4
n = 5
n = 6
sum = 21
Goes into infinite loop

Doubts

Problems

Topic: while Loop In Java

Read this topic
Take test on this topic

0
Wrong
Score more than 2 points

© meritcampus 2019

All Rights Reserved.

Open In App