CS101: Introduction To Computers: Conditional Expressions
CS101: Introduction To Computers: Conditional Expressions
Computers
Conditional Expressions
Nov-19
Work on int, char, float,
CS101, Programming
double… 4
Examples
Rel. Expr. Result Remark
3>2 1
3>3 0
‘z’ > ‘a’ 1 ASCII values used for char
2 == 3 0
‘A’ <= 65 1 'A' has ASCII value 65
‘A’ == ‘a’ 0 Different ASCII values
(‘a’ – 32) == ‘A’ 1
5 != 10 1
1.0 == 1 AVOID May give unexpected result
due to approximation
Output: 0,1
Remember
value 0 represents false.
any other value represents true.
E !E
0 1
Non-0 0
Nov-19 CS101, Programming 8
Examples
Expr Result Remark
2 && 3 1
2 || 0 1
‘A’ && ‘0’1 ASCII value of ‘0’≠0
‘A’ && 0 0
‘A’ && ‘b’1
! 0.0 1 0.0 == 0 is
guaranteed
! 10.05 0 Any real ≠ 0.0
(2<5) && (6>5) 1 Compound expr
* / % LR
+ - LR
Precedence == != LR
and && LR
|| LR
Associativity = RL
(Refined)
Class Quiz 2
What is the value of expression:
0 <= 10 <= 4
a) Compile time error
c) False (0)
0 /*False*/
Nov-19 CS101, Programming 14
Conditional Statements
In daily routine
If it is very cold, I will skip class.
If there is a quiz tomorrow, I will first
study and then sleep. Otherwise I will
sleep now.
If I have 500 Rs, I will order pizza. If I
have 20 Rs, I will eat Maggi. If I have 5
Rs, I will eat biscuits. If I do not have
any money, I will eat in hostel mess
switch-case
statement S2
statement S3 S3
Execution of if-else statement
First the expression is evaluated.
If it evaluates to a non-zero value, then S1
is executed and then control (program
counter) moves to S3.
If expression evaluates to 0, then S2 is
executed and then control moves to S3.
S1/S2 can be block of statements!
Nov-19 CS101, Programming 21
if statement (no else!)
General form of the if statement
if (expression)
statement S1
S1
statement S2
Execution of if statement S2
TRUE FALSE
a<=b
TRUE FALSE
a<=b
TRUE Print
b<=c
FALSE Print
print TRUE Print
a<=c
FALSE Print
print
a ca cb b c bc a
int day;
scanf (“%d”, &day);
if (day == 1) { printf(“Sunday”); }
else if (day == 2) { printf (“Monday”); }
else if (day == 3) { printf (“Tuesday”); }
else if (day == 4) { printf (“Wednesday”); }
else if (day == 5) { printf (“Thursday”); }
else if (day == 6) { printf (“Friday”); }
else if (day == 7) { printf (“Saturday”); }
else { printf (“ Illegal day %d”, day); }
Nov-19 CS101, Programming 34
Example 2
int day;
scanf (“%d”, &day);
if (day == 1) { printf(“Weekend”); }
else if (day == 2) { printf (“Weekday”); }
else if (day == 3) { printf (“Weekday”); }
else if (day == 4) { printf (“Weekday”); }
else if (day == 5) { printf (“Weekday”); }
else if (day == 6) { printf (“Weekday”); }
else if (day == 7) { printf (“Weekend”); }
else { printf (“ Illegal day %d”, day); }
Nov-19 CS101, Programming 36
Weekday - version 2
int day;
scanf (“%d”, &day);
if ((day == 1) || (day == 7)) {
printf(“Weekend”);
} else if ( (day == 2) || (day == 3)
|| (day == 4) || (day == 5)
|| (day == 6)) {
printf (“Weekday”);
} else {
printf (“ Illegal day %d”, day);
Nov-19
} CS101, Programming 37
Weekday - version 3
int day;
scanf (“%d”, &day);
if ((day == 1) || (day == 7)) {
printf(“Weekend”);
} else if ( (day >= 2) && (day <= 6) ) {
printf (“Weekday”);
} else {
printf (“ Illegal day %d”, day);
}