Unit 3
Unit 3
Decision making in
C
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 1
Need of decision making
if number is odd
{
/* code */
}
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 2
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.
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 3
Decision Making Statements in C
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 4
Relational Operators
Relational Operator is used to compare two expressions.
It gives result either true or false based on relationship of two expressions.
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 5
If statement
if
if is single branch decision making statement.
If condition is true then only body will be executed.
if is a keyword. Flowchart of if
Syntax
if(condition)
{ False
// Body of the if condition
// true part
}
True
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 7
WAP to print Zero if given number is 0
Program Output
1 #include<stdio.h> Enter Number:0
2 void main() Zero
3 {
4 int a;
5 printf("Enter Number:");
6 scanf("%d",&a);
7 if(a == 0)
8 {
9 printf("Zero");
10 }
11 }
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 8
WAP to print Positive or Negative Number
Program Output
1 #include<stdio.h> Enter Number:5
2 void main() Positive Number
3 {
4 int a; Output
5 printf("Enter Number:"); Enter Number:-5
6 scanf("%d",&a); Negative Number
7 if(a >= 0)
8 {
9 printf("Positive Number");
10 }
11 if(a < 0)
12 {
13 printf("Negative Number");
14 }
15 }
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 9
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
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 10
WAP to print Odd or Even Number
Program Output
1 #include<stdio.h> Enter Number:12
2 void main() Even Number
3 {
4 int a; Output
5 printf("Enter Number:"); Enter Number:11
6 scanf("%d",&a); Odd Number
7 if(a%2 == 0)
8 {
9 printf("Even Number");
10 }
11 if(a%2 != 0)
12 {
13 printf("Odd Number");
14 }
15 }
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 11
If..else statement
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 13
WAP to print Positive or Negative Number using if…else
Program Output
1 #include<stdio.h> Enter Number:5
2 void main() Positive Number
3 {
4 int a; Output
5 printf("Enter Number:"); Enter Number:-5
6 scanf("%d",&a); Negative Number
7 if(a >= 0)
8 {
9 printf("Positive Number");
10 }
11 else
12 {
13 printf("Negative Number");
14 }
15 }
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 14
WAP to print Odd or Even Number using if…else
Program Output
1 #include<stdio.h> Enter Number:12
2 void main() Even Number
3 {
4 int a; Output
5 printf("Enter Number:"); Enter Number:11
6 scanf("%d",&a); Odd Number
7 if(a%2 == 0)
8 {
9 printf("Even Number");
10 }
11 else
12 {
13 printf("Odd Number");
14 }
15 }
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 15
WAP to find largest number from given 2 numbers using if
Program Output
1 #include<stdio.h> Enter Two Numbers:4
2 void main() 5
3 { 5 is largest
4 int a, b;
5 printf("Enter Two Numbers:");
6 scanf("%d%d",&a,&b);
7 if(a > b)
8 {
9 printf("%d is largest", a);
10 }
11 if(a < b)
12 {
13 printf("%d is largest", b);
14 }
15 }
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 16
WAP to find largest number from given 2 numbers using if…else
Program Output
1 #include<stdio.h> Enter Two Numbers:4
2 void main() 5
3 { 5 is largest
4 int a, b;
5 printf("Enter Two Numbers:");
6 scanf("%d%d",&a,&b);
7 if(a > b)
8 {
9 printf("%d is largest", a);
10 }
11 else
12 {
13 printf("%d is largest", b);
14 }
15 }
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 17
{ }
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
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 18
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 20
if…else if…else ladder flowchart
condition False
1
… …
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 21
WAP to print Zero, Positive or Negative Number
Program Output
1 #include<stdio.h> Enter Number:5
2 void main() Positive Number
3 {
4 int a; Output
5 printf("Enter Number:"); Enter Number:-5
6 scanf("%d",&a); Negative Number
7 if(a > 0)
8 printf("Positive Number");
9 else if(a==0)
10 printf("Zero");
11 else
12 printf("Negative Number");
13 }
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 22
Nested if
Nested if
If condition-1 is true then condition-2 is evaluated. If it is true then statement-1 will
be executed.
If condition-1 is false then statement-3 will be executed.
Syntax
if(condition-1)
{
if(condition-2)
{
statement-1;
}
else
{
statement-2;
}
}
else
{
statement-3;
}
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 24
Nested if flowchart
True
Statement Statement
1 2
Next
Statement
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 25
WAP to print maximum from given three numbers
Program
1 void main(){
2 int a, b, c; Output
3 printf("Enter Three Numbers:"); Enter Three Numbers:7
4 scanf("%d%d%d",&a,&b,&c); 5
5 if(a>b) 9
6 { 9 is max
7 if(a>c)
8 printf("%d is max",a);
9 else
10 printf("%d is max",c);
11 }
12 else
13 {
14 if(b>c)
15 printf("%d is max",b);
16 else
17 printf("%d is max",c);
18 }
19 }
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 26
Conditional Operator
? : (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.
False
True
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 28
Conditional operator flowchart
Here, Expression1 is the condition to be
evaluated.
True Expression False
1 If the condition(Expression1) is True then
Expression2 will be executed and the result
will be returned.
Expression Expression
2 3
Otherwise, if condition(Expression1) is
false then Expression3 will be executed
Variable
and the result will be returned.
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 29
WAP to find largest number from given 2 numbers using ? :
Program Output
1 #include<stdio.h> Enter Two Numbers:4
2 void main() 5
3 { 5 is largest
4 int a, b, max;
5 printf("Enter Two Numbers:");
6 scanf("%d%d",&a,&b);
7 max = a>b?a:b;
8 printf("%d is largest",max);
9 }
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 30
switch…case
switch...case
The switch statement allows to execute one code block among many alternatives.
It works similar to if...else..if ladder.
Syntax The expression is evaluated once and
switch (expression) compared with the values of each case.
{
case constant1: If there is a match, the corresponding
// statements statements after the matching case are
break; executed.
case constant2:
// statements If there is no match, the default
break; statements are executed.
.
. If we do not use break, all statements
. after the matching label are executed.
default:
// default statements The default clause inside the switch
} statement is optional.
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 32
WAP that asks day number and prints day name using switch…case
void main(){ case 7:
int day; printf("Saturday");
printf("Enter day number(1-7):"); break;
scanf("%d",&day); default:
switch(day) printf("Wrong input");
{ break;
case 1: }
printf("Sunday"); }
break;
case 2:
printf("Monday");
break;
case 3:
Output
printf("Tuesday");
break; Enter day number(1-7):5
case 4: Thursday
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 33
Practice programs
1) Write a program to check whether entered character is vowel or not?
2) Write a program to perform Addition, Subtraction, Multiplication and Division of
2 numbers as per user’s choice (using if…else/Nested if/if..else if ladder).
3) Write a program to read marks of five subjects. Calculate percentage and print
class accordingly. Fail below 35, Pass Class between 35 to 45, Second Class
between 45 to 60, First Class between 60 to 70, Distinction if more than 70.
4) Write a program to find out largest number from given 3 numbers (Conditional
operator).
5) Write a program to print number of days in the given month using Switch
statement.
Computer
Prof.Programming
Nilesh Gambhava with C(102000110) #3110003 (PPS) – Decision Making 34
Thank you