Understanding the break Statement in C
This resource provides a detailed explanation of the break statement in C programming. It covers its usage in both switch-case statements and loops, demonstrating how it can be used to terminate the current loop or switch-case block prematurely.
Break Statement in C
The break
statement in C is used in two different contexts: in switch-case
statements and in loops (while, do-while, and for loops). It terminates the current loop or switch-case block and transfers control to the statement following the terminated statement.
Break Statements in Loops
When used inside a loop, break
causes the loop to terminate immediately. The general syntax is as follows:
while(condition1){
...
if(condition2)
break;
...
}
Example: Break Statement in While Loop
This program checks if a given number is prime or not:
#include <stdio.h>
/*break in while loop*/
int main () {
int i = 2;
int x = 121;
printf("x: %d\n", x);
while (i < x/2){
if (x % i == 0)
break;
i++;
}
if (i >= x/2)
printf("%d is prime", x);
else
printf("%d is not prime", x);
return 0;
}
Output
x: 121
121 is not prime
Break Statements in For Loops
You can use a break
statement inside a for loop as well. The syntax is as follows:
for (init; condition; increment) {
...
if (condition)
break;
...
}
Example: Break Statement in For Loop
This program prints characters from a given string until a vowel is detected:
#include <stdio.h>
#include <string.h>
int main () {
char string[] = "Rhythmic";
int len = strlen(string);
int i;
for (i = 0; i < len; i++){
if (string[i] == 'a' || string[i] == 'e' || string[i] == 'i' || string[i] == 'o' || string[i] == 'u')
break;
printf("%c\n", string[i]);
}
return 0;
}
Output
R
h
y
t
h
m
Example: Break Statement in Nested For Loops
This program lists all prime numbers between 1 and 30:
#include <stdio.h>
int main(){
int i, num, n, flag;
printf("The prime numbers in between the range 1 to 30:\n");
for(num = 2; num <= 30; num++){
flag = 0;
for(i = 2; i <= num/2; i++){
if(num % i == 0){
flag++;
break;
}
}
if(flag == 0)
printf("%d is prime\n",num);
}
return 0;
}
Output
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
Break Statement in Infinite Loops
An infinite loop is rarely created intentionally. However, you may use an infinite loop and break from it when a certain condition is met.
Example: Break Statement in Infinite Loop
This program generates random numbers until a number divisible by 5 is obtained:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
int i, num;
printf ("Program to get the random number from 1 to 100: \n");
srand(time(NULL));
for (; ; ){
num = rand() % 100 + 1; // random number between 1 to 100
printf (" %d\n", num);
if (num%5 == 0)
break;
}
}
Output
Program to get the random number from 1 to 100:
6
56
42
90
Break Statements in Switch Case
In switch-case statements, the break
statement transfers control out of the switch block. Without break
, the program falls through to the subsequent cases, which is not usually desired.
Example: Break Statement in Switch Case
This code prints a greeting message based on the value of the ch
variable:
#include <stdio.h>
int main(){
/* local variable definition */
char ch = 'm';
printf("Time code: %c\n\n", ch);
switch(ch) {
case 'm':
printf("Good Morning \n");
break;
case 'a':
printf("Good Afternoon \n");
break;
case 'e':
printf("Good Evening \n");
break;
default:
printf("Hello");
}
}
Output
Time code: m
Good Morning
Continue Statement in C
The behaviour of the continue
statement in C is somewhat opposite to the break
statement. Instead of forcing the termination of a loop, it forces the next iteration of the loop to take place, skipping the rest of the statements in the current iteration.
What is Continue Statement in C?
The continue
statement is used to skip the execution of the rest of the statement within the loop in the current iteration and transfer it to the next loop iteration. It can be used with all the C language loop constructs (while, do-while, and for).
Continue Statement Syntax
The continue
statement is used as per the following structure:
while (expr){
...
if (condition)
continue;
...
}
Continue Statement with Nested Loops
In the case of nested loops, continue
will continue the next iteration of the nearest loop. The continue
statement is often used with if
statements.
Example: Continue Statement with While Loop
In this program, the loop generates 1 to 10 values of the variable "i". Whenever it is an even number, the next iteration starts, skipping the printf
statement. Only the odd numbers are printed.
#include <stdio.h>
int main(){
int i = 0;
while (i < 10){
i++;
if(i % 2 == 0)
continue;
printf("i: %d\n", i);
}
}
Output
i: 1
i: 3
i: 5
i: 7
i: 9
Example: Continue Statement with For Loop
The following program filters out all the vowels in a string:
#include <stdio.h>
#include <string.h>
int main () {
char string[] = "Welcome to TutorialsArena C Tutorial";
int len = strlen(string);
int i;
printf("Given string: %s\n", string);
printf("after removing the vowels\n");
for (i=0; i<len; i++) {
if (string[i]=='a' || string[i]=='e' || string[i] == 'i' || string[i] == 'o' || string[i] == 'u')
continue;
printf("%c", string[i]);
}
return 0;
}
Output
Given string: Welcome to TutorialsArena C Tutorial
after removing the vowels
Wlcm t TtrlsPnt C Ttrl
Example: Continue Statement with Nested Loops
If a continue
statement appears inside an inner loop, the program control jumps to the beginning of the corresponding loop.
In the example below, there are three for loops one inside the other. These loops are controlled by the variables i
, j
, and k
respectively. The innermost loop skips the printf
statement if k
is equal to either i
or j
, and goes to its next value of k
. The second j
loop executes the continue
when it equals i
. As a result, all the unique combinations of three digits 1, 2 and 3 are displayed.
#include <stdio.h>
int main (){
int i, j, k;
for(i = 1; i <= 3; i++){
for(j = 1; j {= 3; j++){
if (i == j)
continue;
for (k=1; k {= 3; k++){
if (k == j || k == i)
continue;
printf("%d %d %d \n", i,j,k);
}
}
}
return 0;
}
Output
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Example: Removing Spaces Between Words in a String
The following code detects the blank spaces between the words in a string, and prints each word on a different line.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
char string[] = "Welcome to TutorialsArena C Tutorial";
int len = strlen(string);
int i;
printf("Given string: %s\n", string);
for (i = 0; i { len; i++){
if (string[i] == ' '){
printf("\n");
continue;
}
printf("%c", string[i]);
}
return 0;
}
Output
Given string: Welcome to TutorialsArena C Tutorial
Welcome
to
TutorialsArena
C
Tutorial
Example: Finding Prime Factors of a Number
One of the cases where the continue
statement proves very effective is in the problem of writing a program to find prime factors of a given number.
The algorithm of this program works like this:
- The given number is successively divided by numbers starting with 2. If the number is divisible, the given number is reduced to the division, and the resultant number is checked for divisibility with 2 until it is no longer divisible.
- If not by 2, the process is repeated for all the odd numbers starting with 3. The loop runs while the given number reduces to 1.
Here’s the program to find the prime factors:
#include <stdio.h>
int main (){
int n = 64;
int i, m = 2;
printf("Prime factors of %d: \n", n);
while (n > 1){
if (n % m == 0){
n = n/m;
printf("%d ", m);
continue;
}
if (m == 2)
m++;
else
m = m+2;
}
return 0;
}
Output
Prime factors of 64:
2 2 2 2 2 2
Change the number to 45 and then 90. Run the code again. Now you will get the following outputs:
Output for 45
Prime factors of 45:
3 3 5
Output for 90
Prime factors of 90:
2 3 3 5