Page 129 - CA_Blue( J )_Class10
P. 129
Example of the switch statement is: Start
public class SwitchExample {
public static void main(String[] args) {
number=2
int number = 2; // Input number
switch (number) {
true
case 1: case1 Print "One"
Break
System.out.println("One");
break; false
case 2:
System.out.println("Two"); true Print "Two"
case2
break; Break
default:
false
System.out.println("Wrong choice");
break;
Print "Wrong"
} default Chart
}
} Flowchart of switch statement End
Note: The value passed as the choice in the switch statement should be int or char type. Floating-point
value is not supported by the Java in the switch statement.
Program 9 To input the side of a square and print the area and perimeter according to user's choice.
1 import java.util.*;
2 class square
3 {
4 public static void main()
5 {
6 Scanner sc= new Scanner (System.in);
7 int s,a,p,ch;
8 System.out.print("Enter side of a square: ");
9 s=sc.nextInt();
10 System.out.print("Enter 1 for Area and 2 for Perimeter: ");
11 ch=sc.nextInt();
12 switch(ch)
13 {
14 case 1: a = s * s;
15 System.out.println("Area : "+a);
127
Conditional Constructs in Java 127

