You are on page 1of 4

Abstract

The break statement is used at the end of the cases of a switch statement to avoid flow of
control falling through to the next case. It can also, but should not, be used inside the bodies of
iterations.
Usage
The break statement is used to escape from an iteration loop or a switch statement.
Syntax
Syntax Diagrams

BNF
break-statement
::= 'break' ';'

EBNF
break-statement
::= 'break' ';'
Semantics
break
The break statement skips the remaining statements in the body of a loop, and continues
with the statement following the loop.
The break statement skips the remaining statements in the body of a loop, and continues
with the statement following the loop.
Flowchart

Remarks
• The break statement can only be used within the body of a while, do, for,
or switch statement.
• image`images/break.gif`Break Statement Flowchart`216`332
• The break statement should normally be used at the end of each case statement is a
switch statement.
• In other contexts, the break statement is never necessary; it can always be elimitated by
rearranging the code.
• Use of the break statement except in the context of a switch statement violates the rules
of structured programming.

Example break;
#include <iostream.h> case 1:
#include <stdlib.h> cout << "only one.\n";
break;
int main () { case 2:
unsigned Count; cout << "a pair.\n";
unsigned x; break;
case 3:
Count = rand () % 7; cout << "three.\n";
cout << "The count is " << Count << break;
". \nThis is "; default:
switch (Count) { cout << "many.\n";
case 0: break;
cout << "none.\n"; }
cout << x << " is an odd number.\n";
x = rand () % 5; }
cout << "The test number is " << x << ".\n";
while (x < 10) { return 0;
++x; }
if (x % 2 == 0) {
break;
}
Output

The count is 4.
This is many.
The test number is 2.
3 is an odd number.

Usage
The continue statement is used to skip the rest of an iteration loop.
Syntax
Syntax Diagrams

BNF
continue-statement
::= 'continue' ';'

EBNF
continue-statement
::= 'continue' ';'
Flowchart
Remarks
• The continue statement skips the remaining statements in the body of the loop, and
continues with the next iteration of the loop.
• The continue statement is never necessary; it can always be eliminated by rearranging
the code.
• Use of the continue statement violates the rules of structured programming.

Example cout << x << " is an odd number." << endl;


#include <iostream.h> }
}
int main () {
Output
int x;
1 is an odd number.
x = 0;
3 is an odd number.
while (x < 10) {
5 is an odd number.
++x;
7 is an odd number.
if (x % 2 == 0) {
9 is an odd number.
continue;
}

Purpose
The goto statement is used to unconditioally transfer control to a labelled statement.
Syntax
BNF
<goto-statement> ::=
goto <label> ;
EBNF
<goto-statement> ::=
goto <label> ;
Semantics
• The goto statement transfers control to the labeled statement whose label matches
the identifier.
• The goto statement can only be used within the body of a function definition.
Remarks
• The goto statement is never necessary; it can always be elimitated by rearranging the
code.
• Use of the goto statement violates the rules of structured programming.

Example }
#include <iostream.h> next:
#include <stdlib.h>
x = rand () % 5;
int main () { cout << "The test number is " << x << ".\n";
unsigned Count; while (x < 10) {
unsigned x; ++x;
if (x % 2 == 0) {
Count = rand () % 7; goto done;
cout << "The count is " << Count << }
". \nThis is "; cout << x << " is an odd number.\n";
switch (Count) { }
case 0: done:
cout << "none.\n";
goto next; return 0;
case 1: }
cout << "only one.\n";
goto next;
Output
case 2:
The count is 4.
cout << "a pair.\n";
This is many.
goto next;
The test number is 2.
case 3:
3 is an odd number.
cout << "three.\n";
goto next;
default:
cout << "many.\n";
goto next;
Syntax
Syntax Diagrams

BNF
return-statement
::= 'return' <expression> ';'

EBNF
return-statement
::= 'return' <expression> ';'
Contextual Constraints
expression
The type of the expression should be the same as the type of the function.
In a void function, the expression must be null.
Semantics
return
The return statement returns control to the expression which called the function, along
with the value of the expression following the return keyword.
The return statement should be used with an expression in non-void functions.
The type of the expression should be the same as the type of the enclosing function.
The return statement can be used with an expression in void functions, but it is not
required. Control returns to the caller when the end of the function body is reached.
Remarks
• The return statement should be the last statement in a function body.
• Use of more than one return statement in a function statement violates the rules of
structured programming.

You might also like