Break and Continue Statements in Java Same as C and C
C break and continue
In this tutorial, we will learn to use break and continue statements with the help of examples.
break statement
In all the C loops we have a way to break out of a loop at any point in time, immediately, regardless of the conditions set fo the loop.
This is done using the break
keyword.
In simple words, The break statement is a loop control statement which is used to terminate the loop immediately.
Basically 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 based on some condition.
Syntax :
break;
Note : The break statement is almost always used with if...else statement inside the loop.
How break statement works?
Example : break statement
// program to use break statement inside for loop
#include <stdio.h>
int main () {
for (int i = 1; i <= 10; i++) {
// break condition
if (i == 5) {
break;
}
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4
In the above example,a for loop is used to print the value of i in each iteration. In which a if statement is used with break statement in it.
The condition of if statment is i == 5 i.e. when i is equal to 5 and break statement is executed to terminate the loop. Hence, the output doesn't include values greater than or equal to 5.
In C programming, break is also used with the switch statement. This will be discussed in the next tutorial.
continue
This is one of the easiest and the most basic keywords in C/C++, which gives programmers control over loops.
The continue exactly as the name suggests. Since we use this in loops, it will skip over the remaining body of the current loop, and continue to the next iteration.
We can use this inside any loops like for
, while
, or do-while
loops.
In Simple words,
1. The continue statement is used to skip the remaining portion of a for/while loop.
2. we continue onto the next iteration, and move to the loop condition check.
3. This is quite useful for programmerss to be flexible with their approach in control loops.
Syntax :
continue;
Note : The continue statement is almost always used with the if...else statement.
How continue statement works?
Example : continue statement
// loop to print numbers 1 to 10 except 4
#include <stdio.h>
int main () {
for (int i = 1; i <= 10; i++) {
// if i is equal to 4 , continue to next iteration without printing 4.
if (i == 4) {
continue;
}
else{
// otherwise print the value of i.
printf("%d ", i);
}
}
return 0;
}
Output
1 2 3 5 6 7 8 9 10
In the above program, we have used the the for loop to print the value of i in each iteration. Here, notice the code,
if (i == 4) {
continue;
}
This means
- When i is equal to 4, the continue statement skips the current iteration and starts the next iteration
- Then, i becomes 5 , and the condition is evaluated again.
- Hence, 5 and 6 are printed in the next two iterations.
- Loop prints the value of i until it became 11 and condition becomes fales. Then, the loop terminates.
- Note: The continue statement is almost always used with decision-making statements.
- Note: The break statement terminates the loop. However, the continue statement only skips the current iteration.
Next Tutorial
We hope that this tutorial helped you develop better understanding of the concept of break & Continue Statementsin C.
Keep Learning : )
In the next tutorial, you'll learn about C Switch-case
Statement.
Source: https://www.algbly.com/Tutorials/C-programming/C-break-continue.html
0 Response to "Break and Continue Statements in Java Same as C and C"
Post a Comment