Effect of Break Statements In a Switch Structure

//Effect of break statements in a switch structure
//Filename: BreakStatementsInSwitch.java

import java.util.*;
public class BreakStatementsInSwitch
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
int num;

System.out.print("Enter an integer between "+ "0 and 10: "); //Line 1
num = console.nextInt(); //Line 2
System.out.println(); //Line 3
System.out.println("The number you entered "+ "is " + num); //Line 4

switch (num) //Line 5
{
case 0: //Line 6
case 1: //Line 7
System.out.print("Hello "); //Line 8
case 2: //Line 9
System.out.print("there. "); //Line 10
case 3: //Line 11
System.out.print("I am "); //Line 12
case 4: //Line 13
System.out.println("Mickey."); //Line 14
break; //Line 15
case 5: //Line 16
System.out.print("How "); //Line 17
case 6: //Line 18
case 7: //Line 19
case 8: //Line 20
System.out.println("are you?"); //Line 21
break; //Line 22
case 9: //Line 23
break; //Line 24
case 10: //Line 25
System.out.println("Have a nice day."); //Line 26
break; //Line 27
default: //Line 28
System.out.println("Sorry the number is "+ "out of range."); //Line 29
}

System.out.println("Out of switch " + "structure."); //Line 30
}
}

/*
Sample Output
These outputs were obtained by executing the preceding program several times. In each
of these outputs, the user input is shaded.

Sample Run 1:
Enter an integer between 0 and 10: 0
The number you entered is 0
Hello there. I am Mickey.
Out of switch structure.

Sample Run 2:
Enter an integer between 0 and 10: 3
The number you entered is 3
I am Mickey.
Out of switch structure.

Sample Run 3:
Enter an integer between 0 and 10: 4
The number you entered is 4
Mickey.
Out of switch structure.

Sample Run 4:
Enter an integer between 0 and 10: 7
The number you entered is 7
are you?
Out of switch structure.

Sample Run 5:
Enter an integer between 0 and 10: 9
The number you entered is 9
Out of switch structure.

Explanation

 A walk-through of this program, using certain values of the switch expression num, can
help you understand how the break statement functions. If the value of num is 0,
the value of the switch expression matches the case value 0. All statements following
case 0: execute until a break statement appears.

 The first break statement appears at Line 15, just before the case value of 5. Even
though the value of the switch expression does not match any of the case values (1, 2,
3, or 4), the statements following these values execute.
When the value of the switch expression matches a case value, all statements execute until
a break is encountered, and the program skips all case labels in between. Similarly, if the
value of num is 3, it matches the case value of 3 and the statements following this label
execute until the break statement is encountered at Line 15. If the value of num is 9, it
matches the case value of 9. In this situation, the action is empty, because only the break
statement, at Line 24, follows the case value of 9.

*/

No comments: