Break and Continue Statements in C
Introduction:
The break
and continue
statements in C provide control over loop execution. This comprehensive guide aims to provide a thorough understanding of these statements, covering basics, syntax, use cases, and practical applications.
See Also – Pointers in C Programming
Table of Contents:
- Foundations of Break and Continue Statements in C
- Break Statement Definition and Purpose
- Continue Statement Definition and Purpose
- Syntax and Usage
- Break and Continue in Loops
- Using Break in Loops
- Harnessing Continue in Loops
- Practical Examples
- Break and Continue in Switch-Case
- Break in Switch-Case
- Continue in Switch-Case
- Advanced Break and Continue Techniques
- Labeling Loops for Control
- Breaking Out of Nested Loops
- Fine-Tuning Loop Execution with Continue
- Practical Applications of Break and Continue in C
- Loop Termination Conditions
- Menu-Driven Programs with Break
- Filtering and Processing Data with Continue
1. Foundations of Break and Continue Statements in C:
Break Statement Definition and Purpose:
// Break Statement Definition and Purpose
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
break; // Terminate the loop when i is 5
}
printf("%d ", i);
}
Continue Statement Definition and Purpose:
// Continue Statement Definition and Purpose
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the rest of the loop body when i is 5
}
printf("%d ", i);
}
Syntax and Usage:
- The
break
statement is used to exit the loop or switch statement. - The
continue
statement is used to skip the rest of the loop body and proceed with the next iteration.
// Syntax and Usage
while (condition) {
// Code before break or continue
if (someCondition) {
break; // or continue;
}
// Code after break or continue
}
2. Break and Continue in Loops:
Using Break in Loops:
// Using Break in Loops
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
break; // Terminate the loop when i is 5
}
printf("%d ", i);
}
Harnessing Continue in Loops:
// Harnessing Continue in Loops
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
continue; // Skip the rest of the loop body when i is 5
}
printf("%d ", i);
}
Practical Examples:
// Practical Example: Finding Prime Numbers
int num, i, isPrime;
for (num = 2; num <= 50; num++) {
isPrime = 1; // Assume num is prime
for (i = 2; i <= num / 2; i++) {
if (num % i == 0) {
isPrime = 0; // Set isPrime to 0 if num is divisible
break; // No need to check further
}
}
if (isPrime == 1) {
printf("%d is prime\n", num);
}
}
3. Break and Continue in Switch-Case:
Break in Switch-Case:
// Break in Switch-Case
int dayOfWeek = 3;
switch (dayOfWeek) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break; // Exiting the switch statement
// ... additional cases
default:
printf("Invalid day\n");
}
Continue in Switch-Case:
// Continue in Switch-Case
int dayOfWeek = 3;
switch (dayOfWeek) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
continue; // Illegal use of continue in switch-case
// ... additional cases
default:
printf("Invalid day\n");
}
4. Advanced Break and Continue Techniques:
Labeling Loops for Control:
// Labeling Loops for Control
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= 5; j++) {
if (i == 3 && j == 3) {
goto endLoop; // Jump to the end of the outer loop
}
printf("%d%d ", i, j);
}
printf("\n");
}
endLoop:
printf("Loop terminated prematurely.\n");
Breaking Out of Nested Loops:
// Breaking Out of Nested Loops
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= 5; j++) {
if (i == 3 && j == 3) {
goto endLoop; // Jump to the end of the outer loop
}
printf("%d%d ", i, j);
}
printf("\n");
}
endLoop:
printf("Loop terminated prematurely.\n");
Fine-Tuning Loop Execution with Continue:
// Fine-Tuning Loop Execution with Continue
int i, j;
for (i = 1; i <=
5; i++) {
if (i == 3) {
continue; // Skip the rest of the loop body for i=3
}
for (j = 1; j <= 5; j++) {
printf("%d%d ", i, j);
}
printf("\n");
}
5. Practical Applications of Break and Continue in C:
Loop Termination Conditions:
// Loop Termination Conditions
int sum = 0, num;
while (1) {
printf("Enter a number (0 to exit): ");
scanf("%d", &num);
if (num == 0) {
break; // Exit the loop if the user enters 0
}
sum += num;
}
printf("Sum: %d\n", sum);
Menu-Driven Programs with Break:
// Menu-Driven Programs with Break
int choice;
while (1) {
printf("Menu:\n");
printf("1. Option 1\n");
printf("2. Option 2\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Code for Option 1
break;
case 2:
// Code for Option 2
break;
case 3:
// Code for Exit
goto endProgram; // Jump to the end of the program
default:
printf("Invalid choice\n");
}
}
endProgram:
printf("Program exited.\n");
Filtering and Processing Data with Continue:
// Filtering and Processing Data with Continue
int i;
for (i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}
Latest posts by Mahesh Wabale (see all)
- AnchorSetup using Docker-Compose - October 18, 2024
- Devops assignment - October 17, 2024
- Deployment of vault HA using MySQL - September 18, 2024