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

Est 102 Control Flow

The document discusses various control statements in C including selection statements (if, if-else, switch), iteration statements (for, while, do-while), and jump statements (goto, break, continue, return). It provides examples of using if/if-else statements to evaluate conditions and execute code blocks based on the results. It also covers logical and relational operators used in conditional expressions. Flowcharts illustrate the logic flow of if/if-else/switch statements.

Uploaded by

Anugraha K.R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Est 102 Control Flow

The document discusses various control statements in C including selection statements (if, if-else, switch), iteration statements (for, while, do-while), and jump statements (goto, break, continue, return). It provides examples of using if/if-else statements to evaluate conditions and execute code blocks based on the results. It also covers logical and relational operators used in conditional expressions. Flowcharts illustrate the logic flow of if/if-else/switch statements.

Uploaded by

Anugraha K.R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Chapter 2

CONTROL STATEMENTS

Mrs. Anugraha K.R


Assistant Professor
Department of CSE
ICCS CEM
1. LEARNING OBJECTIVES

 Understanding meaning of a statement and statement block

 Learn about decision type control constructs in C and the way


these are used

 Learn about looping type control constructs in C and the


technique of putting them to use

 Learn the use of special control constructs such as goto,


break, continue, and return

 Learn about nested loops and their utility


2. CONTROL STATEMENTS INCLUDE

Selection Iteration Jump


Statements Statements Statements
• if • for • goto

• if-else • while • break

• switch • do-while • continue

• return

© Oxford University Press 2013. All rights reserved.


PROGRAM CONTROL
STATEMENTS/CONSTRUCTS IN ‘C’

Program Control
Statements/Constructs

Selection/Branching Iteration/Looping

Conditional Unconditional do-


for while
Type Type while

if-else-
if if-else
if
switch break continue goto
OPERATORS

<
! >

!= ==
Operators
<=
||
>=
&& !=
RELATIONAL Equality and Logical
OPERATORS Operators

To Specify Symbol Used To Specify Symbol Used


less than <
Equal to ==
greater than >
Not equal to !=
less than or <=
equal to >= Logical AND &&
greater than or
equal to Logical OR ||

Negation !
POINTS TO NOTE
 If an expression, involving the relational operator, is true, it is given
a value of 1. If an expression is false, it is given a value of 0.
Similarly, if a numeric expression is used as a test expression, any
non-zero value (including negative) will be considered as true, while
a zero value will be considered as false.

 Space can be given between operand and operator (relational or


logical) but space is not allowed between any compound operator
like <=, >=, ==, !=. It is also compiler error to reverse them.

 a == b and a = b are not similar, as == is a test for equality, a = b is


an assignment operator. Therefore, the equality operator has to be
used carefully.

 The relational operators have lower precedence than all arithmetic


operators.
A FEW EXAMPLES
The following declarations and
initializations are given:
int x=1, y=2, z=3;
Then,
The expression x>=y evaluates to 0 (false).
The expression x+y evaluates to 3 (true).
The expression x=y evaluates to 2 (true).

© Oxford University Press 2013. All rights reserved.


LO G I C A L OP E R ATORS M AY B E M I XE D W I T H I N R E L AT I O NA L
E X P R E SSIO N S B U T O N E M U ST A B I DE BY T H E I R P R E C EDE NC E
RU L E S W H I C H I S AS FO L LOWS:

NOT AND OR
(!) operator && operator || operator

© Oxford University Press 2013. All rights reserved.


OPERATOR SEMANTICS

Operators Associativity
() ++ (postfix) -- (postfix) left to right
+ (unary) - (unary) right to left
++ (prefix) -- (prefix) * / % left to right
+- left to right
< <= > >= left to right
== != left to right
&& left to right
|| left to right
?: right to left
=+=-=*=/= right to left
, (comma operator) left to right

© Oxford University Press 2013. All rights reserved.


CONDITIONAL EXECUTION AND
SELECTION

Selection Statements

The Conditional Operator

The switch Statement

© Oxford University Press 2013. All rights reserved.


SELECTION STATEMENTS

One-way decisions using if statement

Two-way decisions using if-else statement

Multi-way decisions

Dangling else Problem

© Oxford University Press 2013. All rights reserved.


ONE-WAY DECISIONS USING IF STATEMENT

Flowchart for if construct


if(TestExpr)
stmtT; T F
TestExpr

stmtT

© Oxford University Press 2013. All rights reserved.


WRITE A PROGRAM THAT PRINTS THE
LARGEST AMONG THREE NUMBERS.
Algorithm C Program
1. START #include <stdio.h>
2. PRINT “ENTER THREE int main()
NUMBERS” {
int a, b, c, max;
3. INPUT A, B, C
printf(“\nEnter 3 numbers”);
4. MAX=A scanf(“%d %d %d”, &a, &b, &c);
5. IF B>MAX THEN MAX=B max=a;
if(b>max)
6. IF C>MAX THEN MAX=C
max=b;
7. PRINT “LARGEST if(c>max)
NUMBER IS”, MAX max=c;
8. STOP printf(“Largest No is %d”, max);
return 0;
}

© Oxford University Press 2013. All rights reserved.


TWO-WAY DECISIONS USING IF-ELSE
STATEMENT
Flowchart of if-else
The form of a two-way construct
decision is as follows:

if(TestExpr) TestExpr
stmtT;
else
stmtF;
stmtT stmtF

© Oxford University Press 2013. All rights reserved.


WRITE A PROGRAM THAT PRINTS THE
LARGEST AMONG THREE NUMBERS.

Algorithm C Program
1. START #include <stdio.h>
2. PRINT “ENTER THREE int main()
NUMBERS” {
int a, b, c, max;
3. INPUT A, B, C printf(“\nEnter 3 numbers”);
4. MAX=A scanf(“%d %d %d”, &a, &b, &c);
5. IF B>MAX THEN MAX=B max=a;
if(b>max)
6. IF C>MAX THEN MAX=C max=b;
7. PRINT “LARGEST if(c>max)
NUMBER IS”, MAX max=c;
printf(“Largest No is %d”, max);
8. STOP
return 0;
}

© Oxford University Press 2013. All rights reserved.


MULTI-WAY DECISIONS

if(TestExpr1)
switch(expr)
stmtT1; {
else if(TestExpr2) case constant1: stmtList1;
stmtT2; break;
else if(TestExpr3) case constant2: stmtList2;
break;
stmtT3;
case constant3: stmtList3;
.. . break;
else if(TestExprN) ………………………….
stmtTN; ………………………….
else default: stmtListn;
}
stmtF;

if-else-if ladder General format of switch


statements
© Oxford University Press 2013. All rights reserved.
FLOWCHART OF AN IF-ELSE-IF CONSTRUCT

TestExpr

TestExpr2

TestExpr3
stmtT1
stmtT2 TestExprN

stmtT3
stmtTF
stmtTN

© Oxford University Press 2013. All rights reserved.


THE FOLLOWING PROGRAM CHECKS WHETHER A
NUMBER GIVEN BY THE USER IS ZERO, POSITIVE, OR
NEGATIVE
#include <stdio.h>
int main()
{
int x;
printf(“\n ENTER THE NUMBER:”);
scanf(“%d”, &x);
if(x > 0)
printf(“x is positive \n”);
else if(x == 0)
printf(“x is zero \n”);
else
printf(“x is negative \n”);
return 0;
}

© Oxford University Press 2013. All rights reserved.


NESTED IF

 When any if statement is Construct 1 Construct 2


written under another if if(TestExprA) if(TestExprA)
statement, this cluster is if(TestExprB) if(TestExprB)
called a nested if. stmtBT; stmtBT;
else else
 The syntax for the stmtBF; stmtBF;
nested is given here: else else
stmtAF; if(TestExprC)
stmtCT;
else
stmtCF;

© Oxford University Press 2013. All rights reserved.


A PROGRAM TO FIND THE LARGEST AMONG
THREE NUMBERS USING THE NESTED LOOP
#include <stdio.h>
int main()
{
int a, b, c;
printf(“\nEnter the three numbers”);
scanf(“%d %d %d”, &a, &b, &c);
if(a > b)
if(a > c)
printf(“%d”, a);
else
printf(“%d”, c);
else
if(b > c)
printf(“%d”, b);
else
printf(“%d”, c);
return 0;
}

© Oxford University Press 2013. All rights reserved.


6. THE CONDITIONAL OPERATOR

 It has the following #include <stdio.h>


int main()
simple format: {
int a,b,c;
printf(“\n ENTER THE TWO
expr1 ? expr2 : expr3 NUMBERS:”);
scanf(“%d %d”, &a, &b);
c=a>b? a : b>a ? b : -1;
It executes by first if(c==-1)
evaluating expr1, which printf(“\n BOTH NUMBERS
ARE EQUAL”);
is normally a relational else
expression, and then printf(“\n LARGER NUMBER IS
evaluates either expr2, if %d”,c);
return 0;
the first result was true, }
or expr3, if the first
result was false. An Example

© Oxford University Press 2013. All rights reserved.


THE SWITCH STATEMENT

The general format of a


switch statement is
switch(expr)
{
case constant1: stmtList1;
break;
case constant2: stmtList2;
break;
case constant3: stmtList3;
break;
………………………….
………………………….
default: stmtListn;
}

The C switch construct


© Oxford University Press 2013. All rights reserved.
© Oxford University Press 2013. All rights reserved.
© Oxford University Press 2013. All rights reserved.
© Oxford University Press 2013. All rights reserved.
© Oxford University Press 2013. All rights reserved.
© Oxford University Press 2013. All rights reserved.
ICCS CEM
“WHILE” CONSTRUCT

while statement is a
Expanded SyntaxThe
pretest loop. of “while”
basic and
its Flowchart
syntax of theRepresentation
while
statement is shown below:

© Oxford University Press 2013. All rights reserved.


AN EXAMPLE

#include <stdio.h> This loop contains all the


int main() parts of a while loop.
{ When executed in a
int c; program, this loop will
c=5; // Initialization output
while(c>0)
5
{ // Test Expression
4
printf(“ \n %d”,c); 3
c=c-1; // Updating 2
} 1
return 0;
}

© Oxford University Press 2013. All rights reserved.


8.3 “DO-WHILE” CONSTRUCT

The C do-while loop


The form of this loop
construct is as
follows:
do
{
stmT; /* body of
statements would be
placed here*/
}while(TestExpr);

© Oxford University Press 2013. All rights reserved.


© Oxford University Press 2013. All rights reserved.
© Oxford University Press 2013. All rights reserved.
POINT TO NOTE

With a do-while statement, the body of the


loop is executed first and the test expression is
checked after the loop body is executed. Thus,
the do-while statement always executes the
loop body at least once.

© Oxford University Press 2013. All rights reserved.


“FOR” CONSTRUCT
 The general form of the for statement is as follows:
for(initialization; TestExpr; updating)
stmT;
for construct
flow chart

© Oxford University Press 2013. All rights reserved.


FOR LOOP

© Oxford University Press 2013. All rights reserved.


FOR LOOP

© Oxford University Press 2013. All rights reserved.


FOR LOOP

© Oxford University Press 2013. All rights reserved.


GOTO STATEMENT

The following program is used


The control is to find the factorial of a
unconditionally number.
#include <stdio.h>
transferred to the int main()
statement associated {
int n, c;
with the label specified long int f=1;
in the goto statement. printf(“\n Enter the
number:”);
The form of a goto scanf(“%d”,&n);
statement is if(n<0)
goto end;
for(c=1; c<=n; c++)
goto label_name; f*=c;
printf(“\n FACTORIAL IS %ld”,
f);
end:
return 0;
}

© Oxford University Press 2013. All rights reserved.


10. SPECIAL CONTROL STATEMENTS

“return” statements

“break” statements

“continue” statements

© Oxford University Press 2013. All rights reserved.


“BREAK” AND “CONTINUE” STATEMENTS

break continue
1. It helps to make an early 1. It helps in avoiding the
exit from the block where it remaining statements in a
appears. current iteration of the loop
and continuing with the next
Iteration
2. It can be used in all control 2. It can be used only in loop
statements including switch constructs.
construct.

© Oxford University Press 2013. All rights reserved.


NESTED LOOPS
 A nested loop refers to #include <stdio.h>
a loop that is contained int main()
within another loop. {
 If the following output int row, col;
has to be obtained on for(row=1;row<=4;++row)
the screen {
1 for(col=1;col<=row;++col)
22 printf(“%d \t”, row);
333 printf(“\n”);
4444 }
then the corresponding return 0;
program will be }

© Oxford University Press 2013. All rights reserved.

You might also like