If statement
Conditional Expressions If statement uses conditions A condition is an expression that can be evaluated to be TRUE (any value > 0) or FALSE (value of 0) Conditional Expression is composed of expressions combined with relational and/or logical operators
Relational Operators == equality (x == 3) != non equality (y != 0) < less than (x<y) > greater than (y>10) <= less than equal to (x<=0) >= greater than equal to (x>=y) !!! a==b vs. a=b !!!
Examples a < b x+y >= k/m fabs(denum) < 0.0001
Logical Operators ! not !(x==0) && and (x>=0) && (x<=10) || or (x>0) || (x<0) A B A && B A || B !A !B False True
Examples a<b<c is not the same as (a<b) && (b<c) a+b * 2 < 5 && 4>=a/2 || t-2 < 10 a=3, b=5, t=4
Precedence for Arithmetic, Relational, and Logical Operators
Exercise Assume that following variables are declared a = 5.5 b = 1.5 k = -3 Are the following true or false a < 10.0 + k a + b >= 6.5 k != a-b !(a == 3*b) a<10 && a>5 fabs(k)>3 || k<b-a
If statement if(Boolean expression) statement; //single statement { //more than one statement statement1; . statement n; }
Examples if (x>0) { x=sqrt(x); } k++; 4 2 x k -2 2 x k 4 2 x k -2 2
if - else statement if (boolean expression) statement1; else { statement block1 } statement block2 x=y; x=y; k=3;
If-else statement What does the following program do? Assume that x, y, temp are declared. if (x>y) temp = x; else temp = y; 1 3 ? x y temp 3 1 ? x y temp
If-else statement Split the following statement into two separate if if (x>y) temp = x; else temp = y; if (x>y) temp = x; if (x<=y) temp = y;
Exercise Write an if-else statement to find both the maximum and minimum of two numbers. Assume that x,y, min, max are declared. if (x>y) Ex: x = 10, y = 5 { y = 3, y = 4 max = x; x = 6, y = 6 min = y; } else { max = y; min = x;
nested if-else if(x > y) if(y < z) k++; if (y<z) k++; else m++; j++; if (y<z) k++; else m++;
Exercise int x=9, y=7, z=2, k=0, m=0, j=0; if (x > y) if(y < z) else m++; j++; What are the values of j, k and m after execution of if statement? 9 7 x y 2 z k m j
Exercise int x=3, y=7, z=2, k=0, m=0, j=0; if (x > y) if(y < z) else m++; j++; What are the values of j, k and m after execution of if statement? 3 7 x y 2 z k m j
Exercise Given a score and the following grading scale write a program to find the corresponding grade. 80-100 A 60-79 B 40-59 C 0-39 D
Solution if ((score >= 80) && (score <=100)) grade = 'A'; grade = 'B'; if ((score >= 40) && (score <= 59)) grade = 'C'; if ((score >= 0) && (score <= 39)) grade = 'D';
Nested if - else statement if (boolean expression) { statement block1 } else if (boolean expression2 { statement block2 else if (Boolean expression3) { statement block3 else { statement block4
Solution if ((score >= 80) && (score <=100)) grade = 'A'; else if ((score >= 60) && (score <= 79)) grade = 'B'; else if ((score >= 40) && (score <= 59)) grade = 'C'; else if ((score >= 0) && (score <= 39)) grade = 'D'; else grade = ‘I’;
Alternate Solution grade = ‘I’; else if (score >= 80) grade = 'A'; grade = 'B'; else if (score >= 40) grade = 'C'; else if (score >= 0) grade = 'D'; else
Exercise Write a program that reads 3 numbers a, b and c from user and computes minimum, maximum and median of the numbers. Example: a = 2, b = 5, c = 3 minimum = 2, maximum = 5, median = 3 a = 2, b = 2, c = 3 minimum = 2, maximum = 3, median = 2
Exercise continued Find minimum of 3 numbers Write an if statement to check if minimum is a if (condition) what should min = a; be condition? Write an if statement to check if minimum is b Write an if statement to check if minimum is c Combine 1-3 into a single nested if-else statement Similarly find maximum of 3 numbers Find median of 3 numbers
Minimum if ((a<b) && (a<c)) min = a; else if ((b<a) && (b<c)) min = b; else if ((c<a) && (c<b)) min = c;
Maximum if ((a>b) && (a>c)) max = a; else if ((b>a) && (b>c)) max = b; else if ((c>a) && (c>b)) max = c;
Median median = b; else if ((c<b) && (b<a)) if ((a<b) && (b<c)) median = b; else if ((c<b) && (b<a)) else if ((b<a) && (a<c)) median = a; else if ((c<a) && (a<b)) else if ((b<c) && (c<a)) median = c; else if ((a<c) && (c<b))
Alternate Solution for Median if (((a<b) && (b<c)) || ((c<b) && (b<a))) median = b; else if (((b<a) && (a<c)) || ((c<a) && (a<b))) median = a; else if (((b<c) && (c<a)) || ((a<c) && (c<b))) median = c;
Exercise Write a program that reads a point (x, y) from user and prints the region it resides in. For example Enter x, y: 3 -1 Point in Region 4 Enter x, y: -1 -5 Point in Region 3 Region 2 Region 1 Region 3 Region 4
Solution if ((x>0) && (y>0)) printf("Region 1\n"); else if ((x<0) && (y>0)) printf("Region 2\n"); else if ((x<0) && (y<0)) printf("Region 3\n"); else if ((x>0) && (y<0)) printf("Region 4\n");
Alternate Solution if (x>0) if (y>0) printf("Region1\n"); else