If (block)

Statement Type:

The Block IF statement is an executable branching statement.

Statement Purpose:

The block IF is Fortran's main branching and decision making tool. It allows the program to execute different instructions when a certain logical condition is met.

Associated Statements:

There are a few different additional required Statements that must be used in the block if structure. These statements are the if then statement, the else if statement, the else statement and the end if statement. It is because of these statements that the block if structure is sometimes refereed to as the if - then - else structure.

If 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 all other statements in the structure will be ignored and not evaluated by the compiler. There can be only one if then statement in a Block IF. It looks like the following.
		If ( logical expression ) then
Else if This is optionally used to specify additional conditions if the preceding IF THEN and ELSE IF statements have not been met. There can be as many ELSE IF statements as one needs in an IF block. It can only be used as a part of an if - then-else structure. It looks like the following.
		Else if (logical expression) then
Else This is used to give a default condition that will be executed if none of the preceding IF statements have been met. There can only be one ELSE statement in a given IF Block. All that is required of this statement is the command ELSE with some sort of executable statements following on the next lines.

End if

This is required to tell the compiler that the end of the if-then-else block has been reached.

		

Examples:

The block if 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 other 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'
		else 
			print *, 'Beam will not fail under these' 	      
   	    &	' loading conditions'
		end if

Related Topics and Lectures:

select case structure

logical operators

arithmetic if

logical if

lecture eleven

Examples

grades1.f, associated.f and iftests.f

Up one level / Home


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