Skip to main content

What is the purpose of break statement in Java?

 In the Java programming language, the "break" statement serves a crucial role in altering the flow of control within loops and switch statements. It is employed for the following primary purposes:

1.     Loop Termination: Within loop constructs such as "for," "while," or "do-while," the "break" statement allows for an immediate exit from the loop's execution. This is particularly useful when a specific condition is met, and there is a need to prematurely terminate the loop.

 

for (int i = 0; i < 10; i++) {

    if (i == 5) {

        break; // The loop exits when 'i' equals 5

    }

    System.out.println(i);

}

In this example, the loop will print numbers from 0 to 4 and then exit when the value of 'i' becomes 5.

 

2.

Switch Statement Exit: Within a "switch" statement, the "break" statement is employed to exit the switch block immediately after the execution of a case statement. It prevents the control from falling through to subsequent cases, ensuring that only the desired case is executed.

 

int choice = 2;

switch (choice) {

    case 1:

        System.out.println("Option 1 selected");

        break;

    case 2:

        System.out.println("Option 2 selected");

        break;

    default:

        System.out.println("Invalid option");

}

In this example, the "break" statements prevent control from reaching the "default" case after printing "Option 2 selected" because the value of 'choice' is 2.

3. Exiting Nested Loops: The "break" statement can also be utilized to exit from nested loops. When encountered in a nested loop, it exclusively exits the innermost loop while leaving outer loops unaffected.

for (int i = 0; i < 3; i++) {

    for (int j = 0; j < 3; j++) {

        if (i + j == 3) {

            break; // The inner loop exits when 'i + j' equals 3

        }

        System.out.println(i + ", " + j);

    }

}

In this example, the "break" statement exclusively exits the inner loop when the condition "i + j == 3" is met.

In summary, the "break" statement in Java is a vital control flow mechanism, enabling programmers to exit loops and switch statements under specific conditions, ultimately enhancing the flexibility and control of Java programs.

 

 

 

 

 

 

 

 

 

 

 

Comments

Popular posts from this blog

Java 8 Programs Asked In Interview For Experienced Professionals | Java 8 Coding Interview Questions Asked In The Interview.

 Q1) // Given Two Strings. Find Out the Two Strings are Anagrams or not.     // Anagram means a String which has same characters present with the another     // String,     // Only the sequence of the Characters are different. package J ava C oncept O f D ay ; import java . util . stream . Collectors ; import java . util . stream . Stream ; public class AnagramStrings {     // Given Two Strings. Find Out the Two Strings are Anagrams or not.     // Anagram means a String which has same characters present with the another     // String,     // Only the sequence of the Characters are different.     public static void main ( String [] args ) {         String str1 = "Listen" ;         String str2 = "Silent" ;         str1 = Stream . of ( str1 . split ( "" )). map ( String :: toLowerCase ). sorted (). collect ( Collectors . joining ());...

Java 8 Coding Interview Questions | Asked In Many Interviews

 Q 1) Given A List . Write Java 8 Program to find Out 1st Element From List? ===================================================================== import java . util . Arrays ; import java . util . List ; public class FirstElementOfList {     // Given a List. Find First Element From the List       public static void main ( String [] args ) {                     List < Integer > list = Arrays . asList ( 10 , 15 , 24 , 7 , 24 , 74 , 10 , 7 );             list . stream (). findFirst (). ifPresent ( System . out :: println );     } } Q 2) Given A List ? Find Out the Count of All Element In A List? ============================================================================== import java . util . Arrays ; import java . util . List ; public class TotalNumberOfElements {     public static void main ( String [] args ) {       ...

Java Programs Asked In An Interview | Java 8 | Core Java

 Q1) // Write a Java Program To Find Out the Character Occurnaces In A String. import java . util . HashMap ; import java . util . Map ; public class CharOccurnacesInString {     // Write a Java Program To Find Out the Character Occurnaces In A String.     public static void main ( String [] args ) {         String str = "I am Sandeep Aswar" ;         str = str . replaceAll ( " " , "" ). toLowerCase ();         char [] ch = str . toCharArray ();         Map < Character , Integer > charMap = new HashMap <>();         for ( char c : ch ) {             if ( charMap . containsKey ( c )) {                 charMap . put ( c , charMap . get ( c ) + 1 );             } else {                 charMap . pu...