Java Programming - Flow Control - Discussion

Discussion :: 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 ");
            }
        }
    }
}

[A]. 0 1 2
[B]. 0 1 2 1 2 2
[C]. 2 1 0 1 0 0
[D]. 2 1 2 0 1 2

Answer: Option D

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.


Sanjay said: (Jun 23, 2011)  
How 2 is printed in third iteration, ans supposed to be 21012 ?

Xyz said: (Aug 23, 2011)  
How x-2 matches in the first iteration? I didn't understand.

Derya said: (Dec 13, 2011)  
It doesn't, does it! I think this is wrong.

case x: System.out.print("0 "); x=2
case x-1: System.out.print("1 "); x=1
case x-2: System.out.print("2 "); x=0

z has 3 values 1,2,3 at each iteration of the loop, so it should only print 1 and 0

Anjena said: (Jan 14, 2012)  
Can you explain about final keyword?

Kundan said: (Feb 29, 2012)  
Final keyword used in java to declare a variable as a constant. Value of these types of variable can, t change during run time.

Hemavathi said: (Mar 27, 2012)  
I don't understand the output can anyone please explain this?

Raju said: (Apr 27, 2012)  
Friends ,
If we didn't use break keyword after case statement the next
Case also automatically printed
In first case case3 satisfies so o/p=2
In second case case2 satisfies so o/p=1 2
In third case case3 satisfies so o/p= 0 1 2
Final output is 212012

Priya said: (Sep 7, 2012)  
Great! I understand Raju. Thanks!

Ram said: (Aug 27, 2013)  
Is it necessary to declare x as final?

Singh Sahaab said: (Jan 15, 2014)  
The answer is option D because there is no break statement encountered between cases. If you'll don't believe then code and run it.

Prashant Chittam said: (Jan 28, 2014)  
For those guys who don't understand the code.

Why 3rd condition met..
x is static and its 2.

So 2-2 is the first condition for switch and for loop z=0 is true which is less than 3.

So 2 is getting print.

Now z++ is increased to 1 so the value of z=1.

Again 2-1 is the second condition for switch, and for loop z=1 is true which is less than 3.

So 1 is getting print...with 2 because after printing 1 ..there is no break so that it continue with print 2.

Now the last ...z++ is increased to 2 so the value of z=2.

Which is our static final variable which satisfies case 1.

So it prints 0 with 1 and 2. Because again there is no break so that is continues printing up to end of switch block.

Read carefully.

Ali said: (May 22, 2014)  
Raju you explained very well and easily.

Prasanth x is static but in this time final is important any way you also explained well.

Thanks all of you.

Anshu said: (Aug 6, 2014)  
Why we check the conditions from last but not from first?

Hardik Patel said: (May 23, 2015)  
But you have passed z as an argument in switch case, and in the case you are doing simple function irrespective of z.

I don't understand it what's the work of z in x, x-1, x-2.

Sekhar said: (Jun 2, 2015)  
Friends am really not getting the point could please explain any one I need a answer regarding this question?

Vijay Yadav said: (Jul 9, 2015)  
Friends x is declared as final so value of x never change please give me correct answer. I haven't satisfy by anyone answer.

Rishanth said: (Oct 18, 2015)  
If we do not have a break instruction, the next instructions in the loop gets executed!

Rahul said: (Mar 26, 2016)  
Nice programming question.

Jayashree said: (Jul 24, 2016)  
Guys! what is the first condition?

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

Rimsha Fazal said: (Dec 28, 2016)  
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?

Shital said: (Jul 1, 2017)  
Sorry but no one explains about final keyword because x is declared as final then how it's value will change.

Bagsari said: (Nov 18, 2017)  
I did not understand how final keyword works and how the control works on the switch statement?

Yeshi said: (May 15, 2018)  
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.

Khuddus said: (Jul 23, 2019)  
Remember, once a match is found all remaining statements are executed until a break statement is encountered.

Vasavi said: (Jul 27, 2019)  
Can anyone explain it clearly?

Rhin said: (Sep 4, 2021)  
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.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.