CSCE150 Fortran Lab 4 - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

CSCE150 Fortran Lab 4

Description:

the IF (...) THEN is a single Fortran statement. they must be written together on ... Show me. your code. result of the test case. 6, 8, 10 = Valid triangle! ... – PowerPoint PPT presentation

Number of Views:29
Avg rating:3.0/5.0
Slides: 14
Provided by: cse92
Category:
Tags: csce150 | fortran | lab | me | show

less

Transcript and Presenter's Notes

Title: CSCE150 Fortran Lab 4


1
CSCE150 Fortran Lab 4
2
outline
  • Branching Structures
  • if then else
  • select case
  • Exercise 1
  • Exercise 2
  • Exercise 3

3
Branching Structures
  • if then else
  • (section 3.4.1 and 3.4.2 page 96)
  • Used for binary condition judgment (True/False)
  • Example1
  • INTEGER a
  • IF (agt0) THEN
  • WRITE (,) a is positive
  • END IF

4
  • the IF () THEN is a single Fortran statement
  • they must be written together on the same line
  • the statements to be executed must occupy
    separate lines following the IF () THEN
    statement
  • an END IF statement must follow them on a
    separate line

5
  • Example2
  • INTEGER a
  • IF (agt0) THEN
  • WRITE (,) a is positive
  • ELSE
  • WRITE (,) a is not positive
  • END IF
  • an ELSE statement must occupy a line by itself
  • Example3
  • INTEGER a
  • IF (agt0) THEN
  • WRITE (,) a is positive
  • ELSE IF (alt0) THEN
  • WRITE (,) a is negative
  • ELSE
  • WRITE (,) a is zero
  • END IF

6
Branching Structures
  • select case
  • (section 3.4.7 page 111)
  • Used for multiple selection judgment (multiple
    choices and treatments)

7
  • Example
  • INTEGERa
  • SELECT CASE (a)
  • CASE (0)
  • WRITE(,) a is zero
  • CASE (1)
  • WRITE(,) a is positive
  • CASE (-1)
  • WRITE(,) a is negative
  • CASE DEFAULT
  • WRITE(,) impossible entry
  • END SELECT
  • Example 3 of Ifthenelse
  • INTEGER a
  • IF (agt0) THEN
  • WRITE (,) a is positive
  • ELSE IF (alt0) THEN
  • WRITE (,) a is negative
  • ELSE
  • WRITE (,) a is zero
  • END IF

8
  • review and compare the example 3 of if then
    else and the example of select case
  • select case branching structure can always be
    implemented by if then else structure

9
Exercise 1
  • Write a Fortran program to convert a score to a
    grade (use ifthenelse)
  • 90-100 A
  • 80-89 B
  • 70-79 C
  • 60-69 D
  • 0 - 59 F
  • the score (less than 100) is specified by the
    user
  • print the corresponding grade to the screen

10
Exercise 2
  • triangle judgment
  • Recall from high school geometry that the sum of
    any two sides of a triangle must be strictly
    greater than the third.
  • That is, for sides a, b and c, the inequalities
  • abgtc
  • acgtb
  • bcgta
  • must all hold.

11
  • Write a Fortran program to
  • get three values from the user
  • check whether the inputs could form a valid
    triangle or not
  • if the inputs can form a triangle, print a
    message Valid triangle! to the screen
  • otherwise, print Invalid triangle!

12
  • relational operators
  • Section 3.3.3 on page 91
  • combinational logic operators
  • Section 3.3.4 on page 92
  • Show me
  • your code
  • result of the test case
  • 6, 8, 10 gt Valid triangle!
  • 5, 6, 1 gt Invalid triangle!

13
Exercise 3
  • write a Fortran program to convert a score to a
    grade (use select case)
  • 90-100 A
  • 80-89 B
  • 70-79 C
  • 60-69 D
  • 0 - 59 F
  • the score (less than 100) is specified by the
    user
  • print the corresponding grade to the screen
Write a Comment
User Comments (0)
About PowerShow.com