1 / 25

CSIS 113A

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.

verdi
Download Presentation

CSIS 113A

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  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 Glenn Stevenson CSIS 113A MSJC

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  18. Nested Ifs II Glenn Stevenson CSIS 113A MSJC

  19. Selecting one of several I Glenn Stevenson CSIS 113A MSJC

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

  21. Use of Boolean Expression Glenn Stevenson CSIS 113A MSJC

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

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

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

  25. Two Logical Problems Glenn Stevenson CSIS 113A MSJC

More Related