Presentation is loading. Please wait.

Presentation is loading. Please wait.

If and If-Else Statements 1. Motivation 2 Need to build an intelligent program for an A/C to set the temperature between 20-30 deg C. How?? Too Cold Turn.

Similar presentations


Presentation on theme: "If and If-Else Statements 1. Motivation 2 Need to build an intelligent program for an A/C to set the temperature between 20-30 deg C. How?? Too Cold Turn."— Presentation transcript:

1 If and If-Else Statements 1

2 Motivation 2 Need to build an intelligent program for an A/C to set the temperature between 20-30 deg C. How?? Too Cold Turn off the compressor Too Hot Turn on the compressor

3 Outline Flow Chart Programming with Decisions (2 or more choices) If-Else Statements Switch Statement 3

4 Flowchart Flowchart: A Diagram representing sequential steps in the process or algorithm in order to achieve specific tasks Components in Flowchart: Sequences, Selections, Iterations symbols Creating a flowchart before the actual coding (in C) helps one to finish the task in a more systematic and timely manner 4

5 Symbols in Flowchart 5 Terminal symbol Process or statement symbol Input/output symbol Decision symbol, if the condition is true, perform task 1, if not, perform task 2 Start X=70; Get, print Condition

6 Flowchart (Continued) 6 connector symbol (Coming in from many directions) link symbol (Used in case you are out of space to write) function symbol (Indicating there is more details in the function) flow line ( Direction of the flow in commands/statements ) repeated line (Indicating repeated steps in flow charts)

7 1 Flowchart Showing a program that decides if a new car needs an oil change. 7 Start…..Once the car’s age and the mileage are entered into its database, the program will suggest an oil change if the age is more than 6 months or the mileage is more than 5000. If both conditions are false, then no oil change is needed. The END….

8 (Programming with selections) Instead of sequential flow of controls/statements, when you need to select a path to go (from 2 choices), the decision is based on the. Two types of checking conditions: 1. Relational expression 2. Logical expression which has value of 1 (or positive integer) when it is TRUE, and 0 when it is false 8

9 Relational Expression Relational operators: 9 Relational operators Meanings Greater than <=Less than or equal >=More than or equal ==Equal !=Not equal Examples of correct expression:  ( num_student < 3 )  ( sum_money > 200 )  -1.3 >= (2.0*x + 3)  ( score != -999)

10 Relational Expression (Cont’) 10 Incorrect expression:  a =< b //out of order  a < = b //space is not allowed  a >> b //refers shift expression  a = b //assignment statement  a = = b-1 //space is not allowed Note: Machine has precision issues (Round off). Do not use Float in the relational expression for checking the equal or not equal unless you have to

11 Logical Expression Logical operators: 11 Logical operators Meanings !(NOT, negation) &&Logic AND ||Logic OR Example: a=5;b=10;c=1;d=0; (aa) || (c==1) True (1)!(a>d)False (0)

12 Logical Expression Truth Table 12 expression exp1exp2exp1 && exp2exp1 || exp2 0000 0101 1001 1111

13 Logical Expression (Continued) 13 Short Cut  For expr1 && expr2, If we know that expr1 is false (0). Does not matter what value expr2 is, the result is always false. -For expr1 || expr2, if expr1 is not zero, the result is true no matter what value expr2 is.

14 If Statement If (Condition) is true, the statement_1 is executed If (Condition) is false, the statement_1 is NOT executed 14 if (condition) statement_1; Flowchart If-single statement

15 Example: If-Statement (Truth Table) 15 #include void main() { int expr1,expr2; printf("Enter expr 1: "); scanf("%d",&expr1); printf("Enter expr 2: "); scanf("%d",&expr2); ////////////////////////////////////// if (expr1 && expr2)/* AND*/ printf("expr1 && expr2 is: True \n"); if (!(expr1 && expr2)) printf("expr1 && expr2 is: False \n"); if ((expr1 || expr2) == 1)/* OR */ printf("expr1 || expr2 is: True"); if ((expr1 || expr2) == 0) printf("expr1 || expr2 is: False"); /////////////////////////////////////// printf("\n"); }

16 If Compound Statements Two or more statements to be executed when if (condition) is true or false, they are grouped using {………} 16 if (condition) { statement1; statement2; … } Flowchart If-multiple statement

17 Example: If Compound Statement 17 #include void main() { int expr1,expr2; printf("Enter expr 1: "); scanf("%d",&expr1); printf("Enter expr 2: "); scanf("%d",&expr2); ////////////////////////////////////// if (expr1 && expr2)/* AND*/ { printf("Since expr1 && expr2 is: True \n"); printf("So, expr1 || expr2 is: True"); } if ((expr1 || expr2) == 0) { printf("Since, expr1 || expr2 is: False\n"); printf("So, expr1 || expr2 is: False"); } if (( (expr1 && expr2) == 0) && (expr1||expr2) ) { printf("expr1 && expr2 is: False\n"); printf("But, expr1 || expr2 is: True"); } /////////////////////////////////////// printf("\n"); }

18 If-Else (compound) Statement If-else statement, There are actions (statements) for both TRUE and FALSE conditions 18 if (condition) { statement1; statement2; … } else { statement3; statement4; … } Flowchart If-Else statement

19 If-else statement 19 #include void main() { int expr1,expr2; printf("Enter expr 1: "); scanf("%d",&expr1); printf("Enter expr 2: "); scanf("%d",&expr2); ////////////////////////////////////// if (expr1 && expr2)/* AND*/ printf("expr1 && expr2 is: True \n"); else printf("expr1 && expr2 is: False \n"); if (expr1||expr2) printf("expr1 || expr2 is: True \n"); else printf("expr1 || expr2 is: False"); /////////////////////////////////////// printf("\n"); }

20 If-Else Compound Statement: TEMP conversion 20 #include int main( ) {char type; float temp, fahren, celcius; printf("enter temperature: "); scanf("%f", &temp); printf("enter f(farenheit) or c(celcius) for temperature unit: "); scanf("\n%c", &type); if ( type == 'f') { celcius = (5.0/9.0) *(temp -32.0); printf("\nThe equivalent celcius temperature is %6.2f\n", celcius); } else { fahren = (9.0/5.0)*(temp) + 32.0; printf("\nThe equivalent farenheit temperature is %6.2f\n", fahren); } return 0; }

21 If-Else-If Statement If-else-if statement used when there is more than 2 choices of actions (See the Flow Chart) 21 if (condition1) statement1; else if (condition2) statement2; else if (condition3) statement3; else defaultstatement;

22 Example: If-Else-If Compound Statement Year in the study Program 1 st Year “Freshman” 2 nd Year “Sophomore” 3 rd Year “Junior” 4 th Year “Senior” Others “Super” 22 #include int main( ) { int year; printf("enter student year:"); scanf("%d",&year); if ( year == 1) printf("Freshman"); else if ( year == 2) printf("Sophomore"); else if ( year == 3) printf("Junior"); else if ( year == 4) printf("Senior"); else printf("Super"); return 0; }

23 Nested If Statement Nested if statement: 2(or more) condition needs to be checked for an action, and another action is taken when only one of the condition is true. 23 if (condition1) statement1; if (condition 2) statement2; else statement3; else statement4; Inner If

24 Example: Nested If Compound Statement Two numbers are entered. Check if the first number is between [0,9], and Check if the 2 nd number is between [10,99] 24 #include int main( ) { int num1, num2; printf("program to check num1 (1-9) and num2 (10- 99)\n"); printf("enter num1 num2:"); scanf("%d %d",&num1,&num2); if ( num1 >=0 && num1 <= 9) if (num2 >= 10 && num2 <= 99) printf("Both numbers are entered correctly"); else printf("only num1 is entered correctly"); else if (num2 >= 10 && num2 <= 99) printf("only num2 is entered correctly"); else printf("Both numbers are not entered correctly"); return 0; }

25 Switch Statement switch statement: for more than 1 course of action, similar to if- else-if statement: (). For discrete values only; does not deal with ranges of values 25 switch (expression) { case const1Expr: statement11; statement12; … break; case const2Expr: statement21; statement22; … break; … default: defaultstatement1; defaultstatement2; … } Break statement in every cases except the last (default) one

26 Example: Switch Statement 26 switch (i) { case 1: case 2: case 3: flag = 0; break; case 4: flag = 1; break; default: process(h); }

27 Example: Switch Statement 27 #include int main( ) { int year; printf("enter student year:"); scanf("%d",&year); switch (year) { case 1: printf("Freshman"); break; case 2: printf("Sophomore"); break; case 3: printf("Junior"); break;

28 Example Switch Statement (Continued) 28 case 4: printf("Senior"); break; default: printf("Super"); } return 0; }


Download ppt "If and If-Else Statements 1. Motivation 2 Need to build an intelligent program for an A/C to set the temperature between 20-30 deg C. How?? Too Cold Turn."

Similar presentations


Ads by Google