5. Selection: If and Switch Controls - CiteSeerX

File Input/Output. 4. ... Repetition structures (next week) ... sequence of pseudocode statements: do this, do that, then ..... cout
504KB taille 7 téléchargements 279 vues
Computer Science I CS 135

5. Selection: If and Switch Controls

René Doursat Department of Computer Science & Engineering University of Nevada, Reno Fall 2005

Computer Science I CS 135 0. Course Presentation 1. Introduction to Programming 2. Functions I: Passing by Value 3. File Input/Output 4. Predefined Functions 5. If and Switch Controls 6. While and For Loops 7. Functions II: Passing by Reference 8. 1-D and 2-D Arrays 10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

2

Computer Science I CS 135 5. Selection: If and Switch Controls a. Control Structures b. Logical Expressions c. If / Else Selection Structures d. Switch Selection Structures

10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

3

Computer Science I CS 135 5. Selection: If and Switch Controls a. Control Structures 9 What are control structures? 9 Selection structures 9 Repetition structures (next week)

b. Logical Expressions c. If / Else Selection Structures d. Switch Selection Structures

10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

4

5.a Control Structures What are control structures?

¾ Reminder: there are six basic computer operations 1. a computer can receive information (Get, Read, etc.) 2. a computer can put out information (Display, etc.) 3. a computer can perform arithmetic (Add, Divide, etc.) 4. a computer can assign a value to a variable or memory location (Set, Initialize, etc.) 5. a computer can compare variables and select one of two alternate actions → selection structures 6. a computer can repeat a group of actions → repetition structures 10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

5

5.a Control Structures What are control structures?

¾ Structure theorem 9 it is possible to write any computer program by using only three basic control structures that are easily represented in pseudocode: ƒ sequence structures ƒ selection structures introduce branching (“jumps”) in the sequential logic ƒ repetition structures

¾ Sequence structures 9 straightforward execution of one processing step after another 9 sequence of pseudocode statements: do this, do that, then this, then that, etc. 10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

6

5.a Control Structures What are control structures?

¾ Selection structures 9 condition and choice between two actions, depending on whether the condition is true or false 9 represented by the pseudocode keywords IF, THEN, ELSE, and ENDIF

¾ Repetition structures 9 block of statements to be executed repeatedly, as long as a condition is true 9 represented by the pseudocode keywords WHILE and ENDWHILE

10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

7

5.a Control Structures What are control structures?

¾ Sequence, selection and repetition structures

10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

8

5.a Control Structures Selection structures

¾ A computer can compare variables and select one of two alternate actions → selection structures 9 examples: ƒ one-way − if it starts to rain, go inside the building ƒ two-way − if the car starts, drive; otherwise, take the bus 9 pseudocode examples: IF student is female THEN IF age >= 12 THEN Add 1 to female count Prompt for entrance fee ELSE ENDIF Add 1 to male count One-way selection 10/3-5/2005

ENDIF

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

Two-way selection 9

5.a Control Structures Selection structures

¾ Anatomy of an if /else selection structure (pseudocode) 9 the “header” is a logical expression 9 the “body” contains actions that are performed (or not) depending on the header header

IF (true or false logical expression) [THEN] actions that are performed if true

body

ELSE alternative body

other actions that are performed if false Anatomy of an if / else selection structure

10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

10

5.a Control Structures Repetition structures (next week)

¾ A computer can repeat a group of actions → repetition structures 9 examples: ƒ calculate 100 student grades ƒ pour water in the saucepan until it is full ƒ cook the pasta until it is “al dente” 9 pseudocode example: WHILE water_level < pan_height Add 1 tablespoon to water_volume water_level = water_volume / pan_surface ENDWHILE 10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

11

Computer Science I CS 135 5. Selection: If and Switch Controls a. Control Structures 9 What are control structures? 9 Selection structures 9 Repetition structures

b. Logical Expressions c. If / Else Selection Structures d. Switch Selection Structures

10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

12

Computer Science I CS 135 5. Selection: If and Switch Controls a. Control Structures b. Logical Expressions 9 Relational operators 9 Logical (Boolean) operators 9 Order of precedence

c. If / Else Selection Structures d. Switch Selection Structures

10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

13

5.b Logical Expressions

In this section, we look at the header of if / else selection structures IF (true or false logical expression) actions performed if true ELSE actions performed if false

10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

14

5.b Logical Expressions Relational operators

¾ Relational operators allow to make comparisons

9 a relational operator ƒ is a binary operator: it takes two numeric operands ƒ yields a boolean result, true or false ƒ 10/3-5/2005

false also evaluates as 0 and true evaluates as nonzero CS 135 - Computer Science I - 5. Selection: If and Switch Controls

15

5.b Logical Expressions Relational operators

¾ Relational operators allow to make comparisons 9 examples: ƒ 8 < 8.01 8 less than 8.01 true ƒ 6 != 6.0 6 not equal to 6.0 false ƒ 7 >= 7 7 greater than or equal to 7 true ƒ 'a' == 'b' char ‘a’ equal to ‘b’ false 9 unlike the assignment operator = it is ok to have expressions on both sides of a relational operator, for example if x is 6: ƒ (7 + x/5.0) > (x + 2.1) is true 9 because of precision problems, use caution when equating floating-point expressions (using ==) ƒ 2.0/7 + 5.0/7 == 1.0 is likely to be false 10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

16

5.b Logical Expressions Relational operators

¾ Relational operators allow to make comparisons 9 relational operators are strictly binary ƒ 0 = 5) && ('A' == 'B') true ƒ (14 != 5) && ('A' < 'B') false ƒ (14 < 5) && !('$' >= '*') ?? never mind true ƒ !(14 < 5) && 3 10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

21

5.b Logical Expressions Logical (Boolean) operators

¾ Boolean algebra with && (‘and’) 9 important equivalences among expressions containing && Logical expression x && true x && false x && x x && !x x && y x && (y && z)

10/3-5/2005

Equivalent expression x false x false y && x (x && y) && z

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

22

5.b Logical Expressions Logical (Boolean) operators

¾ The || (‘or’) operator 9 is true if at least one of its logical operand is true

one true operand is enough to yield true 9 examples: ƒ (14 == 5) || ('A' 5) || !('$' >= '*') true

ƒ 10/3-5/2005

!(14 > 5) || 0

?? never mind

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

false 23

5.b Logical Expressions Logical (Boolean) operators

¾ Boolean algebra with || (‘or’) 9 important equivalences among expressions containing || Logical expression x || true x || false x || x x || !x x || y x || (y || z)

10/3-5/2005

Equivalent expression true x x true y || x (x || y) || z

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

24

5.b Logical Expressions Logical (Boolean) operators

¾ Boolean algebra with && and || 9 important equivalences among expressions with && and || Logical expression x || (y && z) x || (x && z) x && (y || z) x && (x || z) !(x || y) !(x && y)

Equivalent expression (x || y) && (x || z) x (x && y) || (x && z) x !x && !y !x || !y

9 De Morgan’s laws 10/3-5/2005

CS 135 - Computer Science I - 5. Selection: If and Switch Controls

25

5.b Logical Expressions Order of precedence

¾ How to evaluate complex logical expressions 9 expressions can mix arithmetic, relational and logical operators: ƒ

!(5 + 3 = 0

false

!(found || x < 0)

false

x + y 100

true

'A'