fortran 2- more basics chapter 3 in your fortran book

8
Fortran 2- More Basics Chapter 3 in your Fortran book

Upload: eileen-stewart

Post on 01-Jan-2016

237 views

Category:

Documents


8 download

TRANSCRIPT

Page 1: Fortran 2- More Basics Chapter 3 in your Fortran book

Fortran 2-More Basics

Chapter 3 in your Fortran book

Page 2: Fortran 2- More Basics Chapter 3 in your Fortran book

Logical IF Structures• We dealt very briefly in Excel with if statements that are used to carry out specific

actions based on certain logical conditions. There are a number of situations in Fortran programming when logical comparisons are valuable in determining the actions that the program will take.

• To make a logical comparison we will require the following operators:OPERATOR MEANING.EQ. equal to.NE. not equal to.LT. less than.LE. less than or equal to.GT. greater than.GE. greater than or equal to

• In order to use these operators we make a comparison such as (AREA.LT.0) that

translates to “AREA less than zero”. This is a logical expression which can be used in an IF statement.

• A program may make use of a simple logical IF statement, or a block IF statement.

Page 3: Fortran 2- More Basics Chapter 3 in your Fortran book

Logical Operators

• Logical expressions can be combined using logical operators:– .OR. and .AND.ex: IF(X.GT.10.AND.X.LT.25)

• The logical expression can be changed to the opposite value by preceding it with .NOT. ex: .NOT.(A.LT.15)

• The order of priority, from highest to lowest is:.NOT., .AND., .OR.

Page 4: Fortran 2- More Basics Chapter 3 in your Fortran book

One line IF Statement

• Making use of a logical IF statement which may be expressed on a single line takes the following form:

IF (logical expression) executable statement

For example:IF (STRESS.GT.FY) STRESS=FY

Page 5: Fortran 2- More Basics Chapter 3 in your Fortran book

Block IF Statements• In many cases the conditions require more than a simple executable

statement. In these situations we will use a “block IF statement”. If(logical expression) Then

statement 1 .

.

.statement n

END IF

• If the logical statement is true, the statements within the Block If will be executed.

• If the logical statement is false, the program will jump to the END IF statement and none of the statements within the BLOCK IF will be executed.

Page 6: Fortran 2- More Basics Chapter 3 in your Fortran book

Else• There are other statements associated with the BLOCK IF structure such as the

ELSE and ELSE IF statements: If(logical expression 1) THEN

statement 1..statement mELSEstatement m+1..statement nEND IF

• The above command will evaluate the logical expression, and if true will

execute statements 1 through m. If the logical expression is false (ELSE) statements m+1 through statements n will be carried out.

Page 7: Fortran 2- More Basics Chapter 3 in your Fortran book

Else IFIf(logical expression 1) THEN

statement 1..statement mELSE IF (logical expression 2) THENstatement m+1..statement nELSEstatement n+1..statement oEND IF

• This will first evaluate the logical expression 1, and if true will execute statements 1 through m and then skip to the END IF.

• If logical expression 1 is false (ELSE IF), logical expression 2 will be evaluated, and if true statements m+1 through n will be executed and then the program will skip to the END IF.

• If the both logical expressions 1 and 2 are false (ELSE) statements n+1 through statement o will be carried out and the IF BLOCK will terminate at the END IF statement.

• We can string together several IF, ELSEIF statements to satisfy the conditions of our problem. The BLOCK IF structure can sometimes be confusing to follow so it is highly encouraged to indent individual sections of the BLOCK IF to set the different sections apart.

Page 8: Fortran 2- More Basics Chapter 3 in your Fortran book

Example

Write a simple Fortran program to evaluate the stress in the rod for a given load, area, and yield stress (Fy)