06 - Selection - Engineering 101 Quote of the Day The...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Engineering 101 Quote of the Day The essence of knowledge is, having it, to apply it; not having it, to confess your ignorance. - Confucius Selection Selection So far we have discussed sequence and iteration. iteration. Selection allows the execution of a portion of your code depending on some condition. Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: if (condition) statement_t else statement_f Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: The condition if (condition) (condition) statement_t else statement_f is similar to the while condition. It evaluates to true or false. Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: If the condition if (condition) (condition) statement_t else statement_f evaluates to true then statement_t is executed. 1 Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: If the condition if (condition) (condition) statement_t else statement_f evaluates to false then statement_f is executed. Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: if (condition) (condition) statement_t else statement_f The statements can either be single line expressions or multiple expressions enclosed in braces. Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: if (condition) (condition) statement_t else statement_f Selection Allows the selective execution of a portion of your code depending on some condition. Syntax in C++: if (condition1) (condition1) statement_t1 else if (condition2) (condition2) statement_t2 else statement_f The else can be entirely removed. Or you can add more else conditions in the form of `else if'. Simple Example int x; cin >> x; if (x % 2 == 0) cout << x << "is an even number" << endl; else cout << x << "is an odd number" << endl; Simple Example int x; x 14 cin >> x; if (x % 2 == 0) cout << x << "is an even number" << endl; else cout << x << "is an odd number" << endl; 2 Simple Example int x; x 14 cin >> x; if (14 % 2 == 0) (14 0) cout << x << "is an even number" << endl; else cout << x << "is an odd number" << endl; Simple Example int x; x 14 cin >> x; if (0 == 0) (0 0) cout << x << "is an even number" << endl; else cout << x << "is an odd number" << endl; Simple Example int x; x 14 cin >> x; if (x % 2 == 0) 14 is an even number cout << x << "is an even number" << endl; else cout << x << "is an odd number" << endl; Simple Example int x; x 27 cin >> x; if (x % 2 == 0) cout << x << "is an even number" << endl; else cout << x << "is an odd number" << endl; Simple Example int x; x 27 cin >> x; if (27 % 2 == 0) (27 0) cout << x << "is an even number" << endl; else cout << x << "is an odd number" << endl; Simple Example int x; x 27 cin >> x; if (1 == 0) (1 0) cout << x << "is an even number" << endl; else cout << x << "is an odd number" << endl; 3 Simple Example int x; x 27 cin >> x; if (x % 2 == 0) 27 is an odd number cout << x << "is an even number" << endl; else cout << x << "is an odd number" << endl; Multiple Expressions int x; cin >> x; if (x % 2 == 0){ cout << x << "is an even number" << endl; x = x / 2; } else cout << x << "is an odd number" << endl; More complex example Computing your taxes If your taxable income is over But not over Your tax is Plus Of the amount over Taxes double taxes(double income){ double tax; if (income<=21450.0) tax = 0.15*income; return tax; } If your taxable income is over But not over Your tax is Plus Of the amount over $0 $21,450 $51,900 $21,450 $51,900 $0 $3,217.50 $11,734.50 15% 28% 31% $0 $21,450 $51,900 Let's translate this into a program. $0 $21,450 $51,900 $21,450 $51,900 $0 $3,217.50 $11,734.50 15% 28% 31% $0 $21,450 $51,900 Taxes double taxes(double income){ double tax; if (income<=21450.0) tax = 0.15*income; else { if (income <= 51900.0) tax = 3217.5 + 0.28*(income-21450.0); 0.28*(income} return tax; If your taxable But not over Your tax is Plus Of the amount } income is over over $0 $21,450 $51,900 $21,450 $51,900 $0 $3,217.50 $11,734.50 15% 28% 31% $0 $21,450 $51,900 Taxes double taxes(double income){ double tax; if (income<=21450.0) tax = 0.15*income; else { if (income <= 51900.0) tax = 3217.5 + 0.28*(income-21450.0); 0.28*(incomeelse tax = 11734.5 + 0.31*(income-51900.0); 0.31*(income} return tax; } 4 Practice with Selection You are at a movie theater: Regular price is $8 Senior Citizens, age 65 or older, $5.50 Students (student == true) $6 Write a program that will assign to an identifier called price the correct ticket price given a customer's age and student status. Regular price is $8 Senior Citizens, age 65 or older, $5.50 Students (student == true) $6 if (age < 65) price = 8.00; else if (student == true) price = 6.00; else price = 5.50; if (age >= 65) price = 5.50; else if (student == true) price = 6.00; else price = 8.00; Write a program that will assign to an identifier called price the correct ticket price given a customer's age and student status. if (age < 65) price = 8.00; if (student == true) price = 6.00; else price = 5.50; if (age >= 65) price = 5.50; if (student == true) price = 6.00; else price = 8.00; Something is wrong with this code int rock=1, scissor=2, paper=3; int a, b; cin >> a >> b; if (a == b) cout << "tie" << endl; else if (a == rock) if (b == scissor) cout << "a wins" << endl; else if (b == paper) cout << "b wins" << endl; else if (a == scissor) if (b == paper) cout << "a wins" << endl; else if (b == rock) cout << "b wins" << endl; else if (a == paper) if (b == rock) cout << "a wins" << endl; else if (b == scissor) cout << "b wins" << endl; What Result Do You Get When... a = 1 (rock) b = 2 (scissor) 1- a wins 2- b wins 3- neither int rock=1, scissor=2, paper=3, a, b; cin >> a >> b; if (a == b) cout << "tie" << endl; else if (a == rock) if (b == scissor) cout << "a wins" << endl; else if (b == paper) cout << "b wins" << endl; else if (a == scissor) if (b == paper) cout << "a wins" << endl; else if (b == rock) cout << "b wins" << endl; else if (a == paper) if (b == rock) cout << "a wins" << endl; else if (b == scissor) cout << "b wins" << endl; What Result Do You Get When... a = 3 (paper) b = 1 (rock) 1- a wins 2- b wins 3- neither int rock=1, scissor=2, paper=3, a, b; cin >> a >> b; if (a == b) cout << "tie" << endl; else if (a == rock) if (b == scissor) cout << "a wins" << endl; else if (b == paper) cout << "b wins" << endl; else if (a == scissor) if (b == paper) cout << "a wins" << endl; else if (b == rock) cout << "b wins" << endl; else if (a == paper) if (b == rock) cout << "a wins" << endl; else if (b == scissor) cout << "b wins" << endl; What is wrong with the code? int rock=1, scissor=2, paper=3; int a, b; cin >> a >> b; if (a == b) cout << "tie" << endl; else if (a == rock) if (b == scissor) cout << "a wins" << endl; else if (b == paper) cout << "b wins" << endl; else if (a == scissor) if (b == paper) cout << "a wins" << endl; else if (b == rock) cout << "b wins" << endl; else if (a == paper) if (b == rock) cout << "a wins" << endl; else if (b == scissor) cout << "b wins" << endl; The "else" is always associated with the most recent "if" 5 What is wrong with the code? int rock=1, scissor=2, paper=3; int a, b; cin >> a >> b; if (a == b) cout << "tie" << endl; else if (a == rock) if (b == scissor) cout << "a wins" << endl; else if (b == paper) cout << "b wins" << endl; What Result Do You Get When... a = 2 (scissor) b = 3 (paper) 1- a wins 2- b wins 3- neither int rock=1, scissor=2, paper=3, a, b; cin >> a >> b; if (a == b) cout << "tie" << endl; else if (a == rock) if (b == scissor) cout << "a wins" << endl; else if (b == paper) cout << "b wins" << endl; else if (a == scissor) if (b == paper) cout << "a wins" << endl; else if (b == rock) cout << "b wins" << endl; else if (a == paper) if (b == rock) cout << "a wins" << endl; else if (b == scissor) cout << "b wins" << endl; The "else" is always associated with the most recent unpaired "if" else if (a == scissor) if (b == paper) cout << "a wins" << endl; else if (b == rock) cout << "b wins" << endl; else if (a == paper) if (b == rock) cout << "a wins" << endl; else if (b == scissor) cout << "b wins" << endl; What is wrong with the code? int rock=1, scissor=2, paper=3; int a, b; What is wrong with the code? int rock=1, scissor=2, paper=3; int a, b; How do we fix this? How do we fix this? cin >> a >> b; if (a == b) cout << "tie" << endl; else if (a == rock) if (b == scissor) cout << "a wins" << endl; else if (b == paper) cout << "b wins" << endl; else if (a == scissor) if (b == paper) cout << "a wins" << endl; else if (b == rock) cout << "b wins" << endl; else if (a == paper) if (b == rock) cout << "a wins" << endl; else if (b == scissor) cout << "b wins" << endl; cin >> a >> b; Idea 1: braces if (a == b) cout << "tie" << endl; else if (a == rock) { if (b == scissor) cout << "a wins" << endl; else if (b == paper) cout << "b wins" << endl; } else if (a == scissor) { if (b == paper) cout << "a wins" << endl; else if (b == rock) cout << "b wins" << endl; } else if (a == paper) { if (b == rock) cout << "a wins" << endl; else if (b == scissor) cout << "b wins" << endl; } What is wrong with the code? int rock=1, scissor=2, paper=3; int a, b; What is wrong with the code? int rock=1, scissor=2, paper=3; int a, b; How do we fix this? How do we fix this? Idea 3: think of a simpler cin >> a >> b; Idea 2: remove if (a == b) cout << "tie" << endl; unnecessary conditions else if (a == rock) if (b == scissor) cout << "a wins" << endl; else cout << "b wins" << endl; else if (a == scissor) if (b == paper) cout << "a wins" << endl; else cout << "b wins" << endl; else if (a == paper) if (b == rock) cout << "a wins" << endl; else cout << "b wins" << endl; cin >> a >> b; expression if (a == b) cout << "tie" << endl; else if ((b a) == 1 or (b a) == -2) cout << "a wins" << endl; else cout << "b wins" << endl; a rock (1) scissor (2) paper (3) b scissor(2) paper (3) rock (1) b- a 1 1 -2 6 Selection plus Iteration Often solving a problem involves using a combination of selection plus iteration. What does this procedure do? void thisproc(int N){ thisproc( int j = 1; while (j <= sqrt(static_cast<double>(N))) { sqrt(static_cast<double>(N))) if (N%j == 0) cout << j << "x" << N/j << " "; j++; } cout << endl; endl; return; } Examples are coming... What is the result of calling thisproc(8) ? 131x8 2x4 4x2 8x1 8x1 4x2 241x8 2x4 8x1 void thisproc(int N){ int j = 1; while (j <= sqrt(static_cast<double>(N))) { if (N%j == 0) cout << j << "x" << N/j << " "; j++; } cout << endl; return; } What is the result of calling thisproc(9) ? 131x9 2x4.5 3x3 1x9 3x3 2- 9x1 4.5x2 3x3 4- 9x1 3x3 Which procedure will.... Request a single integer number. It then asks the user to input a number. If the number guessed is too high print out "guess lower" and get another number If the number guessed is too low print out "guess higher" and get another number If the number guessed is correct let the user know this is the number and tell how many times the user had to guess. 1 2 void thisproc(int N){ int j = 1; while (j <= sqrt(static_cast<double>(N))) { if (N%j == 0) cout << j << "x" << N/j << " "; j++; } cout << endl; return; } 3 Using Selection To Check Program Status One of the most important uses of selection is to check the status of a command or action cout << "Enter a positive number" << endl; cin >> n; exit() is if (n <= 0){ defined in the cout << n << " is not positive" << endl; cstdlib library. exit(EXIT_FAILURE); It terminates } the program. cout << "The inverse square root is " << 1.0/sqrt(static_cast<double>(n))<<endl; Using Selection To Check Program Status Streams are a particular case in point since whenever you try to open or read from a file you don't know if the file might not exist or be finished. ifstream infile("myfile.data"); if( infile.fail() ){ cerr << "Problem with file" << endl; exit(EXIT_FAILURE); stream.fail() can be } used with any stream infile >> x >> y >> z; and returns true if the last action failed. 7 fail() can also be used to read files of arbitrary length int x, n=0; ifstream infile("myfile.data"); infile >> x; while (! infile.fail() ){ cout << x; n = n + 1; infile >> x; } cout << "read in " << n << " integers." << endl; Switch Statements A switch statement is used to decide between a number of cases switch (integer_expression){ (integer_expression){ case value: expression value: case value: expression value: default: expression } The integer expression is evaluated and the thread of execution starts from the matching case. If none matches it starts from the default. Switch Statements switch (weekday){ case 1: cout << "Monday"; break; break; case 2: cout << "Tuesday"; break; break; case 3: cout << "Wednesday"; break; break; case 4: cout << "Thursday"; break; break; case 5: cout << "Friday"; break; break; default: cout << "Weekend"; } breaks are needed. Otherwise switch drops through to next expression. Next Lecture Predicates and Intro to Loops 8 ...
View Full Document

This note was uploaded on 05/04/2010 for the course ENGIN 101 taught by Professor Jeffringenberg during the Spring '07 term at University of Michigan.

Ask a homework question - tutors are online