{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
SELECT CASE ’ Construct & Exam 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. 1
Image of page 1

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

View Full Document Right Arrow Icon
Now consider the following piece of code. 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 ’ construct can be used instead of the more general ‘ IF THEN ELSEIF ’ construct.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern