class Ques0357{
public static void main(String[] argv){
int s = 2;
switch (s) {
case 1:
System.out.println(”Fred”);
break;
case 2:
System.out.println(”Fast”);
case 3:
System.out.println(”Tech”);
default:
System.out.println(”default”);
}
}
}

The break statement is used inside the code block following the switch statement to terminate the execution of a statement sequence. As soon as a break statement is encountered, the program control is transferred to the first line of code that follows the entire switch statement.

  • Share/Bookmark

The Switch statement

The switch statement is a multiway branch statement. The general form of the switch statement is given below:

switch(expression) {
case value1://statements
break;
case value2://statements
break;
.
.
default:
//default statements
}

The expression must be of int, char, byte, or short data type and the data type of the values specified must be compatible with the expression.

Each case value must be a unique literal value. Variables are not allowed as case values.

  • Share/Bookmark