Programming Previous page   Next Page

Loop Control -- for, while, continue, break

With loop control statements, you can repeatedly execute a block of code, looping back through the block while keeping track of each iteration with an incrementing index variable. Use the for statement to loop a specific number of times. The while statement is more suitable for basing the loop execution on how long a condition continues to be true or false. The continue and break statements give you more control on exiting the loop.

for

The for loop executes a statement or group of statements a predetermined number of times. Its syntax is

The default increment is 1. You can specify any increment, including a negative one. For positive indices, execution terminates when the value of the index exceeds the end value; for negative increments, it terminates when the index is less than the end value.

For example, this loop executes five times.

You can nest multiple for loops.

Using Arrays as Indices.   The index of a for loop can be an array. For example, consider an m-by-n array A. The statement

sets k equal to the vector A(:,i), where i is the iteration number of the loop. For the first loop iteration, k is equal to A(:,1); for the second, k is equal to A(:,2); and so on until k equals A(:,n). That is, the loop iterates for a number of times equal to the number of columns in A. For each iteration, k is a vector containing one of the columns of A.

while

The while loop executes a statement or group of statements repeatedly as long as the controlling expression is true (1). Its syntax is

If the expression evaluates to a matrix, all its elements must be 1 for execution to continue. To reduce a matrix to a scalar value, use the all and any functions.

For example, this while loop finds the first integer n for which n! (n factorial) is a 100-digit number.

Exit a while loop at any time using the break statement.

while Statements and Empty Arrays.   A while condition that reduces to an empty array represents a false condition. That is,

never executes statement S1 when A is an empty array.

continue

The continue statement passes control to the next iteration of the for or while loop in which it appears, skipping any remaining statements in the body of the loop. In nested loops, continue passes control to the next iteration of the for or while loop enclosing it.

The example below shows a continue loop that counts the lines of code in the file, magic.m, skipping all blank lines and comments. A continue statement is used to advance to the next line in magic.m without incrementing the count whenever a blank line or comment line is encountered.

break

The break statement terminates the execution of a for loop or while loop. When a break statement is encountered, execution continues with the next statement outside of the loop. In nested loops, break exits from the innermost loop only.

The example below shows a while loop that reads the contents of the file fft.m into a MATLAB character array. A break statement is used to exit the while loop when the first empty line is encountered. The resulting character array contains the M-file help for the fft program.


Previous page  Conditional Control -- if, switch Error Control -- try, catch Next page

© 1994-2005 The MathWorks, Inc.