- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C break statement
![]() Share with a Friend |
C Programming - C break statement
C break Statement
The break statement in C is used to exit a loop or switch statement prematurely, bypassing the remaining iterations or cases. It can be used inside any loop (such as for, while, or do-while loops) or inside a switch statement to stop execution.
Syntax of break Statement:
C
break;
- When the break statement is executed, control is transferred out of the innermost loop or switch statement in which it is present.
Use Cases of break Statement:
- Exiting a loop: It is often used to exit a loop early when a certain condition is met.
- Exiting a switch statement: The break statement is used to exit a switch case, preventing the execution of subsequent cases.
Example 1: Using break in a for Loop
C
#include <stdio.h>
int main() {
// Use break to exit the loop when i equals 5
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop when i is 5
}
printf("%d ", i); // Print values of i
}
return 0;
}
Explanation:
- The for loop runs from 1 to 10, but when i reaches 5, the break statement is executed.
- The loop exits early, and the program only prints 1 2 3 4.
Output:
1 2 3 4
Example 2: Using break in a while Loop
C
#include <stdio.h>
int main() {
int i = 1;
// Use break to exit the loop when i equals 7
while (i <= 10) {
if (i == 7) {
break; // Exit the loop when i is 7
}
printf("%d ", i); // Print values of i
i++;
}
return 0;
}
Explanation:
- The while loop runs until i is greater than 10, but when i reaches 7, the break statement is executed, terminating the loop early.
Output:
1 2 3 4 5 6
Example 3: Using break in a switch Statement
C
#include <stdio.h>
int main() {
int num = 3;
switch (num) {
case 1:
printf("One\n");
break; // Break prevents the next cases from executing
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
default:
printf("Default case\n");
}
return 0;
}
Explanation:
- The switch statement checks the value of num. When it matches case 3, the corresponding code is executed.
- The break statement ensures that the program does not execute the default case or any cases after the matching one.
Output:
Three
Example 4: Using break in Nested Loops
C
#include <stdio.h>
int main() {
// Use break to exit the inner loop when i equals 3
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
if (j == 3) {
break; // Exit the inner loop when j equals 3
}
printf("%d-%d ", i, j); // Print i and j
}
printf("\n");
}
return 0;
}
Explanation:
- The inner loop runs for each value of i, but when j equals 3, the break statement is executed, causing the inner loop to exit early for that particular iteration of i.
Output:
1-1 1-2
2-1 2-2
3-1 3-2
4-1 4-2
5-1 5-2
Summary of break Statement:
- The break statement is used to exit a loop or switch statement before its condition is fully met or its body is completely executed.
- In loops: It helps in terminating a loop early based on some condition.
- In switch statements: It prevents "fall-through" by exiting the switch after executing the matched case.
- It only breaks out of the innermost loop or switch: In case of nested loops or switch statements, it exits the innermost structure where it is used.
