1 / 14

Selection Structures in Fortran 90

Selection Structures in Fortran 90. Parts of a Selection Structure. Selection Expression (condition) One or more branches Exactly one branch is chosen based on the value of the selection expression Each branch consists of one or more statements. Generic Selection Structures. if - then

aric
Download Presentation

Selection Structures in Fortran 90

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. Selection Structures in Fortran 90

  2. Parts of a Selection Structure • Selection Expression (condition) • One or more branches • Exactly one branch is chosen based on the value of the selection expression • Each branch consists of one or more statements

  3. Generic Selection Structures • if - then • One branch to be taken whenever the selection expression evaluates to TRUE • if - then - else • One branch to be taken whenever the selection expression evaluates to TRUE • A second branch to be taken whenever the selection expression evaluates to FALSE • case • Three or more branches

  4. Selection Structures in Fortran 90 • Block IF • IF-THEN • IF-THEN-ELSE • Logical IF • case structure • Using ELSE IF (compound IF) • Using SELECT CASE

  5. IF-THEN • Used to define a block of code that may or may not be executed • Block is executed whenever the selection expression is .TRUE. • Block is skipped whenever the selection expression is .FALSE.

  6. IF-THEN Syntax Required parentheses One line or continued IF ( logical_expression ) THEN block of statements END IF Block is executed when logical_expression is .TRUE. but skipped otherwise END IF can be one or two words

  7. IF-THEN-ELSE • Used to define two block of code, only one of which will be executed • One block is executed whenever the selection expression is .TRUE. • The other block is executed whenever the selection expression is .FALSE.

  8. IF-THEN-ELSE Syntax IF ( logical_expression ) THEN block_1 ELSE block_2 END IF Block_1 is executed when logical_expression is .TRUE. A line by itself Block_2 is executed when logical_expression is .FALSE.

  9. Case Structure using ELSE IF (compound IF) • Used to define several blocks of code, only one of which will be executed • Each block is guarded by a selection expression • A block is executed whenever its selection expression is .TRUE. • If the selection condition is true for two or more blocks, only the first block is executed • Unlimited number of blocks • Only one END IF is required

  10. Compound IF Syntax IF ( logical_expression_1 ) THEN block_1 ELSE IF (logical_expression_2 ) THEN block_2 ELSE IF (logical_expression_3 ) THEN block_3 ELSE last block END IF One line or continued Block_k is executed when logical_expression_k is .TRUE. If two or more logical expressions are .TRUE. only the first “true” block is executed Optional Only executed if all expressions are .FALSE. ELSE IF can be one or two words

  11. Case Structure using SELECT CASE • Used to define several blocks of code, only one of which will be executed • Entire structure is controlled by a selection expression • The selection expression must have an integer, character string, or logical value • Each block is guarded by a case value or range of values (must be constant) • A block is executed whenever the value of the selection expression matches or is in the range of the case values for that block • Distinct blocks must have distinct case values • Unlimited number of cases

  12. SELECT CASE Syntax SELECT CASE ( selection_expression ) CASE(case_values) block_1 CASE(case_values) block_2 CASE(case_values) block_3 CASE DEFAULT default block END SELECT Styles for case_values “se” is “selection_expression” V1 ! se == V1 V1: ! se >= V1 :V2 ! se <= V2 V1:V2 ! V1 <= se <= V2 Optional Only executed if no other case is selected

  13. Logical IF • Used to define a single statement which may or may not be executed • The statement is executed whenever the selection expression is .TRUE. • The statement is skipped whenever the selection expression is .FALSE. • Primitive version of the IF-THEN • Primarily used with repetition structures

  14. Logical IF Syntax Required parentheses Exactly one statement IF ( logical_expression ) statement One line or continued statement is executed when logical_expression is .TRUE. but skipped otherwise This is equivalent to IF ( logical_expression ) THEN statement END IF

More Related