Control Constructs in C
Control Constructs in C
• 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
• 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 break the next iteration. All iterations of the loop are executed even
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;