DOC PREVIEW
UW CSE 142 - Lecture Notes

This preview shows page 1-2-17-18-19-35-36 out of 36 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 36 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

University of Washington Computer Programming IOverviewPowerPoint PresentationControl FlowConditional Control FlowSlide 6Slide 7Slide 8ConditionsSlide 10Value of Conditional ExpressionsSlide 12Slide 13Slide 14Slide 15Slide 16Slide 17Slide 18Another Compound ExampleSlide 20Slide 21Slide 22Slide 23if-else Control FlowSlide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36E-1University of WashingtonComputer Programming ILecture 6:Conditionals© 2000 UW CSEE-2OverviewConcepts this lectureConditional executionif statementConditional expressionsRelational and logical operators{Compound statements}E-3Related ReadingRead Sections 4.1-4.5, 4.7-4.94.1: Control structure preview4.2: Relational and logical operators4.3: if statements4.4: Compound statements4.5: Example4.7: Nested if statementsE-4Control Flow“Control flow” is the order in which statements are executedUntil now, control flow has been sequential -- the next statement executed is the next one that appears, in order, in the C programE-5Conditional Control Flowchoosing which of two (or more) statements to execute before continuingchoosing whether or not to to skip a statement before continuingE-6Conditional ExecutionA conditional statement allows the computer to choose an execution path depending on the value of a variable or expressionif the withdrawal is more than the bank balance, then print an errorif today is my birthday, then add one to my ageif using whole milk, add two eggs, otherwise add three eggsE-7if (condition) statement;The statement is executed if and only if the condition is true.if (withdrawalAmount > balance) printf( “Not enough money\n”);if (temperature > 98.6) printf(“You have a fever.\n”);if (x < 100) x = x + 1;Conditional ("if ") StatementE-8Conditional Flow Chartif (x < 100) x = x + 1 ;y = y + 1;x < 100 ? x = x + 1 ;y = y + 1;yesnoE-9ConditionsIn parentheses is a condition, also called a “logical” or “Boolean” expressionMade up of variables, constants, arithmetic expressions, and the relational operatorsMath symbols: < , , >,  , = , in C: < ,<=, > , >= , == , !=E-10 air_temperature > 80.098.6 <= body_temperaturemarital_status == ‘M’divisor != 0Such expressions are used in “if” statements and numerous other places in C.Conditional ExpressionsE-11Value of Conditional ExpressionsWhat is the value of a conditional expression??Answer: we think of it as TRUE or FALSEE-12Value of Conditional ExpressionsWhat is the value of a conditional expression??Answer: we think of it as TRUE or FALSEUnder the hood in C, it's really an integerFALSE 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)E-13Complex Conditionals if I have at least $15 or you have at least $15, then we can go to the moviesif the temperature is below 32 degrees and it’s raining, then it’s snowingif it’s not the case that it’s Saturday or Sunday, then it’s a work dayE-14Complex Conditionals in C We use Boolean operators to code complex conditionals in C.We’ll say lots more about this later! For now, here is some information for reference.Boolean operators && || ! and or not#define TRUE 1#define FALSE 0if (myMoney>=15.0 || yourMoney>=15.0) canGoToMovies = TRUE;E-15Multiple ActionsWhat if there’s more than one conditional action? “If your temperature is high, then you have a fever and should take two aspirin and go to bed and call in sick tomorrow”E-16Compound Statement Groups together statements so that they are treated as a single statement:{statement1 ;statement2 ;...}Also called a "block."Highly usefulNot just in conditionals, but many places in CE-17Using a Compound Statementif ( temperature > 98.6 ) {printf ( “You have a fever. \n” );aspirin = aspirin  2 ;printf (“Go to bed/n”);printf (“Sleep in tomorrow/n”);}E-18Combining 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 depthE-19Another Compound ExampleCash machine program fragment:if (balance >= withdrawal){ balance = balance – withdrawal; dispense_funds(withdrawal);}What if () omitted?What if {} omitted?E-20Finding Absolute Value (1)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;if ( x < 0 ) abs = -x;E-21Finding Absolute Value (2)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;if ( x < 0 ) abs = -x;abs = x;if ( x < 0 ) abs = -x;E-22Finding Absolute Value (3)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;if ( x < 0 ) abs = -x;abs = x;if ( x < 0 ) abs = -x;if (x >= 0) abs = x;else abs = -x;E-23if - else Print error message only if the condition is false:if ( balance >= withdrawal ) {balance = balance - withdrawal ;dispense_funds ( withdrawal ) ;}else {printf ( “Insufficient Funds! \n ” ) ;}no ; hereE-24if-else Control Flowbalance >= withdrawalprintf ( “No money! \n ” ) ;balance = balance - withdrawal ;dispense_funds ( withdrawal ) ;/* arrive here whether condition is TRUE or FALSE */noyesE-25Nested if Statements#define BILL_SIZE 20if ( balance >= withdrawal ) {balance = balance - withdrawal ;dispense_funds ( withdrawal ) ;} else {if ( balance >= BILL_SIZE ) printf ( “Try a smaller amount. \n ” ) ;else printf ( “Go away! \n ” ) ;}E-26if ( 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 ”) ; } Nested ifs , Part IIE-27< 15,000 15,000, < 30,000 30,000, < 50,000 50,000, < 100,000 100,0000%18%22%28%31%incometaxTax Table Example Problem: Print the % tax based on income:E-28Direct Solutionif ( income < 15000 ) { printf( “No tax.” );}if ( income >= 15000 && income < 30000 ) { printf(“18%% tax.”);}if ( income >= 30000 && income < 50000 ) {


View Full Document

UW CSE 142 - Lecture Notes

Download Lecture Notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Lecture Notes 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 Lecture Notes 2 2 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?