coen 244 - 4 - control statement

27
2008 Pearson Education, Inc. All rights rese 1 4 Control Statements

Upload: maxime-pauloin

Post on 18-Feb-2016

222 views

Category:

Documents


0 download

DESCRIPTION

COEN 244

TRANSCRIPT

Page 1: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

1

4Control

Statements

Page 2: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

2

Control Structures- Only three control structures are needed to implement any program,

• Sequence structure Program statements are executed sequentially by default

• Selection structures if, if…else, switch

• Repetition structures while, do…while, for

– Structures are combined in one of two ways• Control statement stacking

Connect exit point of one to entry point of the next• Control statement nesting

Page 3: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

3

4.5 if Selection Statement

• Single Selection statement– Pseudocode

• If student’s grade is greater than or equal to 60 print “Passed”

• if ( grade >= 60 )

cout << "Passed";

Page 4: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

4

4.6 if…else Double-Selection Statement

• Pseudocode– If student’s grade is greater than or equal to 60

print “Passed”Else print “Failed”

• C++ code– if ( grade >= 60 )

cout << "Passed";else cout << "Failed";

Page 5: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

5

4.6 if…else Double-Selection Statement (Cont.)

• Ternary conditional operator (?:)– Three arguments (condition, value if true, value if false)

• Code could be written:– cout << ( grade >= 60 ? “Passed” : “Failed” );

Condition Value if true Value if false

Page 6: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

6

4.6 if…else Double-Selection Statement (Cont.)

• Nested if…else statements– One inside another, test for multiple cases – Once a condition met, other statements are skipped– Example

• If student’s grade is greater than or equal to 90 Print “A” Else

If student’s grade is greater than or equal to 80 Print “B”

Else If student’s grade is greater than or equal to 70

Print “C” Else If student’s grade is greater than or equal to 60 Print “D”

Else Print “F”

Page 7: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

7

4.6 if…else Double-Selection Statement (Cont.)

• Nested if…else statements (Cont.)

– Written In C++• if ( studentGrade >= 90 )

cout << "A";else if (studentGrade >= 80 ) cout << "B"; else if (studentGrade >= 70 ) cout << "C"; else if ( studentGrade >= 60 ) cout << "D"; else cout << "F";

Page 8: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

8

4.6 if…else Double-Selection Statement (Cont.)

• Nested if…else statements (Cont.)

– Written In C++ (indented differently)• if ( studentGrade >= 90 )

cout << "A";else if (studentGrade >= 80 ) cout << "B";else if (studentGrade >= 70 ) cout << "C"; else if ( studentGrade >= 60 ) cout << "D";else cout << "F";

Page 9: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

9

4.6 if…else Double-Selection Statement (Cont.)

• Dangling-else problem– Compiler associates else with the immediately

preceding if– Example

• if ( x > 5 ) if ( y > 5 ) cout << "x and y are > 5";else cout << "x is <= 5";

– Compiler interprets as• if ( x > 5 )

if ( y > 5 ) cout << "x and y are > 5"; else cout << "x is <= 5”;// output is incorrect

Page 10: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

10

switch Multiple-Selection Statement

•switch statement– Used for multiple selections– Tests a variable or expression

• Compared against constant integral expressions to decide on action to take

– Any combination of character constants and integer constants that evaluates to a constant integer value

Page 11: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

Example Switch statement 54 char grade; // grade entered by user55 cout << "Enter the letter grades." << endl56 cin >> grade ;57 // determine which grade was entered 58 switch ( grade ) // switch statement nested in while59 { 60 case 'A': // grade was uppercase A 61 aCount++; // increment aCount 62 break; // necessary to exit switch 63 64 case 'B': // grade was uppercase B 65 bCount++; // increment bCount 66 break; // exit switch 67 68 case 'C': // grade was uppercase C 69 cCount++; // increment cCount 70 break; // exit switch 71 } // end switch statement

 

11

Page 12: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

12

4.7 while Repetition Statement

