Skip to content

Commit 7c79b4a

Browse files
new file: Chapter_4/10_Break_Continue.c
1 parent a3daf3a commit 7c79b4a

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Chapter_4/10_Break_Continue.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
BREAK STATEMENT:
3+
The break statement is used to exit to loop irrespective of whether the condition is true or not.
4+
Whether a break statement is encountered inside the loop, the control is selt outside the loop.
5+
*/
6+
//example:
7+
// #include <stdio.h>
8+
// int main(){
9+
// for(int i = 0; i <= 10; i++){
10+
// if(i == 6){
11+
// break;
12+
// }
13+
// printf("%d\t", i);
14+
// }
15+
// printf("The loop is ended!");
16+
// return 0;
17+
// }
18+
/*
19+
Output:
20+
0 1 2 3 4 5 The loop is ended!
21+
22+
The above example should have printed all numbers from 0 to 20 but it printed upto because we added a break statement when i become 6. so the loop ended when i became 6.
23+
*/
24+
25+
/*
26+
CONTINUE STATEMENT:
27+
Continue statement is similar to break statement but instead of exiting the loop, it skips the iteration and jumps to the next iteration.
28+
*/
29+
//example:
30+
#include <stdio.h>
31+
int main(){
32+
for(int i = 0; i < 15; i++){
33+
if(i == 5){
34+
continue;
35+
}
36+
printf("%d\t", i);
37+
}
38+
printf("The loop is ended!");
39+
return 0;
40+
}
41+
/*
42+
Output:
43+
0 1 2 3 4 6 7 8 9 10 11 12 13 14 The loop is ended!
44+
*/
45+
/*
46+
Note:
47+
1. Sometimes, the name of the variable might not indecate the behaviour of the program.
48+
2. "Break" statement completely exits the loop.
49+
3. "Continue" statement skips particular iteration
50+
*/

0 commit comments

Comments
 (0)