asking questions - conditionals objectives learning the types of questions c++ can ask. writing...

9
Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators logical expressions If statement if with true part if with true and false parts nested if

Upload: clinton-cunningham

Post on 18-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators

Asking Questions - Conditionals

Objectives

Learning the types of questions c++ can ask.

Writing simple questions in c++.

logical operators

relational operators

logical expressions

If statement

if with true part

if with true and false parts

nested if

Page 2: Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators

What Kinds of Questions Can I Ask?

Is the sky blue? What is the meaning of Life?

Unfortunately..It's not that easy!

Then WHAT?

1. Are two values

= = equal (BE CAREFUL!)

!= not equal

> greater

>= greater than or equal

< less than

<= less than or equal

Examples: a > 4 , b = = 3 + d

2. Using those for more complicated

&& and

| | or

Examples:

( a > b ) | | ( c <=d)

(a > 3) && ( a < 12) means 3<a<12

Page 3: Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators

RELATIONAL

They're the easiest to learn .. NATURAL.

expression RELATIONAL_OP expression

3 > a

2.3 + b = = c is ok but (2.3 + b) = = c is better

( b * 2 + d) ! = (23 - x)

Result is either

1 (true)

or

0 (false)

Page 4: Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators

andA logical operator written &&.

Also returns 1(true) or 0(false).

BUT it expects inputs to be 1(true) or 0(false).

Both inputs must be true for the result to be true.

ENGLISH

Are you awake and in cs230?

Answer may be FALSE!

Are you alive and in cs230?

Answer is TRUE!

Truth Table for && (and) Fig. 2.28, pg 107

expression1 expression2 expression1 && expression 2

false (0) false (0) false (0)

false (0) true (1) false (0)

true (1) false (0) false (0)

true (1) true (1) true (1)

Page 5: Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators

or

A logical operator written | |.

Also returns 1(true) or 0(false).

It expects inputs to be 1(true) or 0(false).

If either input or both are true the result is true.

ENGLISH

Are you a male or female?

Answer TRUE for everyone!

Are you in Richmond or in Newport News?

Answer is TRUE!

Truth Table for | | (or) Fig. 2.29, pg 107

expression1 expression2 expression1 || expression 2

false (0) false (0) false (0)

false (0) true (1) true (1)

true (1) false (0) true (1)

true (1) true (1) true (1)

Page 6: Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators

Some Examples of Logical Expressions

w x y z

10 12 4 34

logical expression RESULT

w > 4 1 (t)

w > (y * 2) 1 (t)

(y - z) > (11 - w) 0 (f)

(w > 1) && ( x < 1) 0 (f)

(x > w) | | ( y > z) 1 (t)

Be careful about and/or/= =. Try this:

answer true for all 21 and 22 year olds

(age = 21) and (age = 22)

should be

(age = = 21) || (age = = 22)

English can be misleading. We associate reasonable interpretations.

1.Wrong = / ==2. and instead of or3. English instead of ||

Page 7: Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators

if Statements

One of the numerous places logical expressions are used.

A fundamental program building-block.

"if" allows us to ask questions.

if (logical expression)

do_this_when_true;

Examples:

if (radius < 0.0 )

cout << "Illegal value for radius";

// more than one statement in true part

if(radius < 0.0 ) {

cout << "Illegal value for radius";

radius = 0.0;

}

// p.80 example

if (result == 1)

passes = passes +1;

else

failures = failures +1;

Page 8: Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators

Nested ifUsed in mutually exclusive (only one) case

Examples: either freshman, soph, junior, senior

either grade is A, B, C, D, F, I.

either chevy, ford, chrysler

which income tax bracket

c++ Example: p. 65 (Chapter 2) book only has body of function

// function to return correct letter grade

// based on numerical test score

char letter_grade( float score)

{

if (score >=90)

return 'A';

else if (score >=80)

return 'B';

else if (score >=70)

return 'C';

else if (score >=60)

return 'D';

else

return 'F';

}

New type

Score(85.3)

Lettergrade (B)

Page 9: Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators

Conclusion

Understanding the operation of if is not too bad.

Understanding how to use it will take practice.

Most programs will use them.

Labs will review them.

When I lecture on program design,

this will be addressed.

For Now:

Ignore switch and break statement.

Practice exercises to understand

how to read if

and

how to read/write logical expressions.