[go: up one dir, main page]

0% found this document useful (0 votes)
162 views3 pages

Break, Exit, Continue & Goto Statement Break Statement

The document discusses four control flow statements in C++: 1. The break statement terminates the current loop/switch case and transfers execution to the next statement. 2. The exit function terminates the entire program with a specified exit code. 3. The continue statement skips the rest of the current loop iteration and continues with the next one. 4. The goto statement provides an unconditional jump to a labeled statement, bypassing the normal program flow. It should be used cautiously in structured programming.

Uploaded by

snehachandan91
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
162 views3 pages

Break, Exit, Continue & Goto Statement Break Statement

The document discusses four control flow statements in C++: 1. The break statement terminates the current loop/switch case and transfers execution to the next statement. 2. The exit function terminates the entire program with a specified exit code. 3. The continue statement skips the rest of the current loop iteration and continues with the next one. 4. The goto statement provides an unconditional jump to a labeled statement, bypassing the normal program flow. It should be used cautiously in structured programming.

Uploaded by

snehachandan91
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

BREAK,EXIT,CONTINUE & GOTO STATEMENT Break statement The term break means breaking out of a block of code.

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!

You might also like