Break statement in c
In some situations, we need to jump out of a loop without checking the conditional test. The keyword break allows doing this in c programming.
Whenever a break statement is encountered the control jumps out of the loop and executes the next statement after the loop.
The break statement is associated with if statement.
Break statement is also used in switch case to break the cases.
Look at the control flow of break statement: Here I have showed control flow in a while loop.
Syntax of break statement
break;
Example
//Example for break program in c
#include<stdio.h>
int main()
{
int i;
printf(" We stop at 5 \n");
for(i=0;i<=10;i++)
{
printf("%d \n",i);
if(i==5)
break;
}
printf("After the loop \n");
return 0;
}
Output
We stop at 5 0 1 2 3 4 5 After the loop
Code explanation
In the above program we declare one loop variable.
The process is to print the numbers from 0 to 5. At the beginning i value is zero and is less than 10 therefore control jumps inside the loop and prints the value 0.
Then as i value is not 5 it goes to the beginning of the loop and prints the next value.
This process continues until i value becomes 5.
Once i value becomes 5 control jumps out of the loop and executes remaining statements in program.
continue statement in c
In some situations where we have to go to the beginning of the loop without executing statements after a specific statement in that loop. Continue statement will do this.
Whenever a continue statement is encountered the control jumps to the beginning of a loop by skipping that iteration.
Continue is associated with if statement.
We cannot use continue in switch case.
Look at the control flow of continue statement: Here I have showed control flow in a while loop.
Syntax of continue statement
continue;
Let’s look at the example program
Example
//Example for continue program in c
#include<stdio.h>
int main()
{
int i=8;
while(i>=0)
{
if(i==5)
{
i--;
continue;
}
printf("%d \n",i);
i--;
}
return 0;
}
Output
8 7 6 4 3 2 1 0
Code explanation
In the above program we declare one loop variable.
The solution is to print the values from 8 to 0 in descending order.
First i value will be 8 which is true and therefore control enters inside the loop.
As it is not equal to 5 it’s value is printed. Next The process continues until i value becomes 5.
Once i value becomes 5 the counter value is decremented and the process continues.
So in the process the value 5 is skipped.
Difference between break and continue statement in c with example
Break statement terminates the loop. Continue statement skips one iteration.
If the break statement is executed then the loop is terminated but in the case of continue, the loop may execute by skipping that iteration.
The break statement is used not only with loops but also with switch cases but continue statement can only be used with loops.
People also ask for
What are break and continue in c?
The break statement breaks the loop and executes the next statement in the program. Continue statement allows the control to jump to the beginning of the loop.
How do you use break and continue?
We can use break and continue statements within the loops and switch cases.
What is continue statement in c
Continue is the keyword used in continue statement to skip the iteration of loop.
What is the use of continue statement in c
Continue is used to jump to the beginning of a loop by skipping that iteration.
What does continue statement do in c
Continue jumps to the start of a loop by skipping the iteration.
What is break statement in c
Break statement is used to exit the loop.
How to use break statement in c
We can use break by specifying break keyword in a loop and also we use break in switch cases.
What does break statement do in c
Break statement terminates the loop and in of switch case it terminates the case block.
What is the use of break statement in c
Break statement is used to exit the loop and in switch cases it ends the case block.
People also read
- C program Structure
- Structures in c
- Arrays in c
- string in c
- printf in c
- Break and continue YouTube video
- scanf in c