fbpx

Break and Continue in Loops: Enhancing C++ Control Flow

Break and Continue in Loop


INTRODUCTION

Jump statements in C++ are powerful tools that control the flow of execution in a program. These statements—breakcontinuegoto, and return—allow developers to manage loops, conditionals, and function behaviors more efficiently.

  • Break is used to exit loops or switch cases prematurely.
  • Continue skips the current iteration of a loop and moves to the next.
  • Goto enables an unconditional jump to a labeled statement, though it’s rarely used due to readability concerns.
  • Return exits a function, optionally returning a value to its caller.

By mastering these statements, you can write cleaner and more controlled C++ programs!

Jump statements :

  1. break

2.continue

3.goto

4.return

Break statement:

The break statement is used to exit a loop or a switch statement prematurely, i.e., to terminate the current iteration and stop the loop completely.

  • In Loops: When encountered inside a loop (for, while, or do-while), it causes the loop to terminate immediately and control to move to the next statement after the loop.
  • In switch Statements: It is used to exit the switch block after executing the relevant case.

Break in loops:

——————–

#include <iostream>

using namespace std;

 

int main() {

   for()

        if (m == 3) {

            break; 

        }

        cout << m<< ” “;

    }

    cout << “Loop terminated\n”;

    return 0;

}

Break in switch statement:

———————————–

#include <stdio.h>

int main() {

    printf(“Enter the choice”);

    scanf(“%d”,&num);

    switch(num)

    {

    case 1:

        cout<<”iam in case”;

        break;

    case 2:

        cout<<”iam in case2″;

        break;

    default:

        cout<<“iam  default”;

 

    }

}

Continue Statement

The continue statement is used to skip the current iteration of a loop and move to the next iteration of the loop immediately. It only affects the loop in which it is used.

  • In a for or while loop, when continue is executed, the program skips the remaining part of the loop body for the current iteration and moves to the next iteration.

Example2:

#include <stdio.h>

int main() {

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

        if (i == 3) {

           continue;  // Exit the loop when i is 3

        }

       cout<< i;

    }

    cout<<“Loop terminated\n”;

    return 0;

}

 

goto statement:

  • Goto statements unconditional jump from one part to another part.
  • It is also Sphagetti code:

 

Example:

#include<iostream>

int main()

{

int x=0;

 

if(x<5)

{

goto label;

}

    Cout<<”this printf is not printed”;

label:

cout<<”iies”;

}

 

Return statement:

  • It is going to exit the loops:
  • The non void is going to return back the control to the caller
  • The void function does not return back the control ,instead it terminates the function.

 

#include <iostream>

using namespace std;

 

int add(int a, int b) {

    return a + b;  // Return the sum of a and b

}

 

int main() {

    int result = add(3, 4);

    cout << “Sum: ” << result << endl;

    return 0;

}