DOC PREVIEW
CSUN COMP 106 - Programming

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

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

Unformatted text preview:

Multiple condition if statements February 23, 20061Programming with If Statements Programming with If Statements using Multiple Conditionsusing Multiple ConditionsLarry CarettoComputer Science 106Computing in Engineering and ScienceFebruary 23, 20062Outline• Review last class– Program flow controls– if statements• Exercises with if statements• Multiple choices• Exercises with multiple choices• Sequential if statements versus the if-else-if structure3Review Sequential FlowPrevious StatementStatement after NextCurrent StatementNext Statement• Statements are normally executed in sequential order but you must have the correct sequence• What is wrong withy = x * x;cin >> x;cout << y;4Review Choice StatementsFalse blockYesNoIs condition true?True blockNext statement5Review Choice Before LoopsYesNoIs condition true?Loop blockNext statementStatements in loop block must change condition6Review Choice After LoopsYesNoIs condition true?Loop blockNext statementStatements in loop block must change conditionMultiple condition if statements February 23, 200627Review Function• Transfer control and data to separate part of program• Return control and data FunctionMath library functions are an exampleYou will create your own functions later8Review Conditions• A condition is an expression that evaluates to a boolean value of true or false– Use relational operators: greater than >, equal to ==, less than <, not equal to !=, greater than or equal to >=, less than or equal to <=– Logical operators: not !, and &&, or ||– Examples: hours > 40, wind > 20 && temperature < 309Exercise on Conditions• Use relational (<, >, <=, >=, ==, !=) and logical (!, &&, ||) operators to write conditions for the following:• An integer variable year is not evenly divisible by four• year % 4 != 0 or !( year % 4 == 0 )• A string variable status equals “single” and an integer variable dependents is 0• status == “single” && dependents == 010Review if Statements• Implementation of choice statements in most high-level languages uses an if statement• The C++ format isif (<condition>){<statements done if condition true>}11Review if-else Statements• Executes different statement blocks if condition is true or falseif (<condition>){<statements done if condition true>}else{<statements done if condition false>}<Next statement after one block done>12Writing if Statements Exercise• Define variable inc for “income”, deduct for “deductions” and ti “taxable income”• Taxable income is income minus deductions, but is never less than zero• Write code to compute taxable incomedouble ti = inc – deduct;if ( ti < 0 ){ ti = 0; }Multiple condition if statements February 23, 2006313Exercise• Write a program that delcares and reads a type double variable x, and determines if it is greater than zero– If x > 0 compute and print the natural logarithm using the log() function• Also print the value of x input by the user– Otherwise print an error message that you cannot compute log of a negative number14Exercise Solutiondouble x;cout << “Enter a value for x: “cin << x;if ( x > 0 )cout << “The natural log of “<< x << “ is “ << ln(x);elsecout << “Cannot compute log for “ << “negative input x = “<< x;15Multiple Conditions• Can have several choices– Example is an empirical function for y(x) with different equations for y used in different ranges of x• Structure to handle this is called if-else-if block• Allows initial if (and associated code) to be followed by several other statements like else if ( <new condition> )16if – else – if Structureif (<condition1>){<statements done if condition1 true>}else if (<condition2>){<statements done if condition2 true>}// Place additional conditions here// Continue on next chart17if – else – if Structure// Continued from previous chartelse if (<conditionN>){<statements done if conditionN true>}else // optional to have this final else{<statements done if all conditions false>}<Next statement after any block done>18if – else – if Operation• In this structure only one block of code – the code associated with the first true condition – is executed• Conditions are scanned from top to bot-tom until the first true condition is found• The code associated with that condition is executed and control is transferred to the first statement after the final block in the if – else – if structureMultiple condition if statements February 23, 2006419if – else – if Operation II• Because only one block of code – the code associated with the first true condition – is executed we have information at else-if conditions• Example, what do we know about x at the else-if statement in the following?if ( x < 0 )y = 0;else if ( x …If x < 0, we would set y = 0 and exit the if-else-if structure. If we get to the else-if statement we know x ≥ 020Example/Exercise• How do you program the following definition of an empirical function y(x)?• If x < 0, then y = 0.• If 0 ≤ x < 1, then y = 0.1 x• If 1 ≤ x < 10, then y = ( x – 0.8 ) / 2• If 10 ≤ x < 100, then y = 4.6 + 0.2(x –10)3• If x >= 100, then y = 1624.621Answer to Exercise• If x < 0, then y = 0.• If 0 ≤ x < 1, then y = 0.1 x• If 1 ≤ x < 10, then y = ( x – 0.8 ) / 2if ( x < 0 ){ y = 0; }else if ( x < 1 ) // ( x >= 0 && x < 1 )??{ y = 0.1 * x; }else if ( x < 10 ) // ( x >= 1 && x < 10 )??{ y = ( x – 0.8 ) / 2; }22Answer to Exercise II• If 10 ≤ x < 100, then y = 4.6 + 0.2(x –10)3• If x >= 100, then y = 1624.6else if ( x < 100 ) //( x >= 10 && x < 100 ){ y = 4.6 + 0.2 * pow( x – 10, 3); }else // else if ( x >= 100)??{ y = 1624.6; }23Another Exercise• A diagnostic test has the following result– Score ≥ 75 – take first course– 65 ≤ score < 75 take two-week prep course– Score < 65 take four-week prep course• Complete the following code, using if statements to print out the correct resultint score;cout << “Enter your score: “;cin >> score; 24Another Exercise Solutionif ( score >= 75 ) {cout << “Take college course”;}else if ( score >= 65 ) {cout << “Take two-week prep course”;}else {cout << “Take four-week prep course”;}Multiple condition if statements February 23, 2006525Use of if versus if-else-ifif ( a == 0 && b == 0 && c == 0 )x = 0;if ( a == 0 && b == 0 )x = 1;• What is difference between code above and code below?if ( a == 0 && b == 0 && c == 0 ){ x = 0;


View Full Document

CSUN COMP 106 - Programming

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