Presentation is loading. Please wait.

Presentation is loading. Please wait.

Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

Similar presentations


Presentation on theme: "Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2."— Presentation transcript:

1 Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2

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

3 Glenn Stevenson CSIS 113A MSJC Relational Operators Used to create Boolean expressions –A statement that evaluates to true or false

4 Glenn Stevenson CSIS 113A MSJC Relational Operators II <Less than >Greater than <=Greater than or equals >=Less than or equals ==Equals !=Not Equals int x = 3, y = 4; bool z = x > y; z = x < y; z = x == y; z = x !=y; What is the value of z on each line?

5 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 VariablesComparisons int b = 2; int sh = 3300; double d = 2.34; float f = 2.34F; char c = 'A'; b > sh is false c = d is true

6 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

7 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.

8 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

9 Glenn Stevenson CSIS 113A MSJC An Example #include using namespace std; int main() { int number; cout > number; if(number == 50) cout << "50 is a big number to square! " << endl; cout << number << " squared is " << number * number << endl; return 0; }

10 Glenn Stevenson CSIS 113A MSJC If / else If has an optional else –It cannot stand alone Must be preceded by an if statement

11 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

12 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; }

13 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; }

14 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; }

15 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;

16 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;

17 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;

18 Glenn Stevenson CSIS 113A MSJC Nested Ifs II What does this print when... Amount is 9? When Amount is 100? if (Amount > 10) if (Amount < 100) cout << "Between 1 and 99\n"); else cout << "Less than 10\n"); // Is it? Rule: every else is matched with the last unmatched if statement “Dangling else” Use braces to correct

19 Glenn Stevenson CSIS 113A MSJC Selecting one of several I Selecting one of n if (x == 1) { // action for 1 } else { if (x == 2) { // action for 2 } else { if (x == 3) { … }

20 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

21 Glenn Stevenson CSIS 113A MSJC Use of Boolean Expression Nesting also combines boolean expressions if statement can appear in body of another if This statement uses && if ( balance > 1000 && age > 21 ) statement; This uses a nested if if ( balance > 1000 ) if ( age > 21 ) statement;

22 Glenn Stevenson CSIS 113A MSJC Short Circuit Evaluation Precedence –Logical AND (&&) is higher than OR (||) –What is ( 10 8 && 3 > 5 ) ? if ( (a != 0) && ( b / a > 12 )) if ( (a > 10) || (b++ > 7)) Evaluation stops when outcome is determined

23 Glenn Stevenson CSIS 113A MSJC Ladder-Style if...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;

24 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!!

25 Glenn Stevenson CSIS 113A MSJC Two Logical Problems The impossible condition if (age > 65 && age < 21) … Solution?Change order if you are trying to bound valueChange AND to ORThe unavoidable condition if (age > 21 || age < 65) Solution? Change OR to AND


Download ppt "Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2."

Similar presentations


Ads by Google