| Use a for loop to cycle over a fixed range of numbers.
for i = 1 to 3
Print (i)
1
2
3
for i = 5 to 12 step 2
Print (i)
5
7
9
11
for i = 20.0 to 15.0 step -1.5
Print (i)
20.0
18.5
17.0
15.5
For testing purposes, you can of course also type a loop directly into the console. Just make sure to keep the indentation
(the first loop iteration will execute while you type) and close the loop by typing a command without indentation:
>for i=1 to 3
> Print (i)
1
> Print (i*4)
4
>Print 'Done'
2
8
3
12
Done
If you have a fixed number of elements that are either strings or non-sequential numbers,
use this form of the for loop. This syntax matches Python.
for i in 5,10,3,-8
Print (i)
5
10
3
-8
for text in 'Hi!','I am','Yami..'
Print (text)
Hi!
I am
Yami..
And you can also loop over the content of a file:
for id in file /home/yasara/pdb_id.txt
Clear
LoadPDB (id)
CountRes all
In the example above, the file pdb_id.txt contains a list of PDB IDs to loop over. In this simple text file,
you can use either commas or linefeeds to separate the elements:
# Example for valid loop-file:
# Line starting with '#' are ignored.
# Using linefeeds to separate
1CRN
5TIM
# Using commas to separate
1SOL, 1STY,1THV
# By default, strings are weak strings, single quotes are not essential:
'1RIS', '1RWT'
# Double quotes are needed to specify strong strings:
"1VII","1YAS"
# Integers and floats are also allowed:
1,2,3
5.7, -1e60
In addition to the Python syntax, Yanaconda supports the C-like do
.. while X style. In the latter case, the loop is run at least once, even if the expression at the end is false.
i = 3
while i<50
Print (i)
i = i*2
3
6
12
24
48
i = 60
do
Print (i)
i = i*2
while i<50
60
Like C and Python, Yanaconda supports the 'break' statement to stop a loop. In addition,
it is possible to specify the number of nested loops to stop right after the 'break'.
i = 0
while 1
Print (i)
if i==5
break
i = i+1
0
1
2
3
4
5
for i=1 to 3
for j=10 to 15
Print (i)-(j)
if j==12
break 2
1-10
1-11
1-12
The 'continue' statement has also been borrowed from C and Python. Again,
you can specify the number of the loop to continue with.
for i=1 to 3
for j=10 to 15
if j==12
continue 2
Print (i)-(j)
1-10
1-11
2-10
2-11
3-10
3-11
|