Download presentation
Presentation is loading. Please wait.
Published byEileen Stewart Modified over 9 years ago
1
Fortran 2- More Basics Chapter 3 in your Fortran book
2
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: OPERATORMEANING.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.
3
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.
4
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
5
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.
6
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 m ELSE statement m+1. statement n END 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.
7
Else IF If(logical expression 1) THEN statement 1. statement m ELSE IF (logical expression 2) THEN statement m+1. statement n ELSE statement n+1. statement o END 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.
8
Example Write a simple Fortran program to evaluate the stress in the rod for a given load, area, and yield stress (F y )
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.