Java Programming - Flow Control - Discussion

Discussion Forum : Flow Control - Finding the output (Q.No. 4)
4.
What will be the output of the program?
public class SwitchTest 
{  
    public static void main(String[] args) 
    {
        System.out.println("value =" + switchIt(4)); 
    } 
    public static int switchIt(int x) 
    {
        int j = 1;  
        switch (x) 
        { 
            case l: j++; 
            case 2: j++;  
            case 3: j++; 
            case 4: j++; 
            case 5: j++; 
            default: j++; 
            } 
        return j + x;  
    } 
}
value = 2
value = 4
value = 6
value = 8
Answer: Option
Explanation:

Because there are no break statements, once the desired result is found, the program continues though each of the remaining options.

Discussion:
14 comments Page 1 of 2.

Anukirti said:   5 years ago
How int j =1; is valid as we cannot declare any local variable inside a static method?

Venkatesh said:   6 years ago
The default case value of j=3.
Once it comes out of switch case post-increment of j is there (i.e. Value of j=3 but j++ is there so j becomes 4.
So finally value will be x+j = 4+4 = 8.
So the answer will be 8.
(2)

Venky said:   6 years ago
Nice, thanks for explaining.

Shreyas singh said:   9 years ago
I think it's been initiating from case 4, so on case 4 j=2, case 5 j=3, default case j=4, so now we have value of x as 4, at the end its been said to add this two, so x+j =8.
(1)

Varooon said:   10 years ago
Default : j++; will also will be executed, Because of the Absence of "break" statement. This is the key thing to note here.

Unknown 21 said:   1 decade ago
Our condition is x=4;

Now, move to case 4, where x=4 and j=1 (a/c to loop , but after the complete execution of j++ in case 4, j becomes 2 (due to post increment i.e, ++).

Case 5: Starts with j=2; and after complete execution of j++ in case 5, j becomes 3.

Now, default takes j=3, and after execution of j++, j becomes 4.

And at return:x=4 and j=4; which gives 4+4=8.

Kunal said:   1 decade ago
Hopefully everyone has understood till default case value of j=3.

Once it comes out of switch case postincrement of j is there (i.e. value of j=3 but j++ is there so j becomes 4).

So finally value will be x+j = 4+4 = 8.

So answer will be 8.

Shanmukasrikanth said:   1 decade ago
Here x=4 and j=1 when ever the switch is executed. The control goes to case 4. So in case 4 value of j=2 and their is no break statement in the cases so remaining. Cases will execute as it is so in case 5 value of j=3 in default value of j=4. And at last the function is of type. Int so it return some value. So it returns x+j i.e. 4+4=8. That's it.

Srikanth said:   1 decade ago
@Aashish, thank you.

Ashwini Surya said:   1 decade ago
Line no 5 SwitchIt(4) declare the value of SwitchIt, Then in line 7 SwithchIt defines x so 4 is the value indicate to x.

Then, at line 10 switch(x),x value is 4 so loop go to at case 4 and it increase by 1 at every case so there are three cases on that time y=3 and x=4 but there is declare the value y=1 means y's value is 4.

So answer will be 8.


Post your comments here:

Your comments will be displayed after verification.