control structures in structured programming, we use three basic control structures: –sequence...

11
Control Structures In structured programming, we use three basic control structures: – Sequence – Selection – Repetition So far, we have worked with sequential algorithms, where each instruction is executed once, immediately after the one above it

Upload: brooke-snow

Post on 04-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential

Control Structures

• In structured programming, we use three basic control structures:– Sequence

– Selection

– Repetition

• So far, we have worked with sequential algorithms, where each instruction is executed once, immediately after the one above it

Page 2: Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential

Selection• We use selection to handle making choices

• A condition that is evaluated to be either true or false determines the next action

Is a > b?

Print “Yes”Print “Yes” Print “No”Print “No”

True False

IF 1 is greater than 2 THEN Print “Yes”ELSE Print “No”ENDIF

Page 3: Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential

Relational Operators

• Used to test the relationship between two expressions or two variables

• Condition evaluates as .FALSE.

.TRUE.

Page 4: Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential

FORTRAN’s Relational Operators

• = = Equal uses two equal signs

• > Greater than

• < Less than

• >= Greater than or equal to

• <= Less than or equal to

• /= Not equal

Page 5: Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential

FORTRAN’s Relational Operators

• = = or .EQ. Equal uses two equal signs

• > or .GT. Greater than

• < or .LT. Less than

• >= or .GE. Greater than or equal to

• <= or .LE. Less than or equal to

• /= or .NE. Not equal

Page 6: Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential

Examples

x = 500, y = 300

x= =y

x > 100

y >=250

x /= 500

((x + y) > 700)

((x / 2) .LT. 99)

Page 7: Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential

Compound Logical Expressions

• Used to combine or negate expressions containing logical operators.– used with compound conditions

.AND. And

.OR. Or

.NOT. Not

.EQV. Equivalence

.NEQV. Nonequivalence

Page 8: Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential

Logical Operators((x == 5) .AND. (y != 10)) both conditions must be true for

statement to evaluate as true

((x == 7) .OR. (y > 10)) either condition can be true for statement to evaluate is true.

Onlyevaluates as false if both false

(.NOT. (x >= 7)) reverses the value of expression toits immediate right. same as if (x < 7) then

Page 9: Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential

Order of Precedence( ) Parentheses highest** Exponentiation *, / Multiplication,Division+, - Addition, Subtraction

< <= > >= Greater than, less than, …or equal= = /= Equal, not equal.AND. And.OR. Or= Assignment statement lowest

Rule: Use parentheses to make evaluation order clear

Page 10: Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential

Conditions Practice

• 1. “A” < “F”

“cat” < “can”

“June” > “July”

“cat_ _ _” < “cattle”

“c” = = “C”

Page 11: Control Structures In structured programming, we use three basic control structures: –Sequence –Selection –Repetition So far, we have worked with sequential

Conditions Practice

• 1. Given a = 12, b = 6, c = 8true or false? ((a < b) .AND. (b /= c) (.NOT.(a < b) .AND. .NOT.(b > c)) ((a > b).AND.(a > c).OR.(b > c))

• 2. What would the values of a, b, and c need to be for this statement to be true? (use 1,2,3)((a >c .OR. b > c) .AND. (a < b))