next up previous contents
Next: Jumps Up: Programming Previous: Branches

Loops

Loops with condition x and statement(s) A:
while x A end
The while-loop is repeated until x becomes false (0).
>> x=1;y=1;
>> while(x<10) y=x+y; x++; end
>> y
y = 46

Loops with counter z and statement(s) A:
for z = vector A end
In the for-loop the counter is formally initialized by a vector. In each execution of the loop the counter takes on the value of the next element of vector.

>> x=1;y=1;
>> for(x=1:0.1:100) y=x^2+y; end
>> y
y = 3.3383E6



Helmut Dersch
2009-03-15