0% found this document useful (0 votes)
48 views36 pages

CS101: Introduction To Computers: Conditional Expressions

The document discusses conditional expressions and statements in C programming. It covers: 1) Conditional expressions evaluate to true or false and are used to control program flow. Relational and logical operators are used to build conditional expressions. 2) Common relational operators include >, <, ==, != which compare values. Logical operators include &&, ||, !. 3) Conditional statements like if-else and if allow executing code conditionally based on expression evaluations. 4) Examples are provided to demonstrate conditional expressions, operators, and if/if-else statements to print outputs based on inputs.

Uploaded by

Divya Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views36 pages

CS101: Introduction To Computers: Conditional Expressions

The document discusses conditional expressions and statements in C programming. It covers: 1) Conditional expressions evaluate to true or false and are used to control program flow. Relational and logical operators are used to build conditional expressions. 2) Common relational operators include >, <, ==, != which compare values. Logical operators include &&, ||, !. 3) Conditional statements like if-else and if allow executing code conditionally based on expression evaluations. 4) Examples are provided to demonstrate conditional expressions, operators, and if/if-else statements to print outputs based on inputs.

Uploaded by

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

CS101: Introduction to

Computers
Conditional Expressions

CS101, Programming (These lecture slides are


Nov-19 courtesy of CSE Department, IIT Kanpur) 1
Conditional Expressions
An expression that evaluates to either
true or false.
 Often known as Boolean expression.
C does not have a separate Boolean
data type
 Value 0 is treated as false.
 Non-zero values are treated as true.

Nov-19 CS101, Programming 2


Conditional Expressions
If an expression evaluates to true, we
get a value 1
 Think of 1 as default true value
If an expression evaluates to false, we
get a value 0

Nov-19 CS101, Programming 3


Relational Operators
Compare two quantities
Operator Function
> Strictly greater than
>= Greater than or equal to
< Strictly less than
<= Less than or equal to
== Equal to
!= Not equal to

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

Avoid mixing int and float values while


comparing. Comparison with floats is not exact!
Nov-19 CS101, Programming 5
Examples
# include <stdio.h>
int main() {
float x;
x = 0.1;
printf(“%d,”, x==0.1);
printf(“%d”, x!=0.1);
return 0;
}

Output: 0,1

Nov-19 CS101, Programming 6


Logical Operators
Logical Function Allowed Types
Op
&& Logical AND char, int, float, double
|| Logical OR char, int, float, double
! Logical NOT char, int, float, double

Remember
 value 0 represents false.
 any other value represents true.

Nov-19 CS101, Programming 7


Truth Tables
E1 E2 E1 && E2 E1 || E2
0 0 0 0
0 Non-0 0 1
Non-0 0 0 1
Non-0 Non-0 1 1

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

Nov-19 CS101, Programming 9


Examples
You can create very complex
expressions, involving arithmetic,
logical and relational operators,
constants, variables, function calls.
e.g:

(x + 7 > 93) && !(y + 3 % z)


|| (abs(sqrt(w) – g) < epsilon)

Nov-19 CS101, Programming 10


Example
Problem: Input 3 positive integers.
Print the count of inputs that are even
and odd.
INPUT
 Do not use if-then-else
10 OUTPUT
int a; int b; int c; 5 Even=1
int cEven; // count of even inputs
3 Odd=2
scanf(“%d%d%d”, &a,&b,&c); // input a,b,c

// (x%2 == 0) evaluates to 1 if x is Even,


// 0 if x is Odd
cEven = (a%2 == 0) + (b%2 == 0) + (c%2 == 0);
printf(“Even=%d\nOdd=%d”, cEven, 3-cEven);
Nov-19 CS101, Programming 11
! +- RL

* / % LR

+ - 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

b) Run time crash

c) False (0)

The correct answer is


d) True (1)

Nov-19 CS101, Programming 13


