250 likes | 460 Views
CSIS 113A. Lecture 2. The bool type. Can have only hold two separate values true, false. bool empty = true; bool full; full = false ;. Watch your case! False and false are different. Relational Operators. Used to create Boolean expressions A statement that evaluates to true or false.
E N D
CSIS 113A Lecture 2 Glenn Stevenson CSIS 113A MSJC
The bool type Can have only hold two separate values true, false bool empty = true;bool full; full = false; • Watch your case! • False and false are different Glenn Stevenson CSIS 113A MSJC
Relational Operators Used to create Boolean expressions A statement that evaluates to true or false Glenn Stevenson CSIS 113A MSJC
Relational Operators II • What is the value of z on each line? int x = 3, y = 4;bool z = x > y;z = x < y;z = x == y;z = x !=y; Glenn Stevenson CSIS 113A MSJC
Primitive Relations Each relational operator require 2 primitive operands The result is a Boolean value Only works with comparable primitive types Most types are comparable Normally don’t need to worry about mixed type comparisons Glenn Stevenson CSIS 113A MSJC
Floating Point Relations General Rule Never compare floating point operands using == or != (.1 * 10.0) == 1.0; // C++ considers this to be true • What about this: (.1+.1+.1+.1+.1+.1+.1+.1+.1+.1) == 1.0 Glenn Stevenson CSIS 113A MSJC
Introduction to selection The relational operators, and the Boolean values that they produce, are important They allow us to implement selection . Acts like a “highway divider” in your code. Glenn Stevenson CSIS 113A MSJC
If Statement If is considered a block of code. So why doesn’t it have braces? If you only want to execute one statement as a result of the if you don’t need braces Condition should be derived from relational operators Glenn Stevenson CSIS 113A MSJC
An Example #include <iostream> using namespace std; int main(){int number; cout << "Enter a number and I will square it for you " << endl; cin >> number; if(number == 50) cout << "50 is a big number to square! " << endl; cout << number << " squared is " << number * number << endl; return 0; } Glenn Stevenson CSIS 113A MSJC
If / else If has an optional else It cannot stand alone Must be preceded by an if statement Glenn Stevenson CSIS 113A MSJC
Multiple statements When you want to execute multiple statements as a result of the if condition Must surround code to execute by braces Glenn Stevenson CSIS 113A MSJC
Indentation Styles I 3 acceptable styles 1. opening brace on same line as if Can be difficult to spot missing braces with this style if (amountSold <= 35000) { bonusPct = .035; bonusAmt = amountSold * bonusPct; } else { bonusPct = .075; bonusAmt = amountSold * bonusPct + 100; } Glenn Stevenson CSIS 113A MSJC
Indentation Style II Style I prefer Braces are lined up on top of each other with code indented Make seeing missing braces easy if (amountSold <= 35000) { bonusPct = .035; bonusAmt = amountSold * bonusPct; } else { bonusPct = .075; bonusAmt = amountSold * bonusPct + 100; } Glenn Stevenson CSIS 113A MSJC
Indentation Styles 3 Variation of number 2 Again, braces don’t stand so finding a missing one could again be a problem if (amountSold <= 35000) { bonusPct = .035; bonusAmt = amountSold * bonusPct; } else { bonusPct = .075; bonusAmt = amountSold * bonusPct + 100; } Glenn Stevenson CSIS 113A MSJC
Why use braces? Required if multiple lines of code are used within and if or an if / else What is wrong with the following code? bonusAmt = 0; if (amountSold <= 35000) bonusPct = .035; else bonusPct = .075; bonusAmt+= 100; bonusAmt += amountSold * bonusPct; Glenn Stevenson CSIS 113A MSJC
Rule of thumb Beginning programmers should always use braces Even if there is only one statement to execute It is ok to omit them if you are putting everything on a single line: if (amt < 100) cost = .23; Glenn Stevenson CSIS 113A MSJC
Nested ifs What is a nested if? One if (or if-else) appears as the "body" of another • if ( x == 3 ) if ( z == 4 ) y = 3; else y = 4;else if ( z == 4 ) y = 5; else y = 6; Glenn Stevenson CSIS 113A MSJC
Nested Ifs II Glenn Stevenson CSIS 113A MSJC
Selecting one of several I Glenn Stevenson CSIS 113A MSJC
Selecting one of several II Same problem using if-else-if if (x == 1) { // action for 1} else if (x == 2) { // action for 2} else if (x == 3) { …} Same code, just reformatted Glenn Stevenson CSIS 113A MSJC
Use of Boolean Expression Glenn Stevenson CSIS 113A MSJC
Short Circuit Evaluation Precedence Logical AND (&&) is higher than OR (||) What is( 10 < 15 || 5 > 8 && 3 > 5 ) ? • if ( (a != 0) && ( b / a > 12 )) • if ( (a > 10) || (b++ > 7)) Glenn Stevenson CSIS 113A MSJC
Ladder-Styleif...else Called "ladders" [if-else-if] More easily understood than traditional nesting Note if-else-if statements are interdependentMust often be careful to place in correct order if (age <= 7) fee = 8.00;else if (age <= 12) fee = 10.50;else fee = 21.75; Glenn Stevenson CSIS 113A MSJC
Phantom Semicolon Common error, Remember, decisions are blocks They are terminated by an ending brace That is unless you have one statement to execute if (employeesInBuilding == 0); { demolishBuilding(); } UhOh, this is a big problem!! Glenn Stevenson CSIS 113A MSJC
Two Logical Problems Glenn Stevenson CSIS 113A MSJC