130 likes | 407 Views
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. Selection.
E N D
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
Selection • We use selection to handle making choices • A condition that is evaluated to be either true or false determines the next action True False Is a > b? Print “Yes” Print “No” IF 1 is greater than 2 THEN Print “Yes” ELSE Print “No” ENDIF
Relational Operators • Used to test the relationship between two expressions or two variables • Condition evaluates as .FALSE. .TRUE.
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
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
Examples x = 500, y = 300 x= =y x > 100 y >=250 x /= 500 ((x + y) > 700) ((x / 2) .LT. 99)
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
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. Only evaluates as false if both false (.NOT. (x >= 7)) reverses the value of expression to its immediate right. same as if (x < 7) then
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
Conditions Practice • 1. “A” < “F” “cat” < “can” “June” > “July” “cat_ _ _” < “cattle” “c” = = “C”
Conditions Practice • 1. Given a = 12, b = 6, c = 8 true 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))