Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators."— Presentation transcript:

1 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

2 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

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

4 and A 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)

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

6 Some Examples of Logical Expressions wxyz 1012434 logical expressionRESULT w > 41 (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 or 3. English instead of ||

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

8 Nested if Used 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) Letter grade (B)

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


Download ppt "Asking Questions - Conditionals Objectives Learning the types of questions c++ can ask. Writing simple questions in c++. logical operators relational operators."

Similar presentations


Ads by Google