5. Control Loops for, while if

while Loops

A while loop allows a group of commands to be repeated an indefinite (unknown) number of times until an expression becomes false (is no longer satisfied). It is typically of the form

while expression

commands .......

................

end

The commands between the while and end statements are executed as long as all elements in expression are satisfied.

Example: For what value of n does the summation

wile sum

 

first exceed 30?

clear all

s=0;

k=0;

while s<30

      k=k+1;

      s=s+1/sqrt(k);

end

number_of_terms=k

sum=s