Control Constructs
Control Constructs
CONSTRUCTS IN ‘C’
LANGUAGE
Introduction
• C provides two styles of flow control:
• Branching
• Looping
• Branching is deciding what actions to take and looping is deciding how many times to take a certain
action.
• Branching constructs:
• if
• if … else
• if … else if … else (nested if … else)
• Looping constructs:
• while
• do while
• for
• Loop control statements:
• break
• continue
• goto
Branching
C if statement
• The if statement evaluates the test expression inside
parenthesis.
• If test expression is evaluated to true (nonzero),
statements inside the body of if is executed.
• Iftest expression is evaluated to false (0), statements
inside the body of if is skipped.
if (testExpression)
{
// statements
}
C if statement
• When user enters -2, the test expression (number < 0) becomes true. Hence, You entered -2
is displayed on the screen.
• When user enters 5, the test expression (number < 0) becomes false and the statement
inside the body of if is skipped.
C if…else statement
• The if...else statement executes some code if the test
expression is true (nonzero) and some other code if the test
expression is false (0).
• If test expression is true, codes inside the body of if
statement is executed and, codes inside the body of else
statement is skipped.
• If test expression is false, codes inside the body of else
statement is executed and, codes inside the body of if
statement is skipped.
if (testExpression) {
// codes inside the body of if
}
else {
// codes inside the body of else
}
C if…else statement
A break causes the switch or loop statements to terminate the A continue doesn't terminate the loop, it causes the loop to go to
moment it is executed. Loop or switch ends abruptly when the next iteration. All iterations of the loop are executed even
break is encountered. if continue is encountered. The continue statement is used to
skip statements in the loop that appear after the continue.
When a break statement is encountered, it terminates the block When a continue statement is encountered, it gets the control to
and gets the control out of the switch or loop. the next iteration of the loop.
A break causes the innermost enclosing loop or switch to be A continue inside a loop nested within a switch causes the next
exited immediately. loop iteration.
goto statement
goto statement
• The goto statement is used to alter the normal
sequence of a C program.
Syntax of goto statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
• goto statement can be useful sometimes. For example: to break from nested
loops.
About goto statement
• Can be used as another form of
continue as well.
THANK YOU
ANY QUESTIONS???