Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - Finding the output (Q.No. 3)
3.
What will be the output of the program?
public class Switch2 
{
    final static short x = 2;
    public static int y = 0;
    public static void main(String [] args) 
    {
        for (int z=0; z < 3; z++) 
        {
            switch (z) 
            {
                case x: System.out.print("0 ");
                case x-1: System.out.print("1 ");
                case x-2: System.out.print("2 ");
            }
        }
    }
}
0 1 2
0 1 2 1 2 2
2 1 0 1 0 0
2 1 2 0 1 2
Answer: Option
Explanation:

The case expressions are all legal because x is marked final, which means the expressions can be evaluated at compile time. In the first iteration of the for loop case x-2 matches, so 2 is printed. In the second iteration, x-1 is matched so 1 and 2 are printed (remember, once a match is found all remaining statements are executed until a break statement is encountered). In the third iteration, x is matched. So 0 1 and 2 are printed.

Discussion:
26 comments Page 1 of 3.

Rhin said:   4 years ago
Let me explain,

public class Switch2
{
final static short x = 2;//it will never change the value of x
public static int y = 0;
public static void main(String [] args)
{
for (int z=0; z < 3; z++)
{
switch (z)
{
case x: System.out.print("0 ");
case x-1: System.out.print("1 ");
case x-2: System.out.print("2 ");
}
}
}
}


Iteraton->1 :z=0->switch(0)
goes to case (x-2)-> case(2-2)->case(0):
So 2 will be printed .

======================
Iteration->2 z gets incremented ->z=1->switch(1)
goes to case(x-1)->case(2-1) ->case(1):
prints 1 and there is no break statement so goes to next print statement prints 2.

======================
Iteration->3 z gets incremented ->z=2 ->switch(2)
goes to case(x)->case(2)->case(2):
prints 0 and there is no break statement so 1 and 2 will gets printed
ANd the final output will be->2 1 2 0 1 2.
(1)

Vasavi said:   6 years ago
Can anyone explain it clearly?

Khuddus said:   6 years ago
Remember, once a match is found all remaining statements are executed until a break statement is encountered.

Yeshi said:   7 years ago
Hi.

1. The Final Keyword is like constant keyword in C, we can not re-assign the value once it is declared.

2. As per switch statement, when the value of z is 0 than the case with 0 will get executed.
( switch statement compares the value of a variable to the values specified in case of statements then first).

So first time 0 is printed.

Similarly for other scenarios.

3.Y is just initialized but not used just to make the program look complex.

Bagsari said:   8 years ago
I did not understand how final keyword works and how the control works on the switch statement?

Shital said:   8 years ago
Sorry but no one explains about final keyword because x is declared as final then how it's value will change.

Rimsha Fazal said:   9 years ago
I didn't understand how the conditions are matching in the program. There are simple values given by z in every iteration which is executed and printed what's the roll of x and y then?

Jayashree said:   9 years ago
Guys! what is the first condition?

What is the connection between loop, x and also cases?

Rahul said:   9 years ago
Nice programming question.

Rishanth said:   10 years ago
If we do not have a break instruction, the next instructions in the loop gets executed!


Post your comments here:

Your comments will be displayed after verification.