Evaluation Probably not what you
intended. The intended
0 <= 10 <= 4 expression is:
0 <= 10 && 10 <= 4
(0 <= 10) <= 4
(0 <= 10) && (10 <= 4)
1 <= 4
(1) && (10 <= 4)
1 /* True */
1 && (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 

Nov-19 CS101, Programming 15


Conditional statements in C
3 types of conditional statements in C
 if (cond) action
else some-other-action
 if (cond) action

 switch-case

Each action is a sequence of one or


more statements!

Nov-19 CS101, Programming 16


if-else statement
Read two integers and print the min.
# include <stdio.h>
int main() {
int x, y;
scanf(“%d%d”, &x,&y);
if (x < y) {
printf(“%d”, x); 1. Check if x is less
} else { than y.
printf(“%d”, y); 2. If so, print x
} 3. Otherwise, print y.
return 0;
}
Nov-19 CS101, Programming 19
Tracing Execution of if-else
Run the program
# include <stdio.h>
int main() { Input
int x; int y;
scanf(“%d%d”, &x,&y); 6 10
if (x < y) { x y
printf(“%d\n”,x);
}
else { printf(“%d\n”,y);} 6 10
return 0;
}
6 < 10 so the if-branch is taken Output
6
Nov-19 CS101, Programming 20
if-else statement
General form of the if-else statement
if (expression)
statement S1
else S1 S2

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

 First the expression is evaluated.


 If it evaluates to a non-zero value, then S1
is executed and then control (program
counter) moves to the statement S2.
 If expression evaluates to 0, then S2 is
executed.

Nov-19 CS101, Programming 22


Example
Problem: Input a, b, c are real positive
numbers such that c is the largest of these
numbers. Print ACUTE if the triangle formed
by a, b, c is an acute angled triangle and
print NOT ACUTE otherwise.
int main() {
float a; float b; float c;
scanf(“%f%f%f”, &a,&b,&c); /* input a,b,c */

if ( (a*a + b*b) > (c*c) ) { /* expression*/


printf(“ACUTE”);
}
else {
printf(“NOT ACUTE”);
}
return 0;
Nov-19 } CS101, Programming 23
Finding min of 3 numbers
Input
a,b,c

TRUE FALSE
a<=b

TRUE FALSE TRUE FALSE


a<=c b<=c

Print Print Print Print


a c b c

Nov-19 CS101, Programming 24


Input
a,b,c
int a,b,c;
scanf(“%d%d%d”,&a,&b,&c);
TRUE
a<=b
FALSE if (a <= b) {
if (a <= c) {
TRUE
a<=c
FALSE TRUE
b<=c
FALSE
printf(“min = %d”,a);
}
Print Print Print Print else {
a c b c
printf(“min = %d”, c);
}
Each branch
}
translates to an
else {
if-else
if (b <= c) {
statement
printf(“min = %d”, b);
Hierarchical }
branches result else {
in nested if-s printf(“min =%d”, c);
}
Nov-19
} CS101, Programming 25
More Conditionals
 Sorting a sequence of numbers (i.e.,
arranging the numbers in ascending or
descending order) is a basic primitive.
 Problem: read three numbers into a, b
and c and print them in ascending order.
 Start with the flowchart for finding minimum
of three numbers and add one more level of
conditional check.
 Then translate the flowchart into C program.
Finding min of 3 numbers
Input Replace print by more
a,b,c comparisons and then
print.
TRUE FALSE
a<=b

TRUE FALSE TRUE FALSE


a<=c b<=c

Print Print Print Print


a c b c

Nov-19 CS101, Programming 27


Ascending order of 3 numbers
Input
a,b,c

TRUE FALSE
a<=b

TRUE FALSE TRUE FALSE


a<=c b<=c

TRUE Print
b<=c
FALSE Print
print TRUE Print
a<=c
FALSE Print
print
a ca cb b c bc a

print print print print


abc
Nov-19
acb bac
CS101, Programming
bca 28
if (a <= b) {
if (a <= c) { /* a <= b and a <= c */
if (b <= c) { /* a <= b, a <= c, b <= c */
printf(“%d %d %d \n”, a, b, c);
} else { /* a <= b, a <= c, c < b */
printf(“%d %d %d \n”, a, c, b);
}
} else { /* a <= b, c < a*/
printf(“%d %d %d \n”, c, a, b) ;
}
} else { /* b < a */
if (b <= c) { /* b < a and b <= c */
if (a <= c) { /* b < a, b <= c, a <= c */
printf(“%d %d %d\n”, b, a, c);
} else { /* b < a, b <= c, c < a */
printf(“%d %d %d\n”, b, c, a); }
}
} else { /* b < a, c < b */
printf(“%d %d %d\n”, c, b, a); }
}
}
Nested if, if-else
Earlier examples showed us nested
if-else statements
if (a <= b) {
if (a <= c) { … } else {…}
} else {
if (b <= c) { … } else { … }
}
Because if and if-else are also
statements, they can be used
anywhere a statement or block can
be used.
Nov-19 CS101, Programming 30
Else if
A special kind of nesting is the chain
of if-else-if-else-… statements
if (cond1) { if (cond1)
stmt1 stmt-block1

General form of if-else-if-else…


} else { else if (cond2)
if (cond2) { stmt-block2
stmt2 else if (cond3)
} else { stmt-block3
if (cond3) { else if (cond4)
…. stmt-block4
} else if …
} else
} last-block-of-stmt

Nov-19 CS101, Programming 32


Example

Given an integer day , where 1 ≤


𝑑𝑎𝑦 ≤ 7, print the name of the
weekday corresponding to day.
1: Sunday
2: Monday

7: Saturday

Nov-19 CS101, Programming 33


Printing the day

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

Given an integer day , where 1 ≤


𝑑𝑎𝑦 ≤ 7, print Weekday, if the day
corresponds to weekday, print
Weekend otherwise.
1, 7: Weekend
2,3,4,5,6: Weekday

Nov-19 CS101, Programming 35


Weekday - version 1

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);
}

Nov-19 CS101, Programming 38


Summary of if, if-else
if-else, nested if's, else if.
Braces {…} can be omitted if a block
has only one statement.
Multiple ways to solve a problem
 issues of better readability
 and efficiency.

Nov-19 CS101, Programming 39

You might also like