Search Result
Details here...

Break and Continue in C

Previous

Do-while Loop in C

Next

Switch Case Statement in C


After the concept of loops, now we will learn about break and continue statements.

Break Statement

break is a keyword in C, used to break out of a loop or switch statement. The syntax is:
break;
It is simply used under some conditionals to break out of the loop. It simply terminates the loop, when the statement is encountered.

Did you know?

If a break statement is used in a loop at last, without any conditionals, then it will break out of the loop just after one iteration.

Let's have a look at an example

C

#include <stdio.h>
int main()
{
int i = 0;
while ( 1 ) {
printf("i = %d\n", i);
i ++;
if ( i == 10 ) {
break;
}
}

return 0;
}

Output

i = 0
i = 1
i = 2
i = 3
i = 4
i = 5
i = 6
i = 7
i = 8
i = 9

Info

while(1) is a special loop that runs infinitely. This is because 1 means true, and since the condition is always true, it runs forever.
Wondering how the above loop stops?

Explanation

Initially, the value of i is 0.

For the first time, the condition is checked, which is 1, always true, hence it is a infinite loop. The block is executed, and the last if statement is checked, which when true breaks out of the loop. It happens when i = 10. Hence, even a infinite loop can be broken out of using break statement.

Info

Almost all the applications(GUI), operating system processes, games, etc. run in an infinite loop. But, there will be some conditions that will break out of the loop. Like, the user clicks on a button, or the user enters a value in a text box.

Let's have a look at a better example

C

#include <stdio.h>
int main()
{
int i, marks, sum = 0, flag = 0;
printf("Enter your marks: ");
for ( i = 0; i < 6; i ++ ) {
scanf("%d", &marks);
sum += marks;
if ( marks < 32 ) {
printf("You failed! The total marks is %d", sum);
flag = 1;
break;
}
}
if ( flag == 0 )

printf("You passed! The total marks is %d", sum);
return 0;
}

Output

Enter your marks: 56
72
38
57
45
33
You passed! The total marks is 301

Did you notice?

In the last if statement, no curly braces are used. This is because, only one statement is inside of it. Note that, indentation is not required in the if statement, but it is better to use it for readability.

Explanation

Four integer variables, i, marks, sum and flag, are declared. The value of sum and flag are set to 0. The variable flag is used to store boolean, whether the user has failed or not.

The for loop is used to iterate, and is executed at most 6 times. At the first iteration (i=0), scanf expects the user to enter integer marks. The value of marks is stored in the variable marks. The value of sum is incremented by the value of marks. Now, the value of marks is checked. If the value of marks is less than 32, then the user has failed. The value of flag is set to 1. The loop is terminated, after otput of the message.

If the value of marks is greater than 32, then the user has passed, and the if statement is not executed, but the loop is continued, until the value of i is equal to 6, or the user has failed.

Then the value of sum is printed, only if the value of flag is 0.

Continue Statement

continue is a keyword in C. It is used to skip the current iteration of the loop, and continue with the next iteration. It is similar to break, but it skips the iteration but not break out of the loop. The syntax is as follows:
continue;
It is usually used at the top of the loop, so that the statements below the continue statement are not executed.

Let's have a look at an example

C

#include <stdio.h>
int main()
{
int i;
for( i = 1; i < 10; i ++ ) {
if ( i % 2 != 0 ) {
continue;
}
printf("%d\n", i);
}
return 0;
}

Output

2
4
6
8

Info

The if statement is used to filter only the even numbers. The continue statement is used to skip the odd numbers.

Explanation

An integer variable i is declared. The for loop is executed at most 9 times(here, exactly for 9 times). At the first iteration (i=1), i % 2 won't be equal to 0, means i is a odd number. The below part of the if statement is skipped and the loop is continued.

At the second iteration (i=2), i % 2 will be equal to 0, means i is an even number. The below part of the if statement is executed, and the value of i is printed.

This is continued until the value of i is equal to 10.

Info

In a while loop, if a continue statement is present without the conditionals and before the update of the loop variable, then the loop will be infinite.

Let's have a look at an example of break and continue together

C

#include <stdio.h>
int main()
{
int i, marks, failedIn = 0, passedIn = 0, flag = 0;
printf("Enter the marks: ");
for( i = 0; i < 6; i ++ ) {
scanf("%d", &marks);
if ( marks < 32 ) {
failedIn += 1;
continue;
}
if ( marks > 80 ) {
printf("Marks can't be greater than 80");
flag = 1;
break;
}
passedIn += 1;
}
if ( flag == 0 )
printf("You are passed in %d subjects and failed in %d subjects.", passedIn, failedIn);
return 0;
}

Output

Enter the marks: 32
54
32
45
33
32
You are passed in 6 subjects and failed in 0 subjects.

Woow!

He/she passed in all the subjects. How lucky is he? 😅

By darpan.kattel
Previous

Do-while Loop in C

Next

Switch Case Statement in C