DOC PREVIEW
CSUN COMP 106 - Programming Choice Statements with Boolean (bool) Variables

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:

If statements and bool variables February 28, 20061Programming Choice Statements Programming Choice Statements with Boolean (with Boolean (boolbool) Variables) VariablesLarry CarettoComputer Science 106Computing in Engineering and ScienceFebruary 28, 20062Outline• Review last week– Simple if statements– if-else-if statements• Boolean (bool) variables• Programming with bool variables• Input validation• DeMorgan’s Laws• Nested if statements3Review 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>}4Review 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>5Review if – else – if Structureif (<condition1>){<statements done if condition1 true>}else if (<condition2>){<statements done if condition2 true>}// Place additional conditions here// Continue on next chart6Review if – else – if Structure II// 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>If statements and bool variables February 28, 200627Review if – 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 structure8Boolean (bool) Variables• Variables of type bool can hold values of expressions that are true or false• Can be used to hold results of relational expressions• Useful for testing complex conditions• Variable name can give meaning to condition• Use leap year algorithm as example9Leap Year Example• A year is a leap year if– It is evenly divisible by four– But is not evenly divisible by 100– Except years evenly divisible by 400 are leap years• Calendar programs need an algorithm to determine if a year is a leap year• What is condition for N to be evenly divisible by MN % M == 010Leap Year PseudocodeIf the year is not evenly divisible by fourThe year is not a leap year; quitBut, if the year is evenly divisible by 400The year is a leap year; quitBut, if the year is evenly divisible by 100The year is not a leap year; quitIf no statements above are trueThe year is a leap year11Example – Leap Yearbool leapif ( year % 4 != 0 ){ leap = false; }else if ( year % 400 == 0 ){ leap = true; }else if ( year % 100 == 0 ){ leap = false; }else{ leap = true; }12Simpler Leap Year Examplebool leap = year % 4 == 0 &&( year % 100 != 0 || year % 400 == 0 );• This single statement gives the same result as code on previous slide• Check for following test cases– 2000 is a leap year– 2008 is a leap year– 2006 is not a leap year– 2100 is not a leap yearIf statements and bool variables February 28, 2006313Data Validation• Programs should test input data to make sure they are reasonable– Lengths should be positive– Physical variables have known scales– Accounting systems expect transactions in certain ranges– Age as a variable is nonnegative and less than some arbitrary age (150 years?)• Can test maximum and minimum14Data Validation Exampleint xMin = -3, xMax = 22;cout << “Enter a value for x between “<< xMin << “ and “ << xMax;int x;cin >> x;bool badData = x < xMin || x > xMax;15Programming Data Validation• Will later show how to use loops• Keep sending error message and requesting new data while user enters incorrect data• With only if statements halt execution if user enters bad data– Can also use if–else–if to give user two or three tries to enter correct data then quit– Wait for looping to show easier way16Data Validation Example IIif ( badData ){cout << “Your entry for x = “<< x << “ is out of range”<< “\nMinimum x = “<< xMin<< “, Maximum x = “ << xMax<< “.\nProgram will halt.”;return EXIT_FAILURE} 17What is goodData• Contrast badData on last chart with goodData definition belowbool badData = x < xMin || x > xMax;bool goodData = x >= xMin && x <= xMax;• How are these conditions related?goodData = !badData;• General relations: DeMorgan’s Law18DeMorgan’s Laws• Have two bool variables, a and b, that can have values of true or false• Combinations of conditions for a and b satisfy both of the following• !(a && b) = !a || !b• !(a || b) = !a && !b• Can construct a truth table to verify this by looking at all possible conditionsIf statements and bool variables February 28, 2006419!(a && b) = !a || !b!(a && b)!a || !bfalsefalsetruefalsefalsetruetruetruea && b!b!abafalsefalsetruefalsefalsefalsetruetruetruetruetruetruetruefalsefalsefalsefalsetruetruetrue20!(a || b) = !a && !b!a && !b!(a || b)a || btruetruefalsefalsefalsetruetruefalsetruefalsefalsetruefalsefalsetruetrue!b!abatruetruetruefalsefalsefalsefalsetruefalsefalsefalsetrue21Review Data Validation• Apply DeMorgan’s Law to ValidationbadData = x < Min || x > Max;goodData = x >= Min && x <= Max;goodData = !badData;goodData = !(x < Min || x > Max);DeMorgan: !(a || b ) = !a && !bgoodData = !(x < Min) && !(x>Max)goodData = x >= Min && x <= MaxApplication of DeMorgan’s Law to goodData = !badData gives expected result for goodData22Exercise Background• An example of an iteration problem, shown below, computes x = A)()()1(22nnnxAxx +=+• Iterations continue until converged, defined as |x(n+1)–x(n)| ≤ε1+ ε2 |x(n+1)|• Note use of absolute values in computing convergence condition• Allowed error, ε1and ε2, set by user23Exercise Background II• What is meaning of ε1and ε2 in the condition |x(n+1)–x(n)| ≤ε1+ ε2 |x(n+1)|?• Absolute error is given by ε1– The error cannot be less than this regardless of the values of x(n+1)– Controls iterations for small x(n+1)• Relative error given by ε2– Governs when x is large• Combination accounts for range of x24Numerical Example• Use algorithm to find x = , with A = 2 and initial guess, x(0), = 1A)()()()()()()1(1222222nnnnnnnxxxxxAxx +=+=+=+5.05.1112112)0()1()0()0()1(=−=+=+=


View Full Document

CSUN COMP 106 - Programming Choice Statements with Boolean (bool) Variables

Download Programming Choice Statements with Boolean (bool) Variables
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 Choice Statements with Boolean (bool) Variables 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 Choice Statements with Boolean (bool) Variables 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?