DOC PREVIEW
UW CSE 142 - Conditionals

This preview shows page 1-2 out of 6 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 6 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 6 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 6 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

F-1F-1CSE 142Computer Programming IConditionals© 2000 UW CSEF-2OverviewConcepts this lectureConditional executionif statementConditional expressionsRelational and logical operators{Compound statements} F-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 statementsF-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 programF-5Conditional Control Flowchoosing which of two (or more) statements to execute before continuingchoosing whether or not to to skip a statement before continuingF-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, otherwiseadd three eggsF-2F-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 ") StatementF-8Conditional Flow Chartif (x < 100) x = x + 1 ;y = y + 1;x < 100 ? x = x + 1 ;y = y + 1;yesnoF-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: < ,<=, > , >= , == , != F-10air_temperature > 80.098.6 <= body_temperaturemarital_status == ‘M’divisor != 0Such expressions are used in “if” statements and numerous other places in C.Conditional Expressions F-11Value of Conditional ExpressionsWhat is the value of a conditional expression??Answer: we think of it as TRUE or FALSEF-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)F-3F-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 degreesand it’s raining, then it’s snowingif it’s not the case that it’s Saturday or Sunday, then it’s a work dayF-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;F-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”F-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 CF-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”);} F-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 depthF-4F-19Another Compound ExampleCash machine program fragment:if (balance >= withdrawal){balance = balance – withdrawal;dispense_funds(withdrawal);}What if () omitted?What if {} omitted?F-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;F-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;F-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;F-23if - elsePrint error message only if the condition is false:if ( balance >= withdrawal ) {balance = balance - withdrawal ;dispense_funds ( withdrawal ) ;}else {printf ( “Insufficient Funds! \n ” ) ;}no ; hereF-24if-else Control Flowbalance >= withdrawalprintf ( “No money! \n ” ) ;balance = balance - withdrawal ;dispense_funds ( withdrawal ) ;/* arrive here whether condition is TRUE or FALSE */noyesF-5F-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 ” ) ;F-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 IIF-27< 15,00015,000, < 30,00030,000, < 50,00050,000, < 100,000100,0000%18%22%28%31%incometaxTax Table Example Problem: Print the % tax based on income:F-28Direct Solutionif ( 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.”);}Mutually exclusive conditions - only one will be trueF-29if ( income < 15000 ) { if ( income < 15000 ) {printf( “No tax” ); printf( “No tax” );} else { } else if ( income < 30000 ) {if ( income < 30000 ) { printf( “18%% tax.” );printf( “18%% tax.” ); } else if ( income < 50000 ) {} else { printf( “ 22%% tax.” );if ( income <


View Full Document

UW CSE 142 - Conditionals

Download Conditionals
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 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 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?