Write the output of the following Java program and explain t
Solution
Test.java
public class Test {
public static void main(String args[])
{
int index =3;
switch(index)
{
case 1:System.out.println(\"equal to 1\");break;
case 2:System.out.println(\"equal to 2\");break;
case 3:System.out.println(\"equal to 3\");
case 4:System.out.println(\"equal to 4\");
default:System.out.println(\"others\");
}
}
}
Output :
Explanation :
The expression used in a switch statement is index whose value is 3.
So case 3 will execute.
If a break statement is reached, the switch terminates.
Here no break appears in case 3, so the flow of control will fall through to subsequent cases until a break is reached.
There is no break statement after case 3 so switch terminates after default case.
![Write the output of the following Java program and explain the results. public class Test {public static void main (String args[]} int index-3; switch(index} { Write the output of the following Java program and explain the results. public class Test {public static void main (String args[]} int index-3; switch(index} {](/WebImages/35/write-the-output-of-the-following-java-program-and-explain-t-1104079-1761583974-0.webp)