If Structures

Statement Type:

All forms of the if statements are executable branching statements.

Statement Purpose:

The various forms of if statements are Fortran's main branching tool. They give Fortran an ability to make decisions in a program. The different forms of if statements that can be used include the simple logical if, the if-then-else structure, and the arithmetic if.

Required Form:

There are a few different structures that must be used depending on what form of the if structure you are using. They are as follows: The simple logical if: In a simple logical if, all that is need is some LOGICAL expression enclosed in paraenthesis and some executable statement following the LOGICAL expression. The statement will only be executed if the LOGICAL expression evalutes to .true. An example of this form of if statement is shown below.
		if (ldata.ge.100) print *,'There are to many data', 
	    & ' points'
If then - else structure:
		If ( logical expression ) then 
This is the first statement in an if-then-else structure. It contains the first logical expression to be evaluated. If the logical expression evaluates to .true. then only those statements beginning with the following one and ending before the next ELSE or ENDIF will be evaluated. All other statements in the structure will be ignored and not evaluated by the compiler.
		Else if (logical expression) then
This is used to specifiy additional conditions if the preceeding if-then and else-if statements have not been met. It can only be used as a part of an if - then else structure.
This is used to give a default condition that will be executed if none of the preceding conditions in the structure have been met.
		End if
This is needed to tell the compiler that the end of the if then else block has been reached. See the examples section for an example of the if - then - else structure. Arithmatic if : This is a very old and obsolete structure in Fortran. Never use this in any new programs you write. The two requirements for this type of if statement are an arithmatic expression and three line numbers for the purpose of branching. It is best explained in the example section.


The if -then-else structure was introduced with Fortran 77 and is a very powerful branching tool. It will allow the computer to make decisions based on a variety of different situations. Note however that once the computer evaluates one of the different conditions to be true, it will not evaluate any of the following conditions. An example of this structure is as follows:
		if ( delta.ge.1.) then
			print *, ' Too much deflection'
		else if ( shear.ge.21.) then
			print *, 'Beam fails in shear'
		else if ( sigma.ge.36.) then
			print *, 'Beam fails in tension'
		else if ( sigma.le.-36.) then 
			print *, 'Beam fails in compression'
			print *, 'Beam will not fail under these' 	      
   	    &	' conditions'
		end if
The arithmetic if. It is strongly recommended to never use this form of an if statement. It is a carry over from an archaic form of Fortran and it is highly likely that it will be dropped from some future Fortran standard. It is only shown here so you know what it is if you run into it in when modifying an early Fortran program. The following is an example of this structure.
		If (b**2-4*a*c) 100,200,300,
 	 100 	print*, 'Roots are complex'
		Go to 400
	 200 	print *, ' single real root '
		Go to 400
 	 300 	print *, 'two real roots'
 	 400 continue
In this type of structure the program branches to label 100 if the arithmetic expression in parenthesis evaluates to less than zero, or to 200 if it evaluates to be equal to zero or would branch to 300 if it evaluates to greater than zero.

Related Topics and Lectures:

select case structure
logical operators lecture 11

Up one level / Home

Written by Jason Wehr : jcw142@psu.edu and Maintained by John Mahaffy : jhm@cac.psu.edu