Continue and break statement Statement in C Program.
Hi Everyone ,
Today we will discuss about Continue and break statement in C . Before understanding Continue and break statement in C please follow my below blog for your reference.
https://dheeraj60.blogspot.com/2020/06/while-and-do-while-loop-lop-in-c.html
https://dheeraj60.blogspot.com/2020/06/nested-for-loop-with-simple-pattern.html
https://dheeraj60.blogspot.com/2020/06/loop-concept-in-c-with-examples-dotcmdb.html
Let’s start with Continue Statement in C.
Continue statement is used inside loops. When a continue statement is encountered inside a loop, control jumps to the beginning of the loop for next iteration, skipping the execution statement of loop for the current iteration. Continue statement in C programming works somewhat like the break statement. Instead of termination, it forces the next iteration of the loop to work and skipping the code in between. Break statement will cover later after Continue Statement.
In Loop : for loop, continue statement causes the conditional test and increment part of the loop to execute. And for the while and do...while loops, continue statement causes the program control to pass to the condition.
Syntax of Continue Statement is as below:
Continue;
Let’s understand how we can use continue statement with simple program using while loop and see how the value of variable can skip with using continue statement.
Below is the Program where i want value from 0 to 10 but want to skip any one value by using continue statement.
#include <stdio.h>
#include <stdlib.h>
int main()
{
for (int i=0; i<=10; i++)
{
if (i==2)
{
continue;
}
printf("%d \n", i);
}
return 0;
}
Let’s understand the Program Continue statement is encountered when if (i==2) condition checked here the value of variable i is equals to 2 . So the print statement for condition i==2 will not execute and this statement will be skipped and another value will be displayed in the output apart from 2. In condition you can make changes if you want to skip that particular value.
Below is the output of Above Program in Compiler Code- Block.
If you see the output 2 is not there and all value from 0 to 10 is displayed.
Hope you Understand how continue statement works in program. Let’s take one another example to understand continue statement by using do while to give same condition and can skip any value by giving condition in If statement.
Below is the Program.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i=0;
do
{
if (i==0)
{
i++;
continue;
}
printf("%d \n", i);
i++;
}
while(i<=10);
return 0;
}
Same Program which we write using for loop but here we give condition if (i==0 ) and use same condition by using do while loop so here it will skip 0 value and print the value from 1 to 10 .
Below is the output of above Program in Compiler Code Block.
Hope you are able to understand the use of continue statement , you can try continue statement for other programs which i have written in my previous blog as per your requirement.
Break Statement in C : Break Statement is used to come out of the loop instantly. When you use break statement inside a loop, the control directly comes out of loop and the loop gets terminated. It is used with if statement, whenever used inside loop. In other word we can say break statements are used in the situations when we are not sure about the actual number of iterations for the loop or we want to terminate the loop with some condition. Whereas in continue statement we just skip one condition but through break statement it check the if condition and print that value and loops get terminated after that and only the conditional value will be the output.
Syntax for break statement.
break;
Let’s see the same example which we used in continue statement and it follows the IF condition and after break it will give the output which satisfy the if condition.
Below is the Program.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i =0;
while(i<=10)
{
printf("%d\n", i);
if (i==3)
{
break;
}
i++;
}
printf(" out of the while loop");
return 0;
}
Here it will check the if condition as we given the condition if (i==3) then break statement so it will give the value from 0 to 3 then it will out of the while loop . Not like continue where it can skip 3 and give all the output .
Below is the output of above program in compiler Code – Block.
Output value is 0 to 3 as it prints the value for the the condition if (i==3) then we use break statement so it will get out from the loop . As per requirement we can make changes in condition.
Let’s take one more example by using for loop for break statement where the same program but i want value from 0 to 8 then use break statement to terminate the loop.
Below is the Program.
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
for (i =0; var<=10; i ++)
{
printf("%d\n", i);
if (i==8)
{
break;
}
}
printf("Out of for-loop");
return 0;
}
Here we put the condition if (i==8) after that break statement so it will give the value from 0 to 8 and after that loop gets terminated. So if we are not sure about iteration we can give condition as per our requirement and use break statement to get out from the loop.
Below is the output of above program in Compiler Code – Block.
Here you see the output is 0 to 8 as in condition if (i==8) after that break statement is used.
Now you are able to understand the break statements as well as it’s similar to continue but work differently as in break it work till IF condition and terminate the loop till the condition while in continue it check the condition and skip that portion only and display all the value .
Hope you are able to understand both break and continue statement in C program. Please write some other code and also take reference from my previous blog and write some programs and make use of break and continue statement to control over programs. For any doubts on Programs let me know .We can also use break in switch case statement which i will cover in next blog .
Please read this blog carefully and use this statement in different program as per your requirement. Please feel free to reach me for any questions.
Thanks.
Comments
Post a Comment