Jump Statements

Jump statements alter the normal flow of execution by jumping to another part of the program.

  1. break Statement:- Used to exit a loop or switch statement prematurely.

Syntax:

break;

Example:

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

    if (i == 5) break;

    cout << i << ” “;

}

  1. continue Statement:- Skips the rest of the loop body for the current iteration and proceeds with the next iteration.

Syntax:

continue;

Example:

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

    if (i == 3) continue;

    cout << i << ” “;

}

  1. goto Statement:- Transfers control to a labeled part of the program.

Syntax:

goto label;

//…

label:

    // code

 

Example:

int x = 1;

goto skip;

cout << “This will not print.”;

skip:

cout << “Goto executed.”;