• Repetition statement– Action repeated while some condition remains true– Example

• int product = 3;

while ( product <= 100 ) product = 3 * product;

Page 13: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

13

Repetition Statement

•for repetition statement– Specifies counter-controlled repetition details in a single

line of code for ( initialization; loopContinuationCondition; increment )

statement;

Page 14: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

1414

Fig. 5.3 | for statement header components.

Page 15: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

15

do…while Repetition Statement

•do…while statement– Similar to while statement– Tests loop-continuation after performing body of loop

• Loop body always executes at least once– Example

int product = 3; do { product = 3* product ; } while ( product <= 100);

Page 16: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

16

Conditions of if statements may be formed using relational/equality and logical operators

Page 17: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

17

Fig. 2.12 | Equality and relational operators.

Standard algebraic equality or relational operator

C++ equality or relational operator

Sample C++ condition

Meaning of C++ condition

Relational operators > x > y x is greater than y < x < y x is less than y >= x >= y x is greater than or equal to y <= x <= y x is less than or equal to y Equality operators = == x == y x is equal to y ≠ != x != y x is not equal to y

Page 18: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

18

5.8 Logical Operators

• Logical operators– Allows for more complex conditions

• Combines simple conditions into complex conditions

• C++ logical operators– && (logical AND)– || (logical OR)– ! (logical NOT)

Page 19: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

19

Fig. 5.15 | && (logical AND) operator truth table.

expression1 expression2 expression1 && expression2

false false false false true false true false false true true true

Page 20: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

20

Fig. 5.16 | || (logical OR) operator truth table.

expression1 expression2 expression1 || expression2

false false false false true true true false true true true true

Page 21: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

21

Fig. 5.17 | ! (logical negation) operator truth table.

Expression !expression

false true true false

Page 22: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

22

Fig. 2.9 | Arithmetic operators.

C++ operation C++ arithmetic operator

Algebraic expression

C++ expression

Addition + f + 7 f + 7 Subtraction - p – c p - c Multiplication * bm or b· m b * m

Division / x / y or xy

or x ÷ y x / y

Modulus % r mod s r % s

Page 23: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

23

4.11 Assignment Operators

• Assignment expression abbreviations– Addition assignment operator

• Example– c = c + 3; abbreviates to c += 3;

• Statements of the formvariable = variable operator expression;

can be rewritten asvariable operator= expression;

• Other assignment operators– d -= 4 (d = d - 4)– e *= 5 (e = e * 5)– f /= 3 (f = f / 3)– g %= 9 (g = g % 9)

Page 24: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

24

4.12 Increment and Decrement Operators

• Increment operator ++ – Increments variable by one

• Example– c++

• Decrement operator -- – Decrement variable by one

• Example– c--

Page 25: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

25

Fig. 4.20 | Increment and decrement operators.

Operator Called Sample expression Explanation

++ preincrement ++a Increment a by 1, then use the new value of a in the expression in which a resides.

++ postincrement a++ Use the current value of a in the expression in which a resides, then increment a by 1.

-- predecrement --b Decrement b by 1, then use the new value of b in the expression in which b resides.

-- postdecrement b-- Use the current value of b in the expression in which b resides, then decrement b by 1.

Page 26: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

26

4.12 Increment and Decrement Operators (Cont.)

• If c = 5, then – cout << ++c;

• c is changed to 6• Then prints out 6

– cout << c++; • Prints out 5 (cout is executed before the increment) • c then becomes 6

Page 27: COEN 244 - 4 - Control Statement

2008 Pearson Education, Inc. All rights reserved.

27

Fig. 4.22 | Operator precedence for the operators encountered so far in the text.

Operators Associativity Type () left to right parentheses ++ -- static_cast< type >() left to right unary (postfix)

++ -- + - right to left unary (prefix)

* / % left to right multiplicative

+ - left to right additive

<< >> left to right insertion/extraction

< <= > >= left to right relational

== != left to right equality

?: right to left conditional

= += -= *= /= %= right to left assignment