The break and continue statement in C

break statement

Suppose we are writing a program to search for a particular number among 1000 numbers. In the 10th iteration, we have found the desired number. At this point, we don’t want to transverse the remaining 990 numbers instead we want the loop to terminate and continue with the execution of statement following the loop. This is where the break statement comes into the play.

When break statement is encountered within the loop, the program control immediately breaks out of the loop and resumes execution with the statement following the loop. The break statement is commonly used in conjunction with a condition.

Let’s take an example:

Expected Output:

How it works:

In the 5th iteration value of i becomes 5. The condition (i==5) is checked since it is true. The break statement is executed and the control comes out the for loop to execute the statement following it. Had there been no break statement, this loop would have been executed 9 times.

Let’s create something more useful. The following program determines whether the number entered by the user is prime or not.

Expected Output:

1st run:

2nd run:

How it works:

A number is prime if it is only divisible by 1 and itself. Here is one way to test whether a number is prime or not. Try dividing the number from 2 to one less than the number, if it is found to be divisible by any number within that range, then it is not prime, otherwise it is a prime number.

After looping through all the numbers from 2 to n-1. If the value of the variable flag remains set to 1. Then the number n is prime, otherwise, it is not.

Let’s say in the first run the user entered 5.

In for loop i is initialized to 2. The condition (i < num-1) or (2 < 4) is checked, since it is true, control moves ahead to execute the body of the loop. In the loop body, the if condition num%i == 0 i.e 5%2 == 0 is tested, since it is false. The statement in the body of the if is omitted. Then the value of i is incremented using i++. The condition (i < num-1) i.e 2 < 4 is checked again, since it is still true the body of the for is executed once more. The process repeats until i < num-1. Even after the third and fourth iteration the value of flag remains the same i.e 1. When control comes out of for loop, the if condition i.e flag==1 is checked, since it is true the statement inside the body of the if statement is executed.

Again, Let’s say in the second run the user entered 6.

Everything works exactly as described above. Except in the 2nd iteration when i reaches 3, the if condition (num%i==0) i.e 6%3 == 0 is checked, since it is true. Control moves ahead to execute statement inside the body of the if block. Inside the if block the variable flag is initialized to 1, this means that the number is not prime. The break statement breaks out of the for loop. If condition outside the loop is tested again i.e flag==1, as it is false, the statement in the else block is executed.

 The break Statement inside a Nested Loop

When the break statement is used inside a nested loop then it causes exit only from the innermost loop.

Expected Output:

In this case, when the value of j reaches 3, the condition j == 3 is evaluated to true and break statement causes an exit from the inner for loop (the outer for loop will keep executing) and the program control is transferred to the statement following the loop.

continue statement

The continue statement is used to prematurely end the current iteration and move on the to the next iteration. When the continue statement is encountered in a loop, all the statements after the continue statement are omitted and the loop continues with the next iteration. The continue statement is used in conjunction with a condition.

Sometimes people get confused with between the break and and continue statement. Always remember that the break statement when encountered breaks out of the loop, but when the continue statement is encountered, the loop is not terminated instead the control is passed to the beginning of the loop.

When the continue statement is encountered in the while and do while loop, the control is transferred to the test condition and then the loop continues. whereas in the for loop when continue statement is encountered the control is transferred to the update expression, then the condition is tested.

The following program prints all the numbers between 0 to 10 which are not divisible by 4 using the continue statement.

Expected Output:

How it works:

When the for loop starts the variable i is set to 0, then the condition (i < 10) or (0 < 10) is tested, since it is true, the program control moves ahead to execute the loop body. Inside the loop body, the if condition i % 4 == 0 i.e 0 % 4 == 0 is checked, since it is false, the execution of statement in the if body is skipped. In line 17, we print the value of i, using the printf() statement. There are no more statements to execute in the loop body, so the program control is transferred to the update expression (i++). This completes the first iteration.

In the 5th iteration, the value of i reaches 4. Then the if condition (i % 4 == 0) i.e (4 % 4 == 0) is tested, since it is true, the continue statement is executed, and the control is transferred to the update expression. The expression i++ increments the value of the variable i by 1. Then, the condition i < 10 is tested again. This process continues until i < 10.

Leave a Comment

%d bloggers like this: