UNIT-3 CONTROL STRUCTURE IN
1
Decision Making or Conditional Statement
▪ C program statements are executed sequentially.
▪ Decision Making statements are used to control the flow of
program.
▪ It allows us to control whether a program segment is executed or
not.
▪ It evaluates condition or logical expression first and based on its
result (either true or false), the control is transferred to particular
statement.
▪ If result is true then it takes one path else it takes another path.
Decision Making Statements in C
▪ Decision Making Statements are
One way Decision: if (Also known as simple if)
Two way Decision: if…else
Multi way Decision: if…else if…else if…else
Two way Decision: ?: (Conditional Operator)
n-way Decision: switch…case
Relational Operators
▪ Relational Operator is used to compare two expressions.
▪ It gives result either true or false based on relationship of two
expressions.
▪ A relational operator checks the relationship between two
operands. If the relation is true, it returns 1; if the relation is false,
it returns value 0.
IF STATEMENT
5
If statement
▪ if is single branch decision making statement.
▪ If condition is true then only body will be executed.
▪ if is a keyword.
Syntax
Flowchart of if
if(condition)
{
// Body of the if
False
// true part condition
}
True
…
Number is positive or negative
Step 1: Read no.
Start
Step 2: If no is greater than equal zero, go
to step 4.
Read no. Step 3: Print no is a negative number, go
to step 5.
True False Step 4: Print no is a positive number.
Is no >= 0
Step 5: Stop.
Print no is Print no is
Positive Negative
Stop
WAP to print Number is positive or negative
Program
#include<stdio.h>
int main() Output
{ Enter Number:5
int a; Positive Number
printf("Enter Number:");
scanf("%d",&a); Output
if(a >= 0) Enter Number:-5
{ Negative Number
printf("Positive Number");
}
if(a < 0)
{
printf("Negative Number");
}
}
WAP to print Zero if given number is 0
Program
#include<stdio.h> Output
void main()
Enter Number:0
{
Zero
int a;
printf("Enter Number:");
scanf("%d",&a);
if(a == 0)
{
printf("Zero");
}
}
Modulus Operator
▪ % is modulus operator in C
▪ It divides the value of one expression (number) by the value of another
expression (number), and returns the remainder.
▪ Syntax: express1 % express2
▪ E.g.
• 7%2 Answer: 1
• 6%2 Answer: 0
• 25%10 Answer: 5
• 37%28 Answer: 9
Number is odd or even
Step 1: Read no.
Start
Step 2: If no mod 2 = 0, go to step 4.
Step 3: Print no is a odd, go to step 5.
Read no.
Step 4: Print no is a even.
True Step 5: Stop.
Is no%2= 0
False
Print no is Even Print no is odd
Stop
WAP to print Odd or Even Number
Program
#include<stdio.h>
void main() Output
{
Enter Number:12
int a;
Even Number
printf("Enter Number:");
scanf("%d",&a);
Output
if(a%2 == 0)
{ Enter Number:11
printf("Even Number"); Odd Number
}
if(a%2 != 0)
{
printf("Odd Number");
}
}
▪ Example:: Odd_even.c
IF..ELSE STATEMENT
13
if...else statement
▪ if…else is two branch decision making statement
▪ If condition is true then true part will be executed else false part
will be executed
▪ else is keyword
Flowchart of if…else
Syntax
if(condition)
{ True False
condition
// true part
}
else
{ … …
// false part
}
…
WAP to print Positive or Negative Number
using if…else
Program
#include<stdio.h>
int main() Output
{ Enter Number:5
int a; Positive Number
printf("Enter Number:");
scanf("%d",&a); Output
if(a >= 0) Enter Number:-5
{ Negative Number
printf("Positive Number");
}
else
{
printf("Negative Number");
}
}
▪ Example:: po_negative.c
WAP to print Odd or Even Number using
if…else
Program
#include<stdio.h>
void main() Output
{
Enter Number:12
int a;
Even Number
printf("Enter Number:");
scanf("%d",&a);
Output
if(a%2 == 0)
{ Enter Number:11
printf("Even Number"); Odd Number
}
else
{
printf("Odd Number");
}
}
▪ Example:: Odd_even.c
WAP to find largest number from given 2
numbers using if…else
Program
#include<stdio.h>
Output
void main()
{ Enter Two Numbers:4
int a, b; 5
printf("Enter Two Numbers:"); 5 is largest
scanf("%d%d",&a,&b);
if(a > b)
{
printf("%d is largest", a);
}
else
{
printf("%d is largest", b);
}
}
▪ Example::Max_2.c
Condition statement
▪ If body of if contains only one statement then { } are not
compulsory
▪ But if body of if contains more than one statements then { }
are compulsory
if(a >= b)
{
printf("%d is largest", a);
} Both
are
same
if(a >= b)
printf("%d is largest", a);
IF…ELSE IF…ELSE IF…ELSE
LADDER IF
ELSE IF LADDER
19
If…else if…else if…else
▪ if…else if…else if…else is multi branch decision making
statement.
▪ If first if condition is true then remaining if conditions will not
be evaluated.
▪ If first if condition is false then second if condition will be
evaluated and if it is true then remaining if conditions will not be
evaluated.
▪ if…else if…else if…else is also known as if…else if
ladder
Syntax
if(condition-1)
statement-1;
else if(condition-2)
statement-2;
else
statement-3;
if…else if…else ladder flowchart
False
condition1
True False
condition2
… True Condition
False
3
… True
…
WAP to print Zero, Positive or Negative
Number
Program
#include<stdio.h>
void main() Output
{ Enter Number:5
int a; Positive Number
printf("Enter Number:");
scanf("%d",&a); Output
if(a > 0)
Enter Number:-5
printf("Positive Number");
Negative Number
else if(a==0)
printf("Zero");
else
printf("Negative Number");
}
▪ Example:: ElseIfLadder.c
NESTED IF
23
Nested if
• A nested if in C is an if statement that is the target of another if statement.
• Nested if statements means an if statement inside another if statement.
Syntax
if(condition-1)
{
if(condition-2) • If condition-1 is true then
{
statement-1;
condition-2 is evaluated. If it is
} true then statement-1 will be
else executed.
{
statement-2;
}
} • If condition-1 is false then
else
{
statement-3 will be executed.
statement-3;
}
Nested if flowchart
False True
condition1
False
Statement3 condition2
True
Statement 1 Statement 2
Next
Statement
Largest number from 3 numbers
(Flowchart)
Start
Read a, b, c
True False
Is a>b
True False True False
Is a>c Is b>c
Print a is Print b is
Print c is largest Print c is largest
largest largest
Stop
WAP to print maximum from given three
numbers
Program
void main(){
int a, b, c; Output
printf("Enter Three Numbers:"); Enter Three Numbers:7
scanf("%d%d%d",&a,&b,&c); 5
if(a>b) 9
{ 9 is max
if(a>c)
printf("%d is max",a);
else
printf("%d is max",c);
}
else
{
if(b>c)
printf("%d is max",b);
else
printf("%d is max",c);
}
}
CONDITIONAL OPERATOR
28
? : (Conditional Operator)
▪ The conditional works operator is similar to the if-else.
▪ It is also known as a ternary operator.
▪ It returns first value of expression (before colon(:)) if expression is
true and second value of expression if expression is false.
Fals
Tru e
e
variable = Expression1 ? Expression2 : Expression3
Result value
Result value
Conditional operator flowchart
• Here, Expression1 is the condition
to be evaluated.
True False
Expression1 • If the condition(Expression1) is
True then Expression2 will be
executed and the result will be
Expression Expression returned.
2 3
• Otherwise ,if condition
Variable (Expression1) is false then
Expression3 will be executed and
the result will be returned
WAP to find largest number from given 2
numbers using ? :
Program
#include<stdio.h> Output
void main() Enter Two Numbers:4
{ 5
int a, b, max; 5 is largest
printf("Enter Two Numbers:");
scanf("%d%d",&a,&b);
max = a>b?a:b;
printf("%d is largest",max);
}
SWITCH…CASE
32
switch...case
▪ The switch statement allows to execute one code block among
many alternatives.
▪ It works similar to if...else..if ladder.
Syntax
switch (expression)
{
case constant1:
// statements
break;
case constant2:
// statements
break;
.
.
.
default:
// default statements
}
switch...case
The expression is evaluated once and compared with the values of
each case.
If there is a match, the corresponding statements after the
matching case are executed.
If there is no match, the default statements are executed.
If we do not use break, all statements after the matching label are
executed.
The default clause inside the switch statement is optional.
WAP that asks day number and prints day
name using switch…case
case 7:
void main() printf("Saturday");
{ break;
int day; default:
printf("Enter day number(1-7):"); printf("Wrong input");
scanf("%d",&day); break;
switch(day) }
{ }
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break; Output
case 3: Enter day number(1-7):5
printf("Tuesday");
break; Thursday
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday"); Example::Switchcase.c
break;
case 6:
printf("Friday");
break;
LOOPING CONTROL
STRUCTURES
36
What is loop?
▪ Loop is used to execute the block of code several times according
to the condition given in the loop. It means it executes the same
code multiple times.
▪ When we have some steps to be performed repeatedly, we need
to use looping control structures in our program.
▪ Looping control structures help our program make more readable,
compact and more structured.
“Hello” 5
printf("Hello\n"); Hello
printf("Hello\n"); Hello loop(condition)
{
printf("Hello\n"); Hello //statements
printf("Hello\n"); Hello }
printf("Hello\n"); Hello
if v/s while
Flowchart of if v/s Flowchart of while
False False
condition condition
True True
… …
Looping or Iterative Statements in C
Looping Statements are
Entry Controlled Loop: while, for
Exit Controlled Loop: do…while
Virtual Loop: goto
• Which type of looping statement to use depends on the situation.
Introduction (Cont)
• The loop is controlled by a condition. The condition decides how
many times the statements will be executed.
• If there are more than one sentence (which will normally be the
case) in the loop, then those sentences are put in { } symbols.
• The statements within { } symbols are called body of the loop. We
can say that there are two parts of loop : condition and body.
Types of loops
• Depending on where the condition is checked, we can have two
types of loop structures:
1. Entry Control
2. Exit Control
• In entry control loop, the condition is written first and then the
body of statements.
• While in exit control loop, the body of statements is written first
and then condition is written.
• This means that body of statements in exit control loop will be
executed at least once.
WHILE LOOP (ENTRY
CONTROL LOOP)
42
While loop
▪ While loop is helpful, when we do not know in advance, how
many times the statements are to be repeated.
• Here, the condition is checked first and then only the statements
are executed. So, while loop is entry controlled loop.
• The statements in the body part will be executed till the condition
is TRUE. As soon as the condition becomes false, the control will
come out of the while loop.
• If the condition remains TRUE always then, it will lead to infinite
loop. So, writing a valid condition is important.
Syntax
while(condition)
{
// Body of the while
// true part
}
Print 1 to 10
Step 1: Initialize a to 1 and
Start Read N.
Step 2: If a<=N then go to step
a=1 3 otherwise go to step 6
Read N
Step 3: print a
Step 4: a=a+1.
Is a<=N
Step 5: goto step 2
yes Step 6: Stop.
True False
Print a
no
a=a+1
Stop
WAP to print 1 to n(while loop)
Program
#include <stdio.h> Output
void main() Enter n:10
{ 1
int i,n; 2
i=1; 3
printf("Enter n:"); 4
scanf("%d",&n); 5
while(i<=n) 6
{ 7
8
printf("%d\n",i);
9
i=i+1; 10
}
}
Sum of First 10 interger numbers
Step 1: Sum=0
Start
Step 2: i=1
Sum =0 Step 3: Repeat from step 4
i=1 through 5 until i<=10 otherwise
go to step 6.
no yes Step 4:sum=sum+i.
Is i<= 10
Step 5: i=i+1.
Step 6: Print Sum.
Print Sum Sum=sum+i
Step 7: Stop.
Stop i=i+1
WAP to Sum of First 10 interger numbers
Program
#include<stdio.h>
void main()
{
int sum=0, i=1;
while(i<=10)
{
sum=sum+i;
i=i+1;
}
printf("Sum is=%d",sum);
}
Sum of 10 numbers read from user
Step 1: Sum=0
Start
Step 2: i=1
Sum =0 Step 3: If i< =5 then go to step 4
i=1 other wise go to step 8.
Step 4: read num.
no yes Step 5: sum=sum+num.
Is i<= 5
Step 6: i=i+1.
Print Sum
Step 7: go to step 3
Read num
Step 8: Print Sum.
Step 9: Stop.
Stop Sum=sum+num
i=i+1
WAP to Sum of 10 numbers entered by
user(while loop)
Program
#include<stdio.h>
void main()
{
int sum=0, i=1,n;
while(i<=10)
{
printf("Enter a number=");
scanf("%d",&n);
sum=sum+n;
i=i+1;
}
printf("Sum is=%d",sum);
}
▪ Example:: Sum_Num_read_user.c
WAP to print Odd numbers between 1 to n
(while loop)
Program
#include<stdio.h>
void main() Output
{
Enter the value of n
int i=1, n; 5
printf(“Enter the value of n”); 1
scanf(“%d ”,&n); 3
while(i <= n) 5
{
if(i%2!=0)
printf(“%d/n”,i);
i=i+1;
}
}
WAP to print multiplication table
(while loop)
Program
#include<stdio.h> Output
void main()
Enter n for
{
multiplication table:5
int i=1,n; 5 * 1 = 5
printf("Enter n for multiplication ta 5 * 2 = 10
ble:"); 5 * 3 = 15
scanf("%d",&n); 5 * 4 = 20
while(i<=10) 5 * 5 = 25
{ 5 * 6 = 30
printf("%d * %d = %d\n",n,i,n*i); 5 * 7 = 35
5 * 8 = 40
i=i+1;
5 * 9 = 45
} 5 * 10 = 50
}
DO WHILE LOOP (EXIT
CONTROL LOOP)
52
DO….While loop
▪ Do..While loop is helpful, when we do not know in advance, how many
times the statements are to be repeated.
• When we want that body part execute at least once everytime,then
do..while loop is useful. Condition is checked after the body part ,
that’s why it is “Exit controlled” loop.
• The statements in the body part will be executed till the condition is
TRUE. As soon as the condition becomes false, the control will come
out of the while loop.
• If the condition remains TRUE always then, it will lead to infinite loop.
So, writing a valid condition is important.
Syntax
do
{
// Body of the while
// true part
} while(condition);
WAP to print multiplication table( do..while
loop)
Program
#include<stdio.h> Output
void main()
Enter n for
{
multiplication table:5
int i=1,n; 5 * 1 = 5
printf("Enter n for multiplication ta 5 * 2 = 10
ble:"); 5 * 3 = 15
scanf("%d",&n); 5 * 4 = 20
do 5 * 5 = 25
{ 5 * 6 = 30
printf("%d * %d = %d\n",n,i,n*i); 5 * 7 = 35
5 * 8 = 40
i=i+1;
5 * 9 = 45
} while(i<=10); 5 * 10 = 50
}
FOR LOOP
55
For loop
▪ for loop is helpful, when we know in advance, how many times the
statements are to be repeated. Control variable is needed to track the
iterations in the loop.
• There are three statements in for loop:1)Intialization 2) Condition 3)
Update statement for control variable.
• The statements in the body part will be executed till the condition is
TRUE. As soon as the condition becomes false, the control will come
out of the for.
• If the condition remains TRUE always then, it will lead to infinite loop.
So, writing a valid condition is important.
Syntax
for(initialization;condition;updatestatement)
{
// Body of the while
// true part
}
WAP to print multiplication table( for loop)
Program
#include<stdio.h> Output
void main() Enter n for
{ multiplication table:5
int i=1,n; 5 * 1 = 5
printf("Enter n 5 * 2 = 10
5 * 3 = 15
for multiplication table:");
5 * 4 = 20
scanf("%d",&n); 5 * 5 = 25
for(i=1;i<=10;i++) 5 * 6 = 30
{ 5 * 7 = 35
printf("%d * %d = %d\n",n,i,n*i); 5 * 8 = 40
} 5 * 9 = 45
} 5 * 10 = 50