Java Break Statement When a break statement is encountered inside a loop, the loop is immediately terminated and the program control resumes at the next statement following the loop. The Java break statement is used to break loop or switch statement. It breaks the current flow of the program at specified condition. In case of inner loop, it breaks only inner loop. We can use Java break statement in all types of loops such as for loop , while loop and do-while loop . Syntax: jump-statement ; break; Java Break Statement with Loop Example: //Java Program to demonstrate the use of break statement //inside the for loop. public class BreakExample { public static void main (String[] args) { //using for loop for ( int i= 1 ; i<= 10 ; i++)...
Java Switch Statement The Java switch statement executes one statement from multiple conditions. It is like if-else-if ladder statement. The switch statement works with byte, short, int, long, enum types, String and some wrapper types like Byte, Short, Int, and Long. Since Java 7, you can use strings in the switch statement. In other words, the switch statement tests the equality of a variable against multiple values. Points to Remember There can be one or N number of case values for a switch expression. The case value must be of switch expression type only. The case value must be literal or constant . It doesn't allow variables . The case values must be unique . In case of duplicate value, it renders compile-time error. The Java switch expression must be of byte, short, int, long (with its Wrapper type), enums and string . Each case statement can have a break statement which is...
Comments
Post a Comment