set9 - `SELECT CASE' Construct & Exam Handout Nine 1...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
SELECT CASE Handout Nine 1 SELECT CASE ’ Construct The ‘ SELECT CASE ’ Construct is another decision making or branching construct to control the flow of your codes. 1.1 The ‘ IF THEN ELSEIF ’ construct revisited. Earlier in the course you were introduced to the ‘ IF ’ construct. The ‘ IF ’ construct allows ‘overlap’ of ranges in tests made in the segments of the construct. For example consider the following piece of code. PROGRAM example_if !** First piece of code to simply demonstrate the importance !** of ordering and the allowed overlapping of the test ranges !** in an IF construct. IMPLICIT NONE INTEGER :: i PRINT*,"Enter an INTEGER" READ*,i !*** Get the user to input i !**** IF construct with no overlapping of test ranges. !**** Therefore ORDERING is important IF (i .EQ. 2) THEN PRINT*,"Integer = 2" ELSEIF (i .EQ. 7) THEN PRINT*,"Integer = 7" ELSE IF (i .GE. 2 .AND. i .LE. 8) THEN PRINT*,"Integer is greater than one and less than nine" ELSE IF (i .GE. 6 .AND. i .LE. 10) THEN PRINT*,"Integer is larger than five and less than eleven" ELSE PRINT*,"No Match" END IF END PROGRAM example_if The execution of this construct involves testing each branches logical expression from top to bottom in turn. If one of the logical expressions evaluates to ‘ .TRUE. ’ then the statements in that branch are executed and the construct exited. Note how the ranges overlap so the order that the programmer places the branches in the construct are critical.
Background image of page 1

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

View Full DocumentRight Arrow Icon
PROGRAM example_if2 !** !** Second piece of code to simply demonstrate the importance !** of ordering and the allowed overlapping of the test ranges !** in an IF construct. !** IMPLICIT NONE INTEGER :: i PRINT*,"Enter an INTEGER" READ*,i !*** Get the user to input i !**** IF construct with no overlapping of test ranges. !**** Therefore ORDERING is NOT important IF (i .EQ. 2) THEN PRINT*,"Integer = 2" ELSE IF (i .EQ. 7) THEN PRINT*,"Integer = 7" ELSE IF (i .GT. 2 .AND. i .LT. 7) THEN PRINT*,"Integer is greater than 2 an less than 7" ELSE IF (i .GT. 7 .AND. i .LE. 10) THEN PRINT*,"Integer is greater than 7 and less than 11" ELSE PRINT*,"No Match" END IF END PROGRAM example_if2 Exercise One : In a ‘ handout9/exercise1 ’ directory copy the above two codes from; fortran1 exercise1> cp ~/info/examples/handout9/exercise1/example_if.f90 . fortran1 exercise1> cp ~/info/examples/handout9/exercise1/example_if2.f90 . Remember the ‘period’ at the end is required as it tells Linux you want to copy the file to your current directory. Compile and run the codes making sure you understand the results . Notice how in the second code there is no overlapping of the ranges for the logical test expressions for the various branches of the construct, so each test is unique. As there is no overlapping, the order in which the programmer chooses to place each branch in the construct is no longer an issue. In such a situation the ‘ SELECT CASE
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 7

set9 - `SELECT CASE' Construct & Exam Handout Nine 1...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online