0% found this document useful (0 votes)
19 views

Control Constructs in C

The document discusses different types of flow control in C including branching with if/else statements and looping with while, do-while, and for loops. It provides examples of how if, if-else, if-else if-else statements work along with explanations of while, do-while loops. The document also distinguishes between entry controlled loops like while and for loops versus exit controlled loops like do-while.

Uploaded by

Anushka Chittora
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Control Constructs in C

The document discusses different types of flow control in C including branching with if/else statements and looping with while, do-while, and for loops. It provides examples of how if, if-else, if-else if-else statements work along with explanations of while, do-while loops. The document also distinguishes between entry controlled loops like while and for loops versus exit controlled loops like do-while.

Uploaded by

Anushka Chittora
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

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.
• If test 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

When user enters 7, the test expression ( number%2 == 0 )


is evaluated to false. Hence, the statement inside the body of
else statement printf("%d is an odd integer"); is executed
and the statement inside the body of if is skipped.
C if…else if…else statement (nested if…else)
if (testExpression1)
• The if...else statement executes {
two different codes depending // statements to be executed if testExpression1 is true
upon whether the test }
expression is true or false. else if(testExpression2)
Sometimes, a choice has to be {
made from more than 2 // statements to be executed if testExpression1 is false and
possibilities. testExpression2 is true
}
• The nested if...else statement else if (testExpression 3)
allows you to check for {
multiple test expressions and // statements to be executed if testExpression1 and
testExpression2 is false and testExpression3 is true
execute different codes for }
more than two conditions. .
.
else
{
// statements to be executed if all test expressions are false
}
C if…else if…else statement (nested if…else)
C if…else if…else statement (nested if…else)
Ternary operator vs. if … else statement

• The ? : operator is just like an if ... else statement.


• ? : is a ternary operator in that it takes three values.
• ? : takes the following form:
if condition is true ? then X return value : else Y value;
C switch … case statement
C switch … case statement
switch (variable or constant)
• The nested if...else statement ​{
allows you to execute a block case constant1:
code among many alternatives. // code to be executed if n is equal to constant1;
If you are checking on the value break;
of a single variable in nested
if...else statement, it is better to case constant2:
use switch statement. // code to be executed if n is equal to constant2;
break;
• The switch statement is often .
faster than nested if...else. .
.
default:
// code to be executed if n doesn't match any constant
}
C switch … case statement
• When a case constant is found that matches the
switch expression, control of the program passes to
the block of code associated with that case.
• In the above pseudocode, suppose the value of n is
equal to constant2. The compiler will execute the
block of code associate with the case statement until
the end of switch block, or until the break statement
is encountered.
• The break statement is used to prevent the code
running into the next case.
• If break statement is not used, all cases after the
correct case is executed.
C switch … case statement
• Few points to remember:
• Switch condition accepts integer as well as character type of variables or values.
• Case values should always be a constant.
• Case doesn’t always need to have order 1, 2, 3 and so on. It can have any integer value
after case keyword. Also, case doesn’t need to be in an ascending order always, you
can specify them in any order as per the need of the program.
• The expression provided in the switch should result in a constant value otherwise it
would not be valid.
• switch(1+2+23), switch(1*2+3%4), switch(a+b+c) are all valid.
• Nesting of switch statements are allowed, which means you can have switch
statements inside another switch. However nested switch statements should be
avoided as it makes program more complex and less readable.
• switch-case do not execute any statements outside these blocks case and default
C switch … case statement
Looping
while loop
• The syntax of a while loop is:
while (testExpression)
{
//codes
}
• where, testExpression checks the condition is true or false
before each loop.
• The while loop evaluates the test expression.
• If the test expression is true (nonzero), codes inside the body of
while loop is evaluated. The test expression is evaluated again.
The process goes on until the test expression is false.
• When the test expression is false, the while loop is terminated.
Counter Controlled Loops
• The type of loops, where the number of the execution is known in advance are
termed by the counter controlled loop.
• That means, in this case, the value of the variable which controls the execution
of the loop is previously known.
• The control variable is known as counter.
• A counter controlled loop is also called definite repetition loop.
• while loop is a counter controlled loop.
Entry Controlled Loops
• The types of loop where the test condition is stated before the body of the loop,
are known as the entry controlled loop.
• So in the case of an entry controlled loop, the condition is tested before the
execution of the loop.
• If the test condition is true, then the loop gets the execution, otherwise not.
• For example, the while loop is an entry controlled loop.
while loop
do while loop
• The do while loop is similar to the while loop with one
important difference. The body of do...while loop is executed
once, before checking the test expression. Hence, the do while
loop is executed at least once.
do
{
// codes
} while (testExpression);
• The code block (loop body) inside the braces is executed once.
• Then, the test expression is evaluated. If the test expression is
true, the loop body is executed again. This process goes on until
the test expression is evaluated to 0 (false).
• When the test expression is false (nonzero), the do...while loop
is terminated.
Sentinel Controlled Loops
• The type of loop where the number of execution of the loop is unknown, is
termed by sentinel controlled loop.
• In this case, the value of the control variable differs within a limitation and the
execution can be terminated at any moment as the value of the variable is not
controlled by the loop.
• The control variable in this case is termed by sentinel variable.
• do while is a sentinel controlled loop.
Exit Controlled Loops
• The types of loop where the test condition is stated at the end of the body of
the loop, are know as the exit controlled loops.
• So, in the case of the exit controlled loops, the body of the loop gets execution
without testing the given condition for the first time. Then the condition is
tested.
• If it comes true, then the loop gets another execution and continues till the
result of the test condition is not false.
• For example, the do statement or the do....while loop is an exit controlled
loop.
do while loop
No. Topics Entry controlled loops Exit controlled loops
01 Test condition Test condition appears at the beginning. Test condition appears at the end.
02 Control variable Control variable is counter variable. Control variable is counter & sentinel variable.
03 Execution Each execution occurs by testing condition. Each execution except the first one occurs by testing
condition.
04 Type of loop while and for loop do while loop
05 Examples ======== ======
sum = 0; do
n = 1; {
while (n <= 10) printf(“Input a number.\n”);
{ scanf("%d", &num);
sum = sum + n*n; }
n = n+ 1; while(num>0);
} ======
========
06 Flowchart
for (initializationStatement; testExpression; updateStatement)
{
for loop }
// codes

• The initialization statement is executed


only once.
• Then, the test expression is evaluated.
If the test expression is false (0), for
loop is terminated. But if the test
expression is true (nonzero), codes
inside the body of for loop is executed
and the update expression is updated.
• This process repeats until the test
expression is false.
• The for loop is commonly used when
the number of iterations is known.
for loop
Various forms of for loop
1) Here instead of num++, I’m using num=num+1 which is nothing but same as
num++.
for (num=10; num<20; num=num+1)
2) Initialization part can be skipped from loop as shown below, the counter
variable is declared before the loop itself.
int num=10;
for (;num<20;num++)
• Must Note: Although we can skip initialization part but semicolon (;) before
condition is must, without which you will get compilation error.
Various forms of for loop
3) Like initialization, you can also skip the increment part as we did below. In
this case semicolon (;) is must, after condition logic. The increment part is being
done in for loop body itself.
for (num=10; num<20; )
{
//Code
num++;
}
Various forms of for loop
4) Below case is also possible, increment in body and initialization during
declaration of counter variable.
int num=10;
for (;num<20;)
{
//Statements
num++;
}
Various forms of for loop
5) Counter can be decremented also, In the below example the variable gets decremented
each time the loop runs until the condition num>10 becomes false.
for(num=20; num>10; num--)
6) Infinite loop
for(; ;) { }
A loop becomes an infinite loop if a condition never becomes false.
When the conditional expression is absent, it is assumed to be true. You may have an
initialization and increment expression.
while(1) { }  also a kind of infinite loop.
In while and do while, if increment/decrement operation is missing, then also, it’s an infinite
loop.
Can use break statement to terminate an infinite loop.
Nested for loop
Multiple initializations in for loop
break statement
• It is sometimes desirable to skip some
statements inside the loop or terminate the
loop immediately without checking the test
expression.
• In such cases, break and continue
statements are used.
• The break statement terminates the loop
(for, while and do...while loop) immediately
when it is encountered. The break statement
is used with decision making statement such
as if...else.
• break statement is also used
with switch...case statement.

Syntax of break statement


break;
break statement
break statement

Variable a value keeps on getting


incremented and when it reaches 15, if
condition gets satisfied and break
statement would get executed. This leads
to termination of while loop and hence
value of a will get printed only till 14.
continue statement
• The continue statement skips some
statements inside the loop. The continue
statement is used with decision making
statement such as if...else.

Syntax of continue statement


continue;
continue statement
continue statement

Why value 15 is missing in output?

Variable a value keeps on getting


incremented and when it reaches 15, if
condition gets satisfied and continue
statement would get executed. This leads
to skipping of the current iteration when
value of a is 15, and while loop is again
executed with next value of a which is 16.
break vs. continue statement
break continue
A break can appear in both switch and loop (for, while, do) A continue can appear only in loop (for, while, do) statements.
statements.

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;

• The label is an identifier. When goto statement


is encountered, control of the program jumps
to label: and starts executing the code.
goto statement
About goto statement
• goto statement allows you to do bad stuff such as jump out of scope.
• 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.
ANY QUESTIONS???

You might also like