Case Control Statement



Case Control Statement: -
The statements which are used to execute only specific block of statements in a series of blocks are called case control statements.
Types of Case control statements: -
There are four types of case control Statements
a) Switch                                 b) Continue                
c) Break                                             
1) Switch: -
Switch statement is used to execute only specific case statements based on the switch expression.
Syntax:-
switch (expression)
{
case 1:   
statement 1;
break;
case 2:
statement 2;
break;
default: 
statement 3;
break;
}
Write a program to print Days of week using switch :-
import java.util.Scanner;
class switch1
{
public static void main(String args[])
{
int n;
Scanner in = new Scanner(System.in);
System.out.println("Enter the number 1 to 7");

n = in.nextInt();
switch(n)
{
case 1:
System.out.println("Sunday");
break;
case 2:
System.out.println("Monday");
break;
case 3:
System.out.println("Tuesday");
break;
case 4:
System.out.println("Wednesday");
break;
case 5:
System.out.println("Thusday");
break;
case 6:
System.out.println("Friday");
break;
case 7:
System.out.println("Saturday");
break;
default:
System.out.println("WRONG ENTER");
break;
}
}
}
OUTPUT:- Enter the number 1 to 7
4
Wednesday

Example 2: -
Write a program to print sum, sub and muliply using switch
import java.util.Scanner;
class switch1
{         
public static void main(String args[])
{
int n,a,b,c;
Scanner in = new Scanner(System.in);
System.out.println("Chosse 1 for sum \n 2 for subtract \n 3 for multiply \n 4 for exit");
n = in.nextInt();
switch(n)
            {
                        case 1:
                                    System.out.println("Enter the two numbers for sum ");
                                    a = in.nextInt();
                                    b = in.nextInt();
                                    c = a+b;
                                    System.out.println("Sum = "+c);
                        break;
                        case 2:
                                    System.out.println("Enter the two numbers for sub ");
                                    a = in.nextInt();
                                    b = in.nextInt();
                                    c = a-b;
                                    System.out.println("Sub = "+c);
                        break;

                        case 3:
                                    System.out.println("Enter the two numbers for sub ");
                                    a = in.nextInt();
                                    b = in.nextInt();
                                    c = a*b;
                                    System.out.println("Multiply = "+c);
                        break;
                        case 4:
                        break;
                        default:
                                    System.out.println("unknown number ");
            }
}
}
OUTPUT:-

You like my post please share with friends

No comments:

Post a Comment