Break, Exit, Continue & Goto Statement Break Statement
Break, Exit, Continue & Goto Statement Break Statement
The break statement has two use,you can use it to terminate a case in the switch statement, and you can also use it to force immediate termunation of loop,bypassing the normal loop condition test. Example:- #include<iosttream.h> #include<conio.h> int a ; Cout<<enter the no; Cin>>a; Switch(a) { Case1:cout<<Sunday\n; Break; Case1:cout<<Sunday\n; break; Case2:cout<<monday\n; break; Case3:cout<<tuesday\n; break; Case4:cout<<wednesday\n; break; Case5:cout<<thrusday\n; break; Case6:cout<<friday\n; break; Case7:cout<<Satday\n; break; Default: Cout<<wrong option; } getch(); } Exit statement
Exit is a function defined in the stdlib library means (stdlib.h). The purpose of exit is to terminate the current program with a specific exit code. Its prototype is:
exit (exitcode);
The exitcode is used by some operating systems and may be used by calling programs. By convention, an exit code of 0 means that the program finished normally and any other value means that some error or unexpected results happened Example #include<iuostream.h> #include<conio.h> Void main () { Int n; Cout<<enter the no; Cin>>n; If (n%2==0) { Cout<<it is even no; else Cout<<it is odd no; exit(O); } getch(); }
Continue:The continue statement causes the program to skip the rest of the loop in the current iteration as if the end of the statement block had been reached, causing it to jump to the start of the following iteration. For example, we are going to skip the number 5 in our countdown: Example #include <iostream> #include<conio.h> Void main () {
for (int n=10; n>0; n--) { if (n==5) continue; cout << n << ", "; } cout << "FIRE!\n"; getch(); } Output10, 9, 8, 7, 6, 4, 3, 2, 1, FIRE!
allows to make an absolute jump to another point in the program. You should
Goto statement:
use this feature with caution since its execution causes an unconditional jump ignoring any type of nesting limitations. The destination point is identified by a label, which is then used as an argument for the goto statement. A label is made of a valid identifier followed by a colon (:). Generally speaking, this instruction has no concrete use in structured or object oriented programming aside from those that low-level programming fans may find for it. For example, here is our countdown loop using goto:
// goto loop example #include <iostream> using namespace std; int main () { int n=10; loop: cout << n << ", "; n--; if (n>0) goto loop; cout << "FIRE!\n"; return 0; }
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, FIRE!