Computer Science: Control Statements: Jump Statement: Goto Statement

Get top class preparation for IAS/Mains/Optional Public-Administration right from your home: Get detailed illustrated notes covering entire syllabus: point-by-point for high retention.

Download PDF of This Page (Size: 276K)

Image of Jump statement

Image of Jump Statement

for (initialization; decision; increment/decrement)

{

Statement(s).

}

Flowchart with decision box

Flowchart with Decision Box

The flow diagram indicates that in for loop three operations take place:

(i) Initialization of loop control variable

(ii) Testing of loop control variable

(iii) Update the loop control variable either by incrementing or decrementing.

  • Operation (i) is used to initialize the value. On the other hand, operation (ii) is used to test whether the condition is true or false.

  • If the condition is true, the program executes the body of the loop and then the value of loop control variable is updated.

  • Again, it checks the condition and so on. If the condition is true, it gets out of the loop.

Example

for (int i = 0; i < 5; i + +)

cout << i.

The output of the above program is

0 1 2 3 4

for (char i = ‘A’; i < ‘E’; i + +)

cout < < i.

The output of the above program is

A B C D

Jump Statements

The jump statements unconditionally transfer program control within a function.

Image of Jump Statements

Image of Jump Statements

Goto Statement

Syntax of goto statement

goto pgr;

|

|

|

pgr:

pgr is known as label. It is a user defined identifier. After the execution of goto statement, the control transfers to the line after label pgr.

Break statement

  • Syntax of break statement

  • The break statement can be used in a switch statement and in any of the loops. It causes program execution to pass to the next statement following the switch or the loop.

Continue statement

  • The continue statement is used in loops and causes a program to skip the rest of the body of the loop.

  • The continue statement skips rest of the loop body and starts a new iteration.

  • exit () function

  • The execution of a program can be stopped at any point with exit () and a status code can be informed to the calling program.

  • The general format is exit (code); where code is an integer value.

  • The code has a value 0 for correct execution. The value of the code varies depending upon the operating system. It requires a process.h header file.

Developed by: