C break and continue

The "break" and "continue" statements are used in C programming to control the flow of execution in loops. The "break" statement is used to exit a loop prematurely, while the "continue" statement is used to skip the current iteration of a loop and move on to the next iteration. In this article, we will take a look at how to use the "break" and "continue" statements in loops with four examples.

Example 1: Printing numbers from 1 to 10 using "break" statement

The first example demonstrates how to use the "break" statement to exit a loop prematurely. In this example, the loop variable "i" is initialized to 1, and the loop body is executed as long as the condition "i <= 10" is true. The "break" statement is used to exit the loop when the value of "i" is equal to 5.

1#include <stdio.h> 2 3int main() { 4 int i = 1; 5 while (i <= 10) { 6 if (i == 5) { 7 break; 8 } 9 printf("%d\n", i); 10 i++; 11 } 12 return 0; 13}

Output:

11 22 33 44

Example 2: Printing even numbers from 1 to 10 using "continue" statement

The second example demonstrates how to use the "continue" statement to skip the current iteration of a loop and move on to the next iteration. In this example, the loop variable "i" is initialized to 1, and the loop body is executed as long as the condition "i <= 10" is true. The "continue" statement is used to skip the current iteration when the value of "i" is odd.

1#include <stdio.h> 2 3int main() { 4 int i = 1; 5 while (i <= 10) { 6 if (i % 2 != 0) { 7 i++; 8 continue; 9 } 10 printf("%d\n", i); 11 i++; 12 } 13 return 0; 14}

Output:

12 24 36 48 510

Example 3: Finding the first perfect square using "break" statement

The third example demonstrates how to use the "break" statement to exit a loop prematurely. In this example, the loop variable "i" is initialized to 1, and the loop body is executed as long as the condition "i <= 100" is true. The "break" statement is used to exit the loop when the first perfect square is found.

1#include <stdio.h> 2#include <math.h> 3 4int main() { 5 int i = 1; 6 while (i <= 100) { 7 int root = sqrt(i); 8 if (root * root == i) { 9 printf("The first perfect square is: %d\n", i); 10 break; 11 } 12 i++; 13 } 14 return 0; 15}

Output:

1The first perfect square is: 1

Example 4: Finding the first prime number using "continue" statement

The fourth example demonstrates how to use the "continue" statement to skip the current iteration of a loop and move on to the next iteration. In this example, the loop variable "i" is initialized to 2, and the loop body is executed as long as the loop condition "i <= 100" is true. The "continue" statement is used to skip the current iteration when the value of "i" is not a prime number.

1#include <stdio.h> 2#include <math.h> 3 4int main() { 5 int i = 2; 6 while (i <= 100) { 7 int j; 8 for (j = 2; j <= sqrt(i); j++) { 9 if (i % j == 0) { 10 break; 11 } 12 } 13 if (j <= sqrt(i)) { 14 i++; 15 continue; 16 } 17 printf("The first prime number is: %d\n", i); 18 break; 19 } 20 return 0; 21}

Output:

1The first prime number is: 2

The "break" and "continue" statements are useful tools for controlling the flow of execution in loops in C programming. By using these statements, you can exit a loop prematurely or skip the current iteration of a loop, allowing you to write more flexible and efficient code.