C tutorial - Explain break statement and Continue Statement in c language?

Programming languages or concepts
0

 Break and Continue Statements in C

 

Introduction:
In C programming, the break and continue statements are used to control the flow of execution within loops. While loops and for loops are commonly used to repeat statements, sometimes we need to exit a loop prematurely or skip certain parts of the loop's code. This is where the break and continue statements come in handy. In this tutorial, we will explore how these statements work and how to use them effectively in C programs.

The Break Statement:

The break statement is used to terminate the execution of a loop immediately, regardless of the loop condition. When encountered, the break statement transfers control to the statement following the loop. The break statement is commonly used inside if...else statements or switch case control structures.

Syntax of the break statement:
  
  
  

break;

Flowchart:

                       ┌─────┐
                        │ Loop   │
                       └──┬──┘
                                │
               if(condition)  break;
                               │
                              ▼
                       ┌── ───   ┐
                       │Next          │
                       │Statement│
                       └── ──   ─┘


Example usage of the break statement:


#include <stdio.h>

int main() {
    int a = 10;
    while (a < 20) {
        printf("Number is %d\n", a);
        a++;
        if (a > 15) {
            break;
        }
    }
    return 0;
}
Output:

Number is 10
Number is 11
Number is 12
Number is 13
Number is 14
Number is 15


Explanation
In this example, we have a while loop that executes as long as the variable `a` is less than 20. Inside the loop, we print the value of `a` and increment it by 1. However, when `a` becomes greater than 15, the break statement is encountered, causing the loop to terminate immediately. As a result, the numbers printed are from 10 to 15.

The Continue Statement:

The continue statement is used to skip the rest of the code within the current iteration of a loop and proceed to the next iteration. It allows you to control which parts of the loop's code are executed for a particular iteration.

Syntax of the continue statement:

continue;


Example usage of the continue statement:

#include <stdio.h>

int main() {
    for (int i = 6; i <= 10; i++) {
        if (i == 8) {
            continue;
        } else {
            printf("%d ", i);
        }
    }
    return 0;
}

Output:

6 7 9 10


Explanation:
In this program, we use a for loop to iterate from 6 to 10. For each iteration, we check if the value of `i` is equal to 8. If it is, the continue statement is executed, skipping the remaining code inside the loop for that iteration. As a result, the number 8 is not printed. For all other values of `i`, the printf statement is executed, printing the numbers 6, 7, 9, and 10.

Conclusion:
The break statement allows us to exit a loop prematurely, while the continue statement lets us skip certain parts of the loop's code and proceed to the next iteration. By using these statements effectively, we can have greater control over the flow of execution in our C programs.:
In C programming, the break and continue statements are used to control the flow of execution within loops. While loops and for loops are commonly used to repeat statements, sometimes we need to exit a loop prematurely or skip certain parts of the loop's code. This is where the break and continue statements come in handy. In this tutorial, we will explore how these statements work and how to use them effectively in C programs.
The Break Statement:
The break statement is used to terminate the execution of a loop immediately, regardless of the loop condition. When encountered, the break statement transfers control to the statement following the loop. The break statement is commonly used inside if...else statements or switch case control structures.
Syntax of the break statement:

break;

Flowchart:

                       ┌─────┐
                       │ Loop│
                       └──┬──┘
                          │
               if(condition)  break;
                          │
                          ▼
                       ┌─────┐
                       │Next │
                       │Statement│
                       └─────┘

Example usage of the break statement:

#include <stdio.h>
int main() {
    int a = 10;
    while (a < 20) {
        printf("Number is %d\n", a);
        a++;
        if (a > 15) {
            break;
        }
    }
    return 0;
}

Output:

Number is 10
Number is 11
Number is 12
Number is 13
Number is 14
Number is 15


Explanation
In this example, we have a while loop that executes as long as the variable `a` is less than 20. Inside the loop, we print the value of `a` and increment it by 1. However, when `a` becomes greater than 15, the break statement is encountered, causing the loop to terminate immediately. As a result, the numbers printed are from 10 to 15.
The Continue Statement:
The continue statement is used to skip the rest of the code within the current iteration of a loop and proceed to the next iteration. It allows you to control which parts of the loop's code are executed for a particular iteration.

Syntax of the continue statement:


continue;

Example usage of the continue statement:

#include <stdio.h>
int main() {
    for (int i = 6; i <= 10; i++) {
        if (i == 8) {
            continue;
        } else {
            printf("%d ", i);
        }
    }
    return 0;
}

Output:

6 7 9 10

Explanation:
In this program, we use a for loop to iterate from 6 to 10. For each iteration, we check if the value of `i` is equal to 8. If it is, the continue statement is executed, skipping the remaining code inside the loop for that iteration. As a result, the number 8 is not printed. For all other values of `i`, the printf statement is executed, printing the numbers 6, 7, 9, and 10.
Conclusion:
The break statement allows us to exit a loop prematurely, while the continue statement lets us skip certain parts of the loop's code and proceed to the next iteration. By using these statements effectively, we can have greater control over the flow of execution in our C programs.

Post a Comment

0Comments

Post a Comment (0)
close