-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbreak_continue.cpp
More file actions
58 lines (45 loc) · 1.91 KB
/
Copy pathbreak_continue.cpp
File metadata and controls
58 lines (45 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*
break statement: This statement terminates the smallest enclosing loop (i.e., while, do-while, for loop, or switch statement). Below is the program to illustrate the same:
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "Printing 1 -10 Number \n";
// Now we are going to illustrate use of break statement
for (int i = 1; i < 10; i++)
{
cout << i << " ";
if (i == 5)
break;
}
return 0;
}
// n = 10;
// output -> 1 2 3 4 5
/*
continue statement: This statement skips the rest of the loop statement and starts the next iteration of the loop to take place. Below is the program to illustrate the same:
*/
#include <bits/stdc++.h>
using namespace std;
int main()
{
cout << "Printing 1 -10 Number \n";
// Now we are going to illustrate use of break statement
for (int i = 1; i <= 10; i++)
{
if (i == 5)
continue;
cout << i << " ";
}
return 0;
}
// n = 10;
// output -> 1 2 3 4 6 7 8 9 10
/**************** IMPORTANT *********************/
/*
In C and C++, exit() and break are two different keywords with distinct meanings.
exit() is a function that terminates the entire program and returns an exit code to the operating system. It is typically used to immediately stop the program execution and return control to the operating system. The exit code can be used by the calling process or system to determine whether the program terminated successfully or with an error.
break is a keyword used to break out of a loop or a switch statement. When used within a loop, it causes the program to immediately exit the loop and continue with the next statement outside the loop. When used within a switch statement, it exits the switch and continues with the next statement after the switch.
In summary, exit() is used to terminate the entire program, while break is used to break out of a loop or switch statement.
*/