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

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

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

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

Glenn Stevenson CSIS 113A MSJC

CSIS 113A

Lecture 2

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

Page 3: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

Glenn Stevenson CSIS 113A MSJC

Relational Operators

• Used to create Boolean expressions– A statement that evaluates to true or false

Page 4: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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?

Page 5: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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

Variables Comparisonsint b = 2; int sh = 3300; double d = 2.34; float f = 2.34F; char c = 'A';

b > sh is false c < b is false f == d is false f < d is true c >= d is true

Page 6: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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

Page 7: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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.

Page 8: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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

Page 9: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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

Page 10: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

Glenn Stevenson CSIS 113A MSJC

If / else

• If has an optional else– It cannot stand alone

• Must be preceded by an if statement

Page 11: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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

Page 12: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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

Page 13: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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

Page 14: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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

Page 15: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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;

Page 16: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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;

Page 17: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

Glenn Stevenson CSIS 113A MSJC

Nested ifs

• What is a nested if?– One if (or if-else) appears as the "body"

of anotherif ( x == 3 ) if ( z == 4 ) y = 3; else y = 4;else if ( z == 4 ) y = 5; else y = 6;

Page 18: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

Glenn Stevenson CSIS 113A MSJC

Nested Ifs II

Page 19: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

Glenn Stevenson CSIS 113A MSJC

Selecting one of several I

Page 20: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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

Page 21: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

Glenn Stevenson CSIS 113A MSJC

Use of Boolean Expression

Page 22: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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

Page 23: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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;

Page 24: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

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

Page 25: Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

Glenn Stevenson CSIS 113A MSJC

Two Logical Problems