14.09.2015 Views

Matvec Users’ Guide

Matvec Users' Guide

Matvec Users' Guide

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

4.3. WHILE STATEMENT 37<br />

that all those three sections can be omitted simultaneously, thus for (; ;) BLOCK construction provides<br />

user an infinite loop. The break statement is the only wat to exit such an infinite loop.<br />

The second form of for statement is similar to that in Maple language. The expression must evaluate to<br />

a either matrix, or vector, or list, otherwise a run-time error will result. The for statement simply executes<br />

the BLOCK the N times where N is the size of expression unless it encounters the break statement, which<br />

will terminate the nearest for–loop execution. The for statement can be nested, too. For instance,<br />

> A = zeros(3,4);<br />

> for (i=0; i A<br />

Col 1 Col 2 Col 3 Col 4<br />

Row 1 2.00000 3.00000 4.00000 5.00000<br />

Row 2 3.00000 4.00000 5.00000 6.00000<br />

Row 3 4.00000 5.00000 6.00000 7.00000<br />

4.3 While Statement<br />

The while statement is very similar to that in C/C++.<br />

while (expression) BLOCK<br />

If the expression evaluates to anything rather than zero, then the BLOCK is executed, otherwise goes to<br />

the statement right after the BLOCK.<br />

For example,<br />

> i=1; j=0;<br />

> while(i > 10) {<br />

j += i++;<br />

}<br />

> j<br />

45<br />

4.4 Repeat–Until Statement<br />

The repeat statement is very similar to that in PASCAL language.<br />

repeat BLOCK until (expression)<br />

the BLOCK is executed until the expression evaluates to anything rather than zero. Then, it goes to the<br />

statement right after the expression.<br />

For example,<br />

> i=1; j=0;<br />

> repeat {<br />

j += i++;<br />

} until (i >= 10)<br />

> j<br />

45

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!