Presentation is loading. Please wait.

Presentation is loading. Please wait.

Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X1 Chapter 3 Control Statements.

Similar presentations


Presentation on theme: "Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X1 Chapter 3 Control Statements."— Presentation transcript:

1 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X1 Chapter 3 Control Statements

2 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 2 Comparison Operators

3 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 3 Type bool expression: a combination of values, variables, operators and functions which, when evaluated, produce a value. examples: 2 + 2, pow(2, 2), ++i, k % 2 = = 0 Boolean expression (a.k.a. condition): an expression that results in a Boolean value, that is, true (1) or false (0). examples: 5 > 3, k % 2 = = 0, isupper(‘a’)  All conditions are expressions but not all expressions are conditions

4 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 4 Simple if Statements if (booleanExpression) { statement(s); } if (radius >= 0) { area = radius * radius * PI; cout << "The area for the circle of " << " radius " << radius << " is " << area; }

5 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 5 Examples Listing 3.1 gives a program that checks whether a number is even or odd. The program prompts the user to enter an integer (line 9) and displays “number is even” if it is even (lines 11-12) and “number is odd” if it is odd (lines 14-15). TestBoolean Run

6 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 6 Note

7 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 7 Caution Adding a semicolon at the end of an if clause is a common mistake. This mistake is hard to find, because it is not a compilation error or a runtime error, it is a logic error. This error often occurs when you use the next-line block style.

8 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 8 Boolean Operators Operator Name ! not && and || or

9 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 9 Truth Table for Operator &&

10 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 10 Truth Table for Operator ||

11 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 11 Examples Listing 3.3 gives a program that checks whether a number is divisible by 2 and 3, whether a number is divisible by 2 or 3, and whether a number is divisible by 2 or 3 but not both: TestBooleanOperators Run

12 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 12 Short-Circuit Operator When evaluating p1 && p2, C++ first evaluates p1 and then evaluates p2 if p1 is true; if p1 is false, it does not evaluate p2. When evaluating p1 || p2, C++ first evaluates p1 and then evaluates p2 if p1 is false; if p1 is true, it does not evaluate p2. Therefore, && is referred to as the conditional or short-circuit AND operator, and || is referred to as the conditional or short-circuit OR operator.

13 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 13 The if...else Statement if (booleanExpression) { statement(s)-for-the-true-case; } else { statement(s)-for-the-false-case; }

14 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 14 Examples Listing 3.4 presents a program that lets the user enter a year and checks whether it is a leap year. A year is a leap year if it is divisible by 4 but not by 100 or if it is divisible by 400. So you can use the following Boolean expression to check whether a year is a leap year: (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) LeapYear Run

15 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 15 Nested if Statements if (i > k) { if (j > k) cout << "i and j are greater than k"; } else cout << "i is less than or equal to k";

16 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 16 Multiple Alternative if Statements

17 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 17 Note The else clause matches the most recent if clause in the same block.

18 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 18 Note, cont. Nothing is printed from the preceding statement. To force the else clause to match the first if clause, you must add a pair of braces: int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) cout << "A"; } else cout << "B"; This statement prints B.

19 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 19 TIP

20 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 20 CAUTION

21 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 21 Conditional Operator if (x > 0) y = 1 else y = -1; is equivalent to y = (x > 0) ? 1 : -1; (booleanExpression) ? expression1 : expression2 Ternary operator Binary operator Unary operator

22 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 22 Conditional Operator cout << ((num % 2 == 0) ? "num is even" : "num is odd");

23 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 23 Conditional Operator, cont. (booleanExp) ? exp1 : exp2

24 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 24 Operator Precedence How to evaluate 3 + 4 * 4 > 5 * (4 + 3) – 1?

25 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 25 Operator Precedence F var++, var--  +, - (Unary plus and minus), ++var, --var F (type) Casting F ! (Not)  *, /, % (Multiplication, division, and remainder)  +, - (Binary addition and subtraction) , >= (Comparison)  ==, !=; (Equality) F && (Conditional AND) Short-circuit AND F || (Conditional OR) Short-circuit OR  =, +=, -=, *=, /=, %= (Assignment operator)

26 Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X 26 Operator Precedence Simplified When in doubt, use parentheses version 1: if (ch >= ‘A’ && ch <= ‘Z’) cout << “UC”; version 2: if ((ch >= ‘A’) && (ch <= ‘Z’)) cout << “UC”;


Download ppt "Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved. 013225445X1 Chapter 3 Control Statements."

Similar presentations


Ads by Google