Practical No. 4: Develop programs to demonstrate use of switch - case statement and conditional if (?:)

1) Write any program using switch-case statement
      For Program Click here

Practical Related Questions

1) What will happen if break is not written for a case in switch case?
Ans=> If break statement is not written for a case then after if case condition is true then all the case will execute until break statement not come.

2) When default case is executed?
Ans=> When every case becomes false then default case is going to be execute.

3) List datatypes allowed in switch expression?
Ans=> A switch works with the byte, short, char, and int primitive data types. It also works with enumerated types.

4) Write a program to make use of ternary operator.
  For Program Click here

Exercise

1) State the Output

    1) 
public class SwitchCaseExample1
{
public static void main( String args[] )
{
int num=2;

switch(num+2)
{
case 1 : System.out.println(" Case1 Value is:"+num);  

case 2 : System.out.println(" Case2 Value is:"+num);  

case 3 : System.out.println(" Case3 Value is:"+num);  
 
default : System.out.println(" Default Value is:"+num);  

}
}
}


Output:

  2) 
public class Program
{
public static void main( String args[] )
{
int value = 100;

switch( value )
{
case 100: System.out.println(true);
  break;
 
case 100: System.out.println(true);
   break;
}
}

}


Output:


2) Write any program to check switch-case statement using character datatype.


Previous
Next Post »