142 h -1 conditionals essential feature for a computer language conditional statement: allows the...
TRANSCRIPT
142 H -1
ConditionalsEssential feature for a computer language
Conditional Statement:allows the computer to choose betweenseveral 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 ifthe condition is true
142H -2
Flow ChartIn terms of a flow chart,
if (x < 100) x = x+1;
translates intox = 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
142 H -3
• Use boolean operators to write conditions
&& for AND|| for OR! for NOT
Recall the logical table:
FFTT
TFFF
TTTF
P, Q are conditionsT and F stand for True and False
P Q ! P P && Q P || QT TT FF TF F
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
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?”);
Syntax of the if statement (1)
142 H-5
Examples:
temperature = 99.5;
if (temperature>98.6) printf(“You have a fever”);
pulse = 80.;
controlflow
temperature = 98.1;
if (temperature>98.6) printf(“You have a fever”);
pulse = 80.;
controlflow
(printf isnot executed)
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 statementuse a compound statement (also called “block”)
142 H-6
Syntax of the if statement (2)
block
Some Examples (1)
142 H-7
_ An ATM program
if (balance >= withdrawal){ balance = balance - withdrawal; dispense_funds(withdrawal);}
function called to give themoney to the ATM user.
Can’t omit theparentheses (compilation error)
What happens if we omit the braces {}?
dispense_funds is executed even whenbalance is less than withdrawal!
/* 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 inmain
_ Computing an absolute value
Some Examples (2)
142 H-8
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 istrue or false
x >= 0
abs = xabs = -x
printf...
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!”);}
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
if statement and { } (2)
However:
if (x==5) if (y==5) printf(“Both are 5\n”);else printf(“Is anybody 5?\n”);
Which ifis it associatedto?
if (x==5){ if (y==5) printf(“Both are 5\n”);}else printf(“Is anybody 5?\n”);
Write
142 H-12
This one!
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
unmatched