150 likes | 383 Views
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
E N D
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 • 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
Selection Structures in Fortran 90 • Block IF • IF-THEN • IF-THEN-ELSE • Logical IF • case structure • Using ELSE IF (compound IF) • Using SELECT CASE
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.
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
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.
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.
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
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
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
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
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
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