×

C Tutorial

C Basics

C Data Types

C Input/Output

C Operators

C Conditional Statements

C Control Statements

C Strings

C Functions

C Arrays

C Structure and Unions

C Pointers

C Preprocessor Directives

C Command-line Arguments

C File Handlings

C Graphics

C Advance Topics

C Tips and Tricks

C Important Topics

C Practice

Use of 'break' and 'continue' within the loop in c language

Here, we will learn about break and continue along with their use within the various loops in c programming language.

C 'break' statement

The break is a statement which is used to break (terminate) the loop execution and program's control reaches to the next statement written after the loop body.

Example

Let's consider the following situation

If there is an infinite loop and the input values are integer (we are reading integer numbers), as any input is negative or zero loop body should be terminated.

#include <stdio.h>

int main()
{
    int number;

    /*infinite loop*/
    while (1) {
        printf("Enter integer number: ");
        scanf("%d", &number);
        if (number < 0 || number == 0) {
            printf("Terminating loop...\n");
            break;
        }
        /*print the number*/
        printf("Number is: %d\n", number);
    }

    printf("Bye, Bye...\n");

    return 0;
}

Output

Enter integer number: 10
Number is: 10 
Enter integer number: 20
Number is: 20 
Enter integer number: 30
Number is: 30 
Enter integer number: -5
Terminating loop... 
Bye, Bye...

In the above example, numbers are printing after input but as we input -5, loop body terminated and program's control moves to next statement written after the loop body, which willprint "Bye, Bye..."

C 'continue' statement

The continue is a statement, which is used to move program's control at the starting of the loop body.

There are three steps of a loop execution

  1. Initialization of loop counter
  2. Condition check
  3. Body [with loop counter increment]

When a continue statement found within the loop body, program's control moves to the second step and executes loop again until the condition is not false.

Let's consider the following situation

Example

If we want to take input of 5 positive integer numbers, we don't want to read and print the negative numbers or zeros; if any input is negative number or zero program's should read the number again.

#include <stdio.h>

int main()
{
    int number;
    int counter = 0;

    /*infinite loop*/
    while (counter != 5) {
        printf("Enter integer number: ");
        scanf("%d", &number);
        if (number < 0 || number == 0) {
            printf("Invalid input...\n");
            continue;
        }
        /*print the number*/
        printf("Number is: %d\n", number);
        counter++;
    }

    printf("Bye, Bye...\n");

    return 0;
}

Output

Enter integer number: 10
Number is: 10 
Enter integer number: 20
Number is: 20 
Enter integer number: 30
Number is: 30 
Enter integer number: -5
Invalid input...
Enter integer number: 40
Number is: 40 
Enter integer number: 0 
Invalid input...
Enter integer number: 50
Number is: 50 
Bye, Bye... 

In the above example, total inputs are 7 (there are two invalid inputs -5 and 0), two inputs are not considered as valid input, as program found -5 and 0, program's execution moved to staring of the loop body again due to continue and we got the valid 5 input (5 positive integer numbers)

Usages of both 'break' and 'continue' within the loop

Let's consider the following situation

Example

We want to read N positive integer numbers; if any input is zero, program should take the input again but if any input is negative loo body should be terminated.

#include <stdio.h>

int main()
{
    int number;

    /*infinite loop*/
    while (1) {
        printf("Enter integer number: ");
        scanf("%d", &number);
        if (number == 0) {
            printf("Invalid input...\n");
            continue;
        }
        else if (number < 0) {
            printf("Terminating loop...\n");
            break;
        }
        /*print the number*/
        printf("Number is: %d\n", number);
    }

    printf("Bye, Bye...\n");

    return 0;
}

Output

Enter integer number: 10
Number is: 10 
Enter integer number: 20
Number is: 20 
Enter integer number: 0 
Invalid input...
Enter integer number: 30
Number is: 30 
Enter integer number: -1
Terminating loop... 
Bye, Bye... 

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.