break statement in C Programming

Given Diagram is the flowchart how break statement works in loops.
The Given flow chart is the break statement used inside a loops.
The flow chart simply shows the working of break statement.
1. At the start of the loop it checks for the perticular condition. If the tested condition is true then loop continues.
2. The condition for the break statement is tested inside the loop body. if the condition is satisfied then the control exit from the loop and stops the execution of the loop.
3. if The condition for the break statement tested inside the loop body is false , the control continue for the execution of statements inside the loop body. i.e. " code inside loop after break"
4. The loop continue execution till the condition is satisfied.
Note:
i. break terminates a loop and can not start over again.
ii. code below the break statement only executes when the continue statement is not encountered inside the loop body.



break in C Programming Loops:

1. break in for loop:
The working of the break statement in for loop is as follows:
STEP 1: The Control enters in to the for loop.Control jumps to initilization section (init)
STEP 2: The Control Jumps to Condition Section and test the condition.
The statements inside the loop body starts execution after the loop condition is evaluated to true.
STEP 3: the if (condition) is tested and will be evaluated.
STEP 4a: If the condition is false, the normal execution will continue.
Executes the code // Code inside loop
STEP 4b: If the condition is true, the program loop terminates,stops the execution and control comes out of the loop.
STEP 5: Steps 2 to 4 will repeats till the end of the loop.

2. break in while loop:
The working of the break statement in while loop is as follows:
STEP 1: The Control enters in to the while loop and test the condition ,if the condition tested is true then the Control enters in to the while loops body.
STEP 2: The loop’s execution starts after the loop condition is evaluated to be true.
STEP 3: The condition of the break statement (if (condition)) will be evaluated.
STEP 4a: If the condition is false( if(condition)),the break statement won't be executed but executes the the statemennt "// Code inside loop".
STEP 4b: If the condition is true, the loop terminates (end) and the program control will go outside the loop.
STEP 5: Steps 1 to 4 will repeats till the end of the loop or condition given in the loop is true.


C program to illustrate use of break in for loop

/* C program given below demonstrate the use of the break statement in for loop */
#include <stdio.h>
int main()
{
int i;
for (i = 0; i < 10; i++)
{
if (i == 5)
{
break;
}
printf("%d, ", i);
}
return 0;
}


Output:
0 1 2 3 4 
Explanation : The program above displays the numbers from 0 to 4.
Step 1: int i;
The variable i is declared as integer to store integer numbers.
Step 2: The control enters into the for loop.
for (i = 1; i < 10; i++)
{
if (i == 5)
{
break;
}
printf("%d, ", i);
}
Explanation:
  2.1 : intilize the variable i to 1 in intilization section of for loop. .i.e i=1
  2.2 : control jumps to test the condition in the condition section , i<10 the condition tested is true i.e. 1<10 results in true.
  a. if the condition tested if(i==5) evaluates to true, then the break statement inside if() terminates the loop and control comes out of the loop and stops the execution.
 b. if the condition tested if(i==5) evaluates false then the break statement inside if() does not executes and executes the printf("%d, ",i);
Step 3: The process repeates till the value of i<10, And as soon as the value of i becomes 10( or the condition for the break is not triggered) the execution of the loop terminates and stops the execution.
And finally shows the final output.



C program to illustrate use of break in while loop

/* C program given below demonstrate the use of the break statement in while loop */
#include <stdio.h>
int main()
{
int count = 5;
while (count >= 0)
{
if (count == 2)
{
count--;
// exit the loop if count is equal to 2.
break;
}
printf("%d ", count);
count--;
}
return 0;
}


Output:
5 4 3 
The program above displays number in reverse order between range 0 to 5

Explanation : i. int count = 5;
The variable counter count is declared as int variable and assigned value 5. i.e count=5.
ii. control enters in to the while loop.
while (count >= 0)
test the condition expression which true (while(5>=0)) then control enters into the while loop body.
iii. And executes
if (count == 2)
{
count--;
// break the loop if it is equal to 2.
break;
}
Here condition if(count==2) tested is false( if(5==2)) so statements inside if dose not execute.
And start executing the rest of statement inside body of loop i.e.
iv.printf("%d ", count);
count--;
displays the value of count i.e.  5 on the output screen and decrement(count--) the value of count by 1 then count becomes 4 , count=4.
This process continues till the value of count=3.
when the value of count becomes 2 (count=2)then
the statement
if (count == 2)
{
count--;
// exit loop if the count is equal to 2.
break;
}
executes.
the if(count==2) is true (if(2==2)) then the value of count decremented by 1(count--)
the break statement terminates the loop and control come out of the loop.
v. Otherwise the statements
printf("%d ", count);
count--;
will execute till the condition while(count>=0) is true.