Using INCLAN control statements: Difference between revisions
No edit summary |
No edit summary |
||
Line 2: | Line 2: | ||
Commands can be executed conditionally by virtue of the “if” statement which has the same form as in Fortran: | Commands can be executed conditionally by virtue of the “if” statement which has the same form as in Fortran: | ||
if (i.gt.20) print "i is larger than 20." | if (i.gt.20) print "i is larger than 20." | ||
if (i.lt.0) then | if (i.lt.0) then | ||
print "i is negative." | print "i is negative." | ||
Line 10: | Line 12: | ||
print "i is none of the above." | print "i is none of the above." | ||
end if | end if | ||
Alternatively, comparison and logical operators can also be given in the form of the C programming language: | Alternatively, comparison and logical operators can also be given in the form of the C programming language: | ||
if (i>20) print "i is larger than 20." | if (i>20) print "i is larger than 20." | ||
if (i<0) then | if (i<0) then | ||
print "i is negative." | print "i is negative." | ||
Line 19: | Line 24: | ||
print "i is none of the above." | print "i is none of the above." | ||
end if | end if | ||
Note, however, that expressions are always evaluated according to the rules of Fortran-77. | Note, however, that expressions are always evaluated according to the rules of Fortran-77. | ||
Repeated execution of commands is achieved by forming loops with the “do” statement. Loops executed a predefined number of times have an integer loop variable: | Repeated execution of commands is achieved by forming loops with the “do” statement. Loops executed a predefined number of times have an integer loop variable: | ||
do i 1 20 | do i 1 20 | ||
print "i = $i" | print "i = $i" | ||
end do | end do | ||
Here, the loop variable, i, runs from 1 to 20 in steps of 1. | Here, the loop variable, i, runs from 1 to 20 in steps of 1. | ||
A loop that is executed until a termination condition is met can be constructed as follows: | A loop that is executed until a termination condition is met can be constructed as follows: | ||
do | do | ||
... | ... | ||
Line 31: | Line 40: | ||
... | ... | ||
end do | end do | ||
The “break” statement exits from a loop. | The “break” statement exits from a loop. | ||
Unconditional jumps are possible with the “go to” statement: | Unconditional jumps are possible with the “go to” statement: | ||
do i 1 n | |||
... | |||
if (err) go to cleanup | |||
... | |||
end do | |||
... | |||
cleanup: print "Error in the loop." | cleanup: print "Error in the loop." | ||
... | |||
The “go to” statement transfers the program flow to the position indicated by the label (“cleanup”). | The “go to” statement transfers the program flow to the position indicated by the label (“cleanup”). |
Revision as of 14:17, 7 January 2010
INCLAN provides a full set of control statements to direct the program flow. These are used mainly in macros, i. e. in collections of INCLAN statements that form new commands which can be used in the same way as basic commands. Since control statements are not used interactively, the program prompt (“cyana>”) will no longer be shown.
Commands can be executed conditionally by virtue of the “if” statement which has the same form as in Fortran:
if (i.gt.20) print "i is larger than 20."
if (i.lt.0) then
print "i is negative."
else if (i.lt.10 .and. mod(i,2).eq.0) then
print "i is less than 10 and even."
else
print "i is none of the above."
end if
Alternatively, comparison and logical operators can also be given in the form of the C programming language:
if (i>20) print "i is larger than 20."
if (i<0) then
print "i is negative."
else if (i<10 && mod(i,2)==0) then
print "i is less than 10 and even."
else
print "i is none of the above."
end if
Note, however, that expressions are always evaluated according to the rules of Fortran-77. Repeated execution of commands is achieved by forming loops with the “do” statement. Loops executed a predefined number of times have an integer loop variable:
do i 1 20
print "i = $i"
end do
Here, the loop variable, i, runs from 1 to 20 in steps of 1. A loop that is executed until a termination condition is met can be constructed as follows:
do
... if (x.gt.100.0 .or. finished) break ...
end do
The “break” statement exits from a loop. Unconditional jumps are possible with the “go to” statement:
do i 1 n
... if (err) go to cleanup ...
end do ... cleanup: print "Error in the loop." ...
The “go to” statement transfers the program flow to the position indicated by the label (“cleanup”).