Break statement in c
It is used to break the statement in switch statement and also used for existing the statement.Let us know some basic concept regarding to break statement.
1. ) Break statement use to break the statements in case of switch case statement and with if condition statement.
2. ) when condition is false then break statements gets not work. But after that all the statement gets work and executed.
3. )when condition is true then break statement gets work and all the statements gets not executed.
4. )we will understand break statement with help of while loop.
5. )on thing keep in mind when condition is true break statement gets work after that all the statement gets not work and while loop end due to control comes out of while body.
Let us understand with some important examples.
1.)#include<stdio.h>
int main()
{
int num=5;
while(num>0)
{
num--;
if(num==3)
break;
printf("%d",num);
}
return 0;
}
Here in example 1.while loop will be executed up to condition is true otherwise gets not executed.So firstly condition is true and value decremented from 5 to 4. After that again checked condition becomes false in case if condition.And break statement gets not work but all the statements gets executed to print 4 and loop gets started again. Next time condition is checked again in while loop comes true with updated value(4).After that value decremented from 4 to 3.Again condition is checked and comes true and break statement
gets work and all the statements gets not executed just after the break statement.
After that control jumps out of loop body.
Let see another example.
2.)#include<stdio.h>
int main()
{
int num=5;
while(num>0)
{
if(num==3)
break;
printf("%d",num);
num--;
}
return 0;
}
Here in example 2. value printed 54 after that condition is true so break statement gets work and after that all the statements gets not executed.And control jumps out of while body.
Continue statement
1. ) It is used for continuing the loop.Its use is also same but some differences here.
2. ) when condition is false then continue statement does not work but other next statements gets executed.
3. When condition is true continue statement does not work and all the
statements after that gets not executed .
4. But one thing keep in mind all the statements gets not executed after continue statement but loop is not end and loop again stated with updated value.
Let us understand with example :-
1.) #include<stdio.h>
int main()
{
int num=5;
while(num>0)
{
num--;
if(num==3)
continue;
printf("%d",num);
}
return 0;
}
Here in example 1.while loop will be executed up to condition is true otherwise gets not executed.So firstly condition is true and value decremented from 5 to 4. After that again checked condition becomes false in case if condition. And continue statement gets not work but all the statements gets executed to print 4 and loop gets started again. Next time condition is checked again in while loop comes true with updated value(4) . After that value decremented from 4 to 3.Again condition is checked and comes true and break statement gets work and all the statements gets not executed just after the continue statement .But here loop is not end as in case of break statement. And loop is started again with updated value(3). Again condition is true in case while condition and control inter in loop.After that value decremented from 3 to 1 again condition is false in case if condition .Here continue statement gets not work but all the statement executed after continue statement and further program executed in
this way up to condition is false in case while loop.
Let us see another example:-
2.)#include<stdio.h>
int main()
{
int num=5;
while(num>0)
{
if(num==3)
continue;
printf("%d",num);
num--;
}
return 0;
}
In this example due to high level program may be online compiler does not work so student can use best compiler like dev c++,code block etc.
Here in example 2. value printed 54 after that condition is true so continue statement gets work and after that all the statements gets not executed .But loop is not end here in case continue statement as in case break statement.In example 2. updated value becomes check in condition and control enter in loop. But value is not decremented here so value(3) becomes check in if condition.Checked condition is true so continue statement work and all the statement gets not executed. And again loop started and so on condition is always comes true. In this way loop goes infinite times but not print any thing after 54 .
Here in example 2. value printed 54 after that condition is true so continue statement gets work and after that all the statements gets not executed .But loop is not end here in case continue statement as in case break statement.In example 2. updated value becomes check in condition and control enter in loop. But value is not decremented here so value(3) becomes check in if condition.Checked condition is true so continue statement work and all the statement gets not executed. And again loop started and so on condition is always comes true. In this way loop goes infinite times but not print any thing after 54 .
On the basis of above rule and condition consider you answers of these questions.
1.) #include<stdio.h>
int main()
{
int num=5;
while(num>0)
{
num--;
if(num==3)
continue;
printf("%d",num);
num--;
}
return 0;
}
2.)#include <stdio.h>
int main() {
int num = 5;
while (num > 0) {
num--;
if (num == 4)
continue;
printf("%d\n", num);
}
return 0;
return 0;
}
0 Comments
Any query comment in comment box thank you
Emoji