break and continue Statements


In C programming, we use the break and continue statements to control the flow of execution in loops (such as for, while, and do-while) and in switch-case statements.
These statements allow you to terminate the current iteration of a loop or skip the remaining code in the current iteration and proceed to the next one.

break Statement

The break statement is a control flow statement that terminates the execution of a loop (i.e., a for, while, or do-while loop) or a switch statement.When the program encounters a break statement inside a loop or switch statement, it immediately exits that loop or switch statement and continues executing the next statement outside of the loop or switch statement.

The general syntax of the “break” statement is:

Syntax

break;


For instance:

#include <stdio.h>
int main() {
    for (int i = 1; i <= 10; i++) {
        if (i == 4) {
            break; // Exit the loop when i is equal to 4
        }

        printf("%d ", i);
    }

    return 0;
}


Here’s how it the for break works:

In this instance, it uses the break statement within a for loop to exit the loop when the loop variable i equals 4.

  1. The for loop is initialized with the variable i set to 1. The loop condition is i <= 10, and the loop will continue as long as this condition is true. After each iteration, the loop variable i increments by 1 (i++).
  2. Inside the loop, there is an if statement that checks if i is equal to 4 (i == 4). If the condition holds true, the break statement executes.
  3. The break statement causes the program to immediately exit the loop, regardless of whether the loop condition (i <= 10) still holds true. When i becomes 4, the break statement executes and prematurely terminates the loop.
  4. After exiting the loop, the program proceeds to the next statement, which is the return 0; statement. This ends the main() function, and the program terminates.

The code will print numbers from 1 to 3 because it executes the printf(“%d”, i); statement before reaching the break statement when i is less than 4. However, it won’t print the number 4 as the loop terminates before reaching that iteration.

So, the output of this code will be:

Output

1 2 3

continue Statement

The continue statement in C programming allows you to skip the current iteration of a loop (for, while, or do-while) and proceed to the next iteration. When the program encounters a continue statement inside a loop, it immediately jumps to the next iteration of the loop, bypassing any remaining statements in the current iteration.

The general syntax of the “continue” statement is:

Syntax

continue;


For instance:

#include <stdio.h>
int main() {
    for (int i = 1; i <= 5; i++) {
        if (i == 3) {
            continue; // Skip the current iteration when i is equal to 3
        }
        
        printf("%d ", i);
    }

    return 0;
}


Here’s how it the for continue works:

In this instance, we use the continue statement within a for loop to skip the remaining part of the current iteration whenever the loop variable “i” equals 3.

  1. The for loop is initialized with the loop variable i set to 1. The loop will continue as long as i is less than or equal to 5. After each iteration, the loop variable i is incremented by 1 (i++).
  2. Inside the loop, there is an if statement that checks if i is equal to 3 (i == 3).
  3. When the value of i is 3, the continue statement is executed. This causes the program to skip the remaining statements inside the loop for that particular iteration and immediately proceed to the next iteration of the loop.
  4. The value 3 skips the printf(“%d “, i); statement, but it executes and prints the value of i for all other values (1, 2, 4, and 5)..
  5. The loop continues to iterate until i becomes 6, which violates the loop condition i <= 5, and the loop terminates.

Output

1 2 4 5

The given code uses the “continue” statement to skip the iteration when the number is equal to 3. This results in printing numbers 1, 2, 4, and 5 while excluding 3 from the output.


Both break and continue are powerful control flow statements that allow you to efficiently manage the flow of your program within loops and switch-case statements based on certain conditions.