break, continue, goto

suggest change

The break instruction:

Using break we can leave a loop even if the condition for its end is not fulfilled. It can be used to end an infinite loop, or to force it to end before its natural end.

The syntax is

break;

Example: break is often used in switch cases. Once a case is selected, execution continues into the following cases unless you stop it.

switch (condition) {
	case 1: block1;
	case 2: block2;
	case 3: block3;
	default: blockdefault;
}

If case 1 is selected, block1 runs and then execution falls through block2, block3, and blockdefault. To run only the matching block, put break at the end of each one:

switch (condition) {
	case 1: block1;
	        break;
	case 2: block2;
	        break;
	case 3: block3;
	        break;
	default: blockdefault;
	        break;
	}

so only one block is processed and control leaves the switch statement.

break also leaves the nearest enclosing while, do, or for loop. It does not leave an if. A break that is not inside a loop or switch is ill-formed.

The continue instruction:

The continue instruction causes the program to skip the rest of the loop in the present iteration as if the end of the statement block would have been reached, causing it to jump to the following iteration.

The syntax is

continue;

Example consider the following :

// :glot
#include <iostream>

auto main() -> int
{
    for (int i = 0; i < 10; ++i) {
        if (i % 2 == 0) {
            continue;
        }
        std::cout << "\n @" << i;
    }
    std::cout << '\n';
}

 @1
 @3
 @5
 @7
 @9

In this code, whenever i % 2 == 0, continue skips the rest of the loop body (the output statement). The for loop then runs its increment and tests the condition again.

The goto instruction:

It allows making an absolute jump to another point in the program. You should use this feature carefully since its execution ignores any type of nesting limitation. The destination point is identified by a label, which is then used as an argument for the goto instruction. A label is made of a valid identifier followed by a colon (:)

The syntax is

goto label;
..
.
label: statement;

Note: Use of goto is highly discouraged because it makes it difficult to trace the control flow of a program, making the program hard to understand and hard to modify.

Example :

// :glot
#include <iostream>

auto main() -> int
{
    int num = 1;
    STEP:
    do {
        if (num % 2 == 0)
        {
            num = num + 1;
            goto STEP;
         }
    
       std::cout << "value of num : " << num << '\n';
       ++num;
    } while (num < 10);
}
value of num : 1
value of num : 3
value of num : 5
value of num : 7
value of num : 9

whenever the condition num % 2 == 0 is satisfied the goto sends the execution control to the beginning of the do-while loop.

The exit function:

exit is a function declared in <cstdlib>. It terminates the program with a status code. Its prototype is:

void exit(int status);

cstdlib defines the standard exit codes EXIT_SUCCESS and EXIT_FAILURE.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents