(5) Making Choices Selection In Fortran

(5) Making Choices Selection In Fortran - Selection...

This preview shows pages 1–4. Sign up to view the full content.

1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Selection As we have seen: ± Every programming language must provide a selection mechanism that allows us to 2007 Selection 2 control whether or not a statement should be executed ± This will depend on whether or not some condition is satisfied (such as the discriminant being positive) ! ------------------------------------------------------------ ! Solve Ax^2 + Bx + C = 0 ! ------------------------------------------------------------ PROGRAM QuadraticEquation IMPLICIT NONE ! **** Same old declarations and set up **** ! compute the square root of discriminant d d = b*b - 4.0*a*c IF (d >= 0.0) THEN ! is it solvable? d = SQRT(d) 2007 Selection 3 root1 = (-b + d)/(2.0*a) root2 = (-b - d)/(2.0*a) WRITE(*,*) "Roots are ", root1, " and ", root2 ELSE ! complex roots WRITE(*,*) "There is no real root!" WRITE(*,*) "Discriminant = ", d END IF END PROGRAM QuadraticEquation FORTRAN Selection Used to select between two alternative sequences of statements. The keywords delineate the statement blocks. Syntax 2007 Selection 4 Syntax: IF (logical-expression) THEN first statement block, s 1 ELSE second statement block, s 2 END IF Semantics of IF…THEN…ELSE…END IF ² Evaluate the logical expression. It can have value .TRUE. or value .FALSE. ² If the value is .TRUE., evaluate s 1 , the first block ftt t 2007 Selection 5 of statements ² If the value is .FALSE., evaluate s 2 , the second block of statements ² After finishing whichever of s 1 or s 2 that was chosen, execute the next statement following the END IF What’s Going On? ± What is a “logical expression” ? 2007 Selection 6

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 What’s Going On? ± What is a “logical expression” ? ± Where do the values .TRUE. and FALSE come from? 2007 Selection 7 .FALSE. come from? What’s Going On? ± What is a “logical expression” ? ± Where do the values .TRUE. and FALSE come from? 2007 Selection 8 .FALSE. come from? ± What are those periods around the words true and false? Logical Data Type ² FORTRAN has a LOGICAL data type, just like it has INTEGER and REAL types ² Each type has its associated values 2007 Selection 9 Logical Data Type ² FORTRAN has a LOGICAL data type, just like it has INTEGER and REAL types ² Each type has its associated values 2007 Selection 10 ² There are only two values in the type LOGICAL, true and false Logical Data Type ² FORTRAN has a LOGICAL data type, just like it has INTEGER and REAL types ² Each type has its associated values 2007 Selection 11 ² There are only two values in the type LOGICAL, true and false ² To enable the compiler to distinguish these values from variables, we represent them with periods around the words Logical Data Type ² We can declare variables of type LOGICAL LOGICAL :: positive x, condition 2007 Selection 12 _
3 Logical Data Type ± We can declare variables of type LOGICAL LOGICAL :: positive x, condition 2007 Selection 13 _ ± We can assign values to them condition = .TRUE.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

(5) Making Choices Selection In Fortran - Selection...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online