Presentation is loading. Please wait.

Presentation is loading. Please wait.

142 H -1 Conditionals Essential feature for a computer language Conditional Statement: allows the computer to choose between several paths depending on.

Similar presentations


Presentation on theme: "142 H -1 Conditionals Essential feature for a computer language Conditional Statement: allows the computer to choose between several paths depending on."— Presentation transcript:

1 142 H -1 Conditionals Essential feature for a computer language Conditional Statement: allows the computer to choose between several paths depending on the value of a variable or expression: e.g. if today is my birthday, add 1 to my age if the income is less than $30,000, take for the tax rate 10% else take 15% C Syntax: if (temperature > 98.6) printf("you have a fever.\n"); if ( condition ) statement; executed if and only if the condition is true

2 142H -2 Flow Chart In terms of a flow chart, if (x < 100) x = x+1; translates into x = x+1 True x< 100 False Writing a conditional expression: what follows the if Also called logical or boolean expressions Made of variables, constants, arithmetic expressions and relational operators > for greater than < for less than <= for less than or equal to != for not equal to == for equal to >= for greater than or equal to

3 142 H -3 Use boolean operators to write conditions && for AND || for OR ! for NOT Recall the logical table: F F T T T F F F T T T F P, Q are conditions T and F stand for True and False Examples temperature > 90.0 && humidity > 50.0 !(salary<30000 || exemptions<4) number_of_children==3 && age>35 Check the precedence order (inside cover of text) to be safe (and clear), use parentheses

4 142 H-4 Value of conditional expressions Recall that expressions have a value. What is the value of a conditional expression? Can think of it as TRUE or FALSE (however, there is no such type in C… but there is in C++) In Reality: it is an integer in C, FALSE is 0 TRUE is non zero (frequently 1, and always 1 if the result of the relational operator) Try these (but don’t use them!) if (0) printf(“0 is true”); if (!0) printf(“0 is false”); if (0.9) printf(“is printf executed?”);

5 Syntax of the if statement (1) 142 H-5 Examples: temperature = 99.5; if (temperature>98.6) printf(“You have a fever”); pulse = 80.; control flow temperature = 98.1; if (temperature>98.6) printf(“You have a fever”); pulse = 80.; control flow (printf is not executed)

6 if (temperature>98.6) { printf(“You have a fever”); aspirin = aspirin - 2; } A block can contain any number of statements any kind of statements Can have no statements in a block: if (x>100) {} /* OK */ Can also have more than 1 statement use a compound statement (also called “block”) 142 H-6 Syntax of the if statement (2) block

7 Some Examples (1) 142 H-7 _ An ATM program if (balance >= withdrawal) { balance = balance - withdrawal; dispense_funds(withdrawal); } function called to give the money to the ATM user. Can’t omit the parentheses (compilation error) What happens if we omit the braces {}? dispense_funds is executed even when balance is less than withdrawal !

8 /* one version */ if (x>=0) abs=x; if (x<0) abs=-x; /* another version */ abs = x; if (x<0) abs=-x; double abs(double x) { if (x<0) x=-x; return x; } As a function: local variable. Nothing is changed in main _ Computing an absolute value Some Examples (2) 142 H-8

9 142 H-9 if - else statement to compute the absolute value of x: if (x>=0) abs = x; else abs = -x; printf(“Absolute value of %.2f is %.2f”,x,abs); TrueFalse Control Flow: get here whether the condition is true or false x >= 0 abs = xabs = -x printf...

10 142 H-10 Formatting if statements Style: if (condition) { statement1; statement2;... } else { statement3; statement4;... } e.g. #define BILL_SIZE 20... if (balance >= withdrawal) { balance = balance - withdrawal; dispense_funds(withdrawal); } else { if (balance >= BILL_SIZE) printf(“Try a smaller amount”); else printf(“Sorry, you’re really broke!”); }

11 142 H-11 if statement and { } (1) Consider: if (x==5) if (y==5) printf( “Both are 5\n” ); else printf( “x is 5, but y is not\n” ); else if (y==5) printf( “y is 5, but x is not\n” ); else printf( “Neither is 5\n” ); OK

12 if statement and { } (2) However: if (x==5) if (y==5) printf(“Both are 5\n”); else printf(“Is anybody 5?\n”); Which if is it associated to? if (x==5) { if (y==5) printf(“Both are 5\n”); } else printf(“Is anybody 5?\n”); Write 142 H-12 This one!

13 142 H-13 Matching elses with ifs Each else matches some if in the same block if { if if else { if if else else} else } else Within the same block, read the ifs and elses left to right, matching each else to the closest unmatched if Some ifs may not be matched to any else if if if else else if if else if code within { } unmatched


Download ppt "142 H -1 Conditionals Essential feature for a computer language Conditional Statement: allows the computer to choose between several paths depending on."

Similar presentations


Ads by Google