Presentation is loading. Please wait.

Presentation is loading. Please wait.

If statement.

Similar presentations


Presentation on theme: "If statement."— Presentation transcript:

1 If statement

2 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

3 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 !!!

4 Examples a < b x+y >= k/m fabs(denum) <

5 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

6 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

7 Precedence for Arithmetic, Relational, and Logical Operators

8 Exercise Assume that following variables are declared
a = 5.5 b = k = -3 Are the following true or false a < k a + b >= 6.5 k != a-b !(a == 3*b) a<10 && a>5 fabs(k)>3 || k<b-a

9 If statement if(Boolean expression) statement; //single statement
{ //more than one statement statement1; . statement n; }

10 Examples if (x>0) { x=sqrt(x); } k++; 4 2 x k -2 2 x k 4 2 x k -2 2

11 if - else statement if (boolean expression) statement1; else
{ statement block1 } statement block2 x=y; x=y; k=3;

12 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

13 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;

14 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;

15 nested if-else if(x > y) if(y < z) k++; if (y<z) k++; else
m++; j++; if (y<z) k++; else m++;

16 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

17 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

18 Exercise Given a score and the following grading scale write a program to find the corresponding grade. A B C 0-39 D

19 Solution if ((score >= 80) && (score <=100)) grade = 'A';
grade = 'B'; if ((score >= 40) && (score <= 59)) grade = 'C'; if ((score >= 0) && (score <= 39)) grade = 'D';

20 Nested if - else statement
if (boolean expression) { statement block1 } else if (boolean expression2 { statement block2 else if (Boolean expression3) { statement block3 else { statement block4

21 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’;

22 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

23 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

24 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

25 Minimum if ((a<b) && (a<c)) min = a;
else if ((b<a) && (b<c)) min = b; else if ((c<a) && (c<b)) min = c;

26 Maximum if ((a>b) && (a>c)) max = a;
else if ((b>a) && (b>c)) max = b; else if ((c>a) && (c>b)) max = c;

27 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))

28 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;

29 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

30 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");

31 Alternate Solution if (x>0) if (y>0) printf("Region1\n"); else


Download ppt "If statement."

Similar presentations


Ads by Google