CodersEditor Tutorials

CodersEditor Tutorials

  • Online IDE
  • Q&A
  • C Programming
  • Java
  • GraphQL.NET
  • System Software

›Loop

C Programming

    Introduction

    • Overview
    • Installation & Setup
    • C Program Structure
    • Keywords & Identifiers
    • Data Types
    • Variables & Constants
    • Scope Rules
    • Storage Class
    • Standard Input and Output
    • Operators

    Control Flow/Decision Making

    • Overview
    • if Statement
    • if else Statement
    • Nested If statement
    • Switch Statement
    • Nested Switch Statement
    • Conditional Operators

    Loop

    • Loops Overview
    • While loop
    • For Loop
    • Do while
    • Nested Loops
    • Break
    • Continue Statement
    • Goto statement

    Arrays

    • Arrays
    • Multi Dimensional Arrays
    • Arrays & Functions

    Pointers

    • Pointers Basics
    • Pointers and Arrays
    • Pointers and Functions
    • Memory allocation

    Strings

    • String Basics
    • String Functions

    C Programming Examples

    • Basic Example 1

Break statement in C programming

This article is about the break statement in C and the usage of it.

Break statement

The break is a keyword in C programming which is used to bring the control of the program outside of the loop. The break statement is used inside loops or switch statement.

The break statement terminates the loop in a sequence i.e., in the case of nested loops, it terminates the inner loop first and then the outer loops. Break statement is usually used with if statement whenever it comes inside a loop. It terminates the loop or switch statement and executes the statement following the loop or switch.

Syntax

Statements (loop or switch)
break; 

Example– Use of break in a while loop

#include <stdio.h>
int main()
{
        int num =0;
        while(num<=100)
        {
        printf("value of variable num is: %d\n", num);
        if (num==2)
        {
            break;
        }
        num++;
        }
        return 0;
}

Output

value of variable num is: 0
value of variable num is: 1
value of variable num is: 2

Example – Use of break in a for loop

#include <stdio.h>
int main()
{
        int var;
        for (var =100; var>=10; var --)
        {
            printf("var: %d\n", var);
            if (var==99)
            {
                break;
            }
        }
        return 0;
}

Output:

    var: 100
    var: 99

Example – Use of break statement in switch-case

#include <stdio.h>
int main()
{
        int num;
        printf("Enter value of num:");
        scanf("%d",&num);
        switch (num)
        {
            case 1:
                printf("You have entered value 1\n");
                break;
            case 2:
                printf("You have entered value 2\n");
                break;
            case 3:
                printf("You have entered value 3\n");
                break;
            default:
                printf("Input value is other than 1,2 & 3 ");
        }
        return 0;
}

Output:

Enter value of num:1
You have entered value 1
Enter value of num:2
You have entered value 2
Enter value of num:3
You have entered value 3
Enter value of num:4
Input value is other than 1,2 & 3
← Nested LoopsContinue Statement →
  • Break statement
  • Syntax
  • Example– Use of break in a while loop
  • Output
  • Example – Use of break in a for loop
  • Output:
  • Example – Use of break statement in switch-case
  • Output:
CodersEditor Tutorials
CoderdEditor Developer Tools
CodersEditor Online EditorGit Flavoured Markup EditorFree Online JSON FormatterFind My IP Address Details
Tutorials
C ProgrammingJava
Copyright © 2021 CodersEditor.com