1 / 11

Control Structures

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.

kirti
Download Presentation

Control Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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

  3. Relational Operators • Used to test the relationship between two expressions or two variables • Condition evaluates as .FALSE. .TRUE.

  4. 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

  5. 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

  6. Examples x = 500, y = 300 x= =y x > 100 y >=250 x /= 500 ((x + y) > 700) ((x / 2) .LT. 99)

  7. 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

  8. 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

  9. 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

  10. Conditions Practice • 1. “A” < “F” “cat” < “can” “June” > “July” “cat_ _ _” < “cattle” “c” = = “C”

  11. 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))

More Related