Page 128 - CA_Blue( J )_Class10
P. 128
12 System.out.print("Enter third number :");
13 c=sc.nextInt();
14 System.out.println("--------------------------");
15 g=(a>b)? (a>c)?a:c:(b>c)?b:c;
16 System.out.println("Greatest Number :"+g);
17 }
18 }
You will get the following output:
Let us see some more examples of the Ternary operator.
7.4 SWITCH CASE STATEMENT
Till now, you have used if…else…if ladder statement to check multiple conditions. Due to the use of multiple if statements,
the program becomes more complex and lengthy. To avoid such problems, Java provides the switch statement. The
switch statement executes certain statements depending on the user's choice. The case statement is used to define
different cases where each case has a unique label and a set of statements associated with it. The switch statement
can be used as a substitute for the if…else…if ladder. The syntax of the switch statement is:
switch(choice)
{
case 1:
Statements of block 1;
break;
case 2:
Statements of block 2;
break;
.
.
.
default:
Statements of block last;
}
In the preceding syntax, you have seen that the switch statement takes an expression as choice. This choice is matched
with the label written in front of every case statement. If the choice is 1, case 1 is executed. Then, the break statement
will terminate the statement block associated with the case statement and send the control out of the switch block. If
the choice is 2, then case 2 will be executed and then the break statement will send the control out of the switch block.
If the choice is not matched with any of the given cases, then the default case will execute as the last statement. There
is no need for the break statement inside the default case as it is the last statement of the switch block.
126126 Touchpad Computer Applications-X

