Unformatted text preview:

CSE 142 Computer Programming I Overview Concepts this lecture Conditional execution if statement Conditional expressions Relational and logical operators Compound statements Conditionals 2000 UW CSE F 1 Related Reading F 2 Control Flow Control flow is the order in which statements are executed Read Sections 4 1 4 5 4 7 4 9 4 1 Control structure preview Until now control flow has been sequential the next statement executed is the next one that appears in order in the C program 4 2 Relational and logical operators 4 3 if statements 4 4 Compound statements 4 5 Example 4 7 Nested if statements F 3 Conditional Execution Conditional Control Flow choosing which of two or more statements to execute before continuing F 4 choosing whether or not to to skip a statement before continuing A conditional statement allows the computer to choose an execution path depending on the value of a variable or expression if the withdrawal is more than the bank balance then print an error if today is my birthday then add one to my age if using whole milk add two eggs otherwise add three eggs F 5 F 6 F 1 Conditional if Statement Conditional Flow Chart if condition statement The statement is executed if and only if the condition is true if withdrawalAmount balance if x 100 x x 1 y y 1 yes printf Not enough money n x 100 x x 1 if temperature 98 6 printf You have a fever n no if x 100 x x 1 F 7 Conditions y y 1 F 8 Conditional Expressions In parentheses is a condition also called a logical or Boolean expression Made up of variables constants arithmetic expressions and the relational operators Math symbols in C F 9 Value of Conditional Expressions air temperature 80 0 98 6 body temperature marital status M divisor 0 Such expressions are used in if statements and numerous other places in C F 10 Value of Conditional Expressions What is the value of a conditional expression Answer we think of it as TRUE or FALSE What is the value of a conditional expression Answer we think of it as TRUE or FALSE Under the hood in C it s really an integer FALSE is 0 and 0 is FALSE TRUE is any value other than 0 and non zero is TRUE 1 is the result of a true relational operator e g 4 7 evaluates to 1 F 11 F 12 F 2 Complex Conditionals Complex Conditionals in C We use Boolean operators to code complex conditionals in C if I have at least 15 or you have at least 15 then we can go to the movies We ll say lots more about this later For now here is some information for reference if the temperature is below 32 degrees and it s raining then it s snowing Boolean operators if it s not the case that it s Saturday or Sunday then it s a work day define define F 13 and TRUE FALSE or not 1 0 if myMoney 15 0 yourMoney 15 0 canGoToMovies TRUE Multiple Actions Compound Statement What if there s more than one conditional action Groups together statements so that they are treated as a single statement If your temperature is high then you have a fever and should take two aspirin and go to bed and call in sick tomorrow F 14 statement1 statement2 Also called a block Highly useful Not just in conditionals but many places in C F 15 Using a Compound Statement if temperature 98 6 printf You have a fever n aspirin aspirin 2 printf Go to bed n printf Sleep in tomorrow n F 17 F 16 Combining and Substituting Statements You may use a compound statement anywhere that a single statement may be used Anywhere that a statement is allowed in C any kind of statement can be used A compound statement can contain any number of statements including 0 Among other things these principles imply that compound statements can be nested to any depth F 18 F 3 Another Compound Example Finding Absolute Value 1 Problem Compute the absolute value x of x and put the answer in variable abs Here are three solutions all correct Cash machine program fragment if balance withdrawal balance balance withdrawal dispense funds withdrawal What if omitted What if omitted if x 0 abs x if x 0 abs x F 19 F 20 Finding Absolute Value 2 Finding Absolute Value 3 Problem Compute the absolute value x of x and put the answer in variable abs Here are three solutions all correct Problem Compute the absolute value x of x and put the answer in variable abs Here are three solutions all correct if x 0 abs x abs x if x 0 abs x abs x if x 0 abs x if x 0 abs x if x 0 abs x if x 0 abs x if x 0 abs x else abs x F 21 F 22 if else Control Flow if else Print error message only if the condition is false yes balance withdrawal no if balance withdrawal balance balance withdrawal balance balance withdrawal dispense funds withdrawal dispense funds withdrawal else no here printf Insufficient Funds n printf No money n F 23 arrive here whether condition is TRUE or FALSE F 24 F 4 Nested if Statements Nested ifs Part II define BILL SIZE 20 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 if balance withdrawal balance balance withdrawal dispense funds withdrawal else if balance BILL SIZE printf Try a smaller amount n else printf Go away n F 25 F 26 Tax Table Example Direct Solution Problem Print the tax based on income tax income if income 15000 printf No tax if income 15000 income 30000 printf 18 tax if income 30000 income 50000 printf 22 tax if income 50000 income 100000 printf 28 tax if income 100000 printf 31 tax 15 000 0 15 000 30 000 18 30 000 50 000 22 50 000 100 000 28 100 000 31 F 27 Cascaded ifs if income 15000 printf No tax else if income 30000 printf 18 tax else if income 50000 printf 22 tax else if income 100000 printf 28 tax else printf 31 tax F 28 Mutually exclusive conditions only one will be true Warning Danger Ahead if income 15000 printf No tax else if income 30000 printf 18 tax else if income 50000 printf 22 tax else if income 100000 printf 28 tax else printf 31 tax F 29 The idea of conditional execution is natural intuitive and highly useful However Programs can get convoluted and hard to understand There are syntactic pitfalls to avoid F 30 Order is important Conditions are evaluated in order given F 5 Pitfalls of if Part I The World s Last C Bug if x 10 printf x is 10 status check radar if status 1 launch missiles Bug …


View Full Document

UW CSE 142 - Conditionals

Loading Unlocking...
Login

Join to view Conditionals and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Conditionals and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?