What output is produced by the following code int top 4 n
What output is produced by the following code?
int top = 4, n = -1;
while( n <= top)
{
switch(n)
{
case 1:
System.out.println(\"ONE.\");
break;
case 3:
System.out.println(\"Three.\");
break;
case 4:
System.out.println(\"Four.\");
break;
default:
System.out.println(\"Default case.\");
break;
}
n++;
}
Note: This is Java programming
Solution
n = -1
it will print Default case.
n = -1+1 = 0
it will print Default case.
n = 0+1 = 1
it will print ONE.
n = 1+1 = 2
it will print Default case.
n = 2+1 = 3
it will print Three.
n = 3+1 = 4
it will pring Four.
Hence the output is :
Default case.
Default case.
ONE.
Default case.
Three.
Four.

