Loops in MATLAB

For Loop

1. “For” loop repeats a group of statements a fixed, predetermined number of times.
2. A matching end delineates the statements.
The general syntax is as follows:
for variable = initval:endval
statements

statements
end                                                                                                                                                                                               Example :

While Loop

1. “While” loop repeats a group of statements an indefinite number of times under control of a logical condition.
2. A matching end delineates the statements.
The general syntax is as follows:
while expression
statements
end
Example :

Break and Continue

1. The break command can be used to terminate a loop prematurely (while the comparison in the first line is still true).
2. A break statement will cause termination of the smallest enclosing while or for loop.


3. The continue command is similar to break ; however, instead of terminating the loop, the program just skips to the next pass.

Midpoint Break Loop

1. In these constructs the loop is entered, calculations are processed, and a decision is made at some arbitrary point in the loop whether or not to exit.
2. Then additional calculations are processed and the loop repeats.                                                                                         3. This strategy can be used either with a for loop or a while loop.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s