Conditional Statement
Lecture - 06
Muhammad Mustagis Billah
Former Director Professor & Head
PGD in ICT Program
PGD-in-ICT Dept of Computer Science & Math.
Math
Bangladesh Agricultural University
Mymensingh, Bangladesh
Today’s Topic
2
1. Nested if‐else statement
2. switch statement
3. Logical Operators
Nested if-
if-else statement
3
Problem: WAPTCP the largest number from three given numbers.
Test Data :
Input: Enter a Number: 25 78 23
Output: 78 is largest.
Test
est Data
ata :
Input: Enter a Number: 55 23 51
Output: 55 is largest.
Test Data :
Input: Enter a Number: 55 13 91
Output: 91 is largest.
Nested if-
if-else statement
4
Input: A, B, C
No Yes
A>B
No Yes No Yes
B>C A>C
Output: B Output: C Output: C Output: A
Program Codes
5
1 #include <stdio.h>
2 int grade(int a,a int b,
b int c){
3 int max;
4 if(a > b){
5 if(a > c)
6 max = a;
7 else
8 max = c;
9 }else{
10 if(b > c) int main() {
11 max = b; int x,y;
12 else printf("Enter two numbers: ");
13 max = c; scanf("%d%d%d", &x, &y, &z);
14 } grade(x,y,z);
15 printf(“%d is largest
largest.”,
” ma
max);
) ret rn 0;
return 0
16 return 0; }
17 }
Switch Statement
6
Problem:
Write a program in C to read any digit, display
in the word.
Input : 4
Output : Four
How we think?
7
Problem: WAPT read any digit, display in the word.
Input : 0
Output : Zero
Input : 1
Output : One
………….
………….
Input : 9
Output : Nine
Input : other than 0-
0-9
Output : Invalid
How it work?
8
switch(n
switch(
(n){
Input : 0 case : 0
Output : Zero printf(“Zero”);
printf (“Zero”);
Input
p :1 break;
Output : One case : 1
…………. printf(“One”);
printf (“One”);
…………. break;
………….
Input : 9 case : 9
Output : Nine printf(“Nine”);
printf (“Nine”);
Input : other than 0-
0-9 break;
p : Invalid
Output default :
printf(“Invalid”);
printf (“Invalid”);
}
Structure of switch statement
9
switch (expression)
{
case value1:
//Statements
b
break;k
case value 2:
//Statements
break;
……………
……………
case value n:
//Statements
break;
Default:
//Statements
}
Rules of Switch Statement
10
The switch statement must be an integral type.
Case labels must be constants.
Case labels must be unique.
Case labels must end with a colon.
colon
The break statement transfers the control out
of switch statement.
statement
The break statement is optional.
Logical Operators
11
Used to formulate a Boolean expression
p for makingg
a decision and testing conditions
Returns true or false
Operator Meaning Example
Logical AND. True only If c = 5 and d = 2 then, expression
&& if all operands are true ((c==5) && (d>5)) equals to 0.
Logical
L i l OR.
OR True
T only l if If c = 5 and
d d = 2 then,
h expression
i
|| either one operand is true ((c==5) || (d>5)) equals to 1.
Logical NOT
NOT. True only If c = 5 then,
then expression !(c==5)
! if the operand is 0 equals to 0.
Logical Operators (AND)
12
Operator : AND
Symbol : &&
A eবং B di জেন ঐ পয্ােকটটা িনেয় আেসা।
A and B both bring that packet.
A B A B Result
0 0 0
0 1 0
1 0 0
1 1 1
Logical Operators
13
int a = 5,
5 b = 5,
5 c = 10
(a == b) && (c > b)
True && True
1 && 1
1
Program Codes
14
1 Problem: WAPTCP the largest number from three given numbers.
2
3
4
5 int a, b, c, max;
6 if((a > b) && (a > c))
7
8 max = a;
9
10
else if((b > a) && (b > c))
11 max = b;
12
13 else
14 max = c;
15
16
17
Program Codes
15
1 Problem: WAPTCP the largest number from three given numbers.
2
3 #include <stdio.h>
4 int grade(int a, int b, int c){
5 int max;
6 if((a > b) && (a > c))
7 max = a;
8 if((b
(( > a)) && ((b > c))
))
9 max = b;
10 else int main() {
11 max = c; int x,y,z;
12 printf("Enter two numbers: ");
13 printf(“%d is largest.”, max); scanf("%d%d%d", &x, &y, &z);
14 return 0; grade(x,y,z);
15 } ret rn 0
return 0;
16 }
17
Logical Operators (OR)
16
Operator : OR
Symbol :||
A aথবা B েকu eকজেন ঐ পয্ােকটটা িনেয় আেসা।
A or B anyone bring that packet.
A B A B Result
0 0 0
0 1 1
1 0 1
1 1 1
Logical Operators
17
int a = 5,
5 b = 5,5 c = 10
(a > b) | | (c > b)
False | | True
0 || 1
1
Program Codes
18
Problem: WAPTC check whether a character is an
alphabet,
l h b t di
digit
it or special
i l character.
h t
char x;
if((( >=‘A’)
if(((x ‘A’) && ((x <=‘Z’))
‘Z’)) | | ((x
(( >=‘a’)
‘ ’) && ((x <=‘z’)))
‘ ’)))
printf(“Alphabet.”);
else if((x
(( >= ‘0’)) && ((x <=‘9’)) ))
printf(“Digit.”);
else
printf(“special
printf( special character
character.”); );
Program Codes
19
Problem: Write a C program to check whether an
alphabet
l h b t is
i a vowell or consonant.
t
char x;
if(( ==‘A’)
if((x ‘A’) | | (x
( == ‘a’)
‘ ’) | | (x
( == ‘E’) | | (x
( == ‘e’)
‘ ’) | | (x
(
== ‘I’) | | (x == ‘i’) | | (x == ‘O’) | | (x == ‘o’) | | (x ==
‘U’)) | | ((x == ‘u’))
))
printf(“vowel .”);
else
printf(“consonant.”);
Logical Operators (Not)
20
Operator : Not
Symbol :!
A Result
0 1
1 0
Logical Operators
21
int a = 5,
5 b = 5,5 c = 10
!((a > b) | | (c > b))
!( False | | True)
!(0 | | 1)
!1
0
Logical Operators
22
int a = 5,5 b = 5,5 c = 10
1. (a == b) && (c > b) is 1
2. ((a == b)) && ((c < b)) is 0
3. (a == b) || (c < b) is 1
4. (a != b) || (c < b) is 0
5. !(a != b) is 1
6. !(a == b) is 0
23