Download presentation
Presentation is loading. Please wait.
1
Decisions, decisions, decisions
(it’s all in the bool!!)
2
Review – Boolean expressions
Boolean expressions are created using relational and logical operators There are six relational operators – ==, >, <, >=, <=, != There are four logical operators (logical connectors) but we mostly use three &&, ||, !, ^
3
Assume the following variable declarations:
int var1 = 10; int var2 = 15; bool result; result = (var1 == 10) ; // is a true assertion so result gets a true (non-zero) value Result = (var2 == 10); // is NOT a true assertion so result gets a false (0) value
4
Assume the following variable declarations:
int var1 = 10; int var2 = 15; bool result; result = (var1 == 10) && (var1 != var2); // value of result ?? result = (var2 < var1) || (var2 != var1); // value of result?? result = (var2 > var1) ! (var2 == 25); // value of result??
5
Use of Boolean expressions to control operations
Decision structures If (condition) { true statements } else false statements
6
While structure while ( condition ) true statement; or {
true statements; }
7
Three Considerations when using Iterative control structures
Initialize the control variable – prior to entering the control structure the controlling variable MUST be initialized (given a starting value) Test the controlling variable in the condition – if not sure use a constant for the limit then substitute a limiting variable Modify the control variable within the controlled block of code – otherwise you may generate an “infinite loop”.
8
Some examples: int limit = 10; int ctr = 0; while( ctr < 10 ) {
cout << “counter = “ << ctr << endl; ctr++; } OR cout << “counter = “ << ctr++ << endl;
9
} while( ctr < limit );
The do – while – the condition (test) is after the execution of the block. Example: do { cout << “counter = “ << ctr << endl; ctr++; } while( ctr < limit ); Note the test is after the block and there is a semi-colon at the end of the condition.
10
The BIG 3 Summary and Comparison
11
And the switch() Used for multiple tests
Sometimes used instead of nested if – else Uses an ordinal expression (variable) instead of a Boolean Uses four terms (or words) – in no special order – switch() - starts the switch structure and defines the ordinal statement case – identifies the beginning of the programming sequence to be executed break – terminates the execution of the local code block default –executes if no match is found
12
switch() example switch(resp.at(0)) do {
cout << "Enter 1, 2, 3 or <E>xit: "; getline(cin,resp); switch(resp.at(0)) { // start of switch block case '1': cout << "A one was entered" << endl; break; case '2': cout << "A two was typed in" << endl; case '3': cout << "A three was encountered" << endl; case 'E': case 'e': default: cout << "ERROR - Will Robinson" << endl; cout << "Enter 1, 2, 3 or <E>xit: "; getline(cin, resp); }while((resp.at(0)!= '1')&&(resp.at(0)!= '2')&&(resp.at(0)!= '3')&&(resp.at(0)!= 'E')&&(resp.at(0)!= 'e')); } }while( (resp.at(0)!= 'E')&&(resp.at(0)!= 'e') );
13
Check out Rock, Paper, Scissors
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.