Control Flow In C Language with Examples

Control Flow

Conrol flow statement is also known as jump statement in C programming.

Statements or loops performs a set of operations continually until the control variable will not sarisfy the condition. but if we want to break the loop when condition will satisfy then C give a permission to jump from one statement to end of loop or beginning of loop as well as jump out of a loop.

Type of Jump Statements:

There are basically three type of Jump Statements:

  • Break Statement
  • Continue Statement
  • Goto Statement

Break Statement

Break statement is used to terminate from a loop while the test condition is true.

This statement can be used with in a while do while and for statement.

When used within a switch-case statement, control is transferred to the end of the construct. Wherease, in looping constructs, control is transferred to the next statement after the construct thereby providing an early exit from the loop.

Example:



Outpu:

 Statement 1.
 Statement 2.
 Statement 3.
 Statement 4.
 End of Program.


Continue Statement

The continue statement is used to skip the remaining statement in the loop.

The loop does not terminate when a continue statement is encountered. Infact, the remaining statement in the loop are skipped and control is transferred to the next pass though the loop.

The continue statement can be included only in a while, do-while or for statement. It is simly written as continue; .

In the for statement, continue transfers control to the increment part of the loop, which is evaluated.

In the while and do-while statement, the test part is executed immediately. The continue statement applies only to loops, not to switches.

Example:



Output:

 Statement 1.
 Statement 2.
 Statemnet 3.
 Statement 4.
 Statement 6.
 Statement 7.
 Statement 8.
 Statement 9.
 Statement 10.
 End of Program.


goto Statement

The C language provides a method to transfer control to a labeled point in the program.

The goto statement is used to alter the normal sequence of program execution by transferring control to some other part of the program.

The goto statement requires a destination label, declared as label: . Where, label is an identifier word followed by a semicolon(:) .

The goto statemet is called by using goto statement-label.

Syntax:

goto label;
- - - - - - - - - -
- - - - - - - - - -
label:
- - - - - - - - - -
- - - - - - - - - -

Example:



Output:

 Statement 1.
 Statement 2.
 Statement 3.
 End of Program.