2024 Spring ICS Week14
2024 Spring ICS Week14
2024 Spring ICS Week14
LOOPS
LOOPS – The Loop Concept
• Without loops, computers would be little more than fancy calculators. Loops give
a computer its greatest power power—whether that computer is your laptop,
your cell phone, or your automobile’s automatic braking system.
• We learned about the control constructs—if, if-else, if-elseif, if-elseif-else, each
of which stipulates whether a block of statements is to be executed one time or
zero times.
• The loop construct can stipulate any number of executions of a block of
statements—zero, one, a hundred, a hundred thousand, or any other number.
LOOPS – The Loop Concept
• A loop for the addition of the integers from 1 to 10:
LOOPS – The Loop Concept
• Formal definition of the word “loop” as it is used in computer programming:
• 1a. (noun) a set of statements that is repeated until some condition is met.
• 1b. (noun) a control construct that causes a block of statements to be executed repeatedly
(i.e., zero, one, two, or more times).
• 2. (non-transitive verb) repeat a set of statements until some condition is met.
• In our example:
LOOPS – The For Loop
• It might be helpful to note the similarities between the syntax of the for-loop
and the syntax of the if-statement:
• Both constructs begin with a control statement and end with the keyword end.
• The control statement for each of them begins with a keyword—for in the for-loop and if in
the if-statement.
• The semantics of the two constructs are similar.
• The difference is that in the case of the if-statement that number is limited to zero or one,
whereas in the case of the for-loop the number is zero or some positive integer.
LOOPS – The For Loop
• The values assigned to the loop index do not have to be
• integers,
• regularly spaced, or
• assigned in increasing order.
• At the beginning of the n-th iteration of every for-loop, the loop’s control
statement will assign the loop index the n-th term in its list of values, regardless
of any value that may have been assigned to the loop index within the body of
the loop during the previous iteration.
LOOPS – The For Loop
• Every array operation and every matrix operation can be translated into an
equivalent for-loop version.
LOOPS – Nested For Loops
• As mentioned in the previous section, MATLAB allows the nesting of any control
construct inside any other construct.
• Nested for-loops often occur when we are doing things with two-dimensional
arrays.
• Here is the code that uses explicit loops to find the multiplication of an array
with itself:
LOOPS – The While Loop
• Suppose instead we wanted to sum the positive integers until we reached the
first sum that is greater than 50. Unless we knew that we needed to stop at 10,
we could not do this with a for-loop. What we need to solve this problem is a
while-loop.
LOOPS – The While Loop
• A comparison between the for-loop and the while-loop reveals the major difference between the two:
• Unlike the for-loop, the while-loop has no formal loop index. While we have chosen to use the same variable n in both the for-loop and
the while-loop, it is formally a loop index only in the for-loop.
• There are three important differences between the two types of loops concerning a counter, such as n:
• n must be initialized before the while-loop is entered, whereas initialization is unnecessary for the for-loop;
• n must be incremented in the body of the while-loop, whereas incrementing is unnecessary in body of the for-loop;
• the control-statement of the while-loop does not assign values to n, whereas the control-statement of the for-loop does.
• The key difference between the if-statement and the while-statement is that, after the
body is executed, the if-statement ends, whereas in the while-statement, the conditional
is evaluated again. As long as the conditional is true (i.e., is nonzero), the body will be
executed, and re-executed, and re-executed, etc.
LOOPS – Infinite Loops and Control+C
• The following piece of code tries to find an approximation to the square root of
x:
• We might hope to get an ideal square root by setting the acceptable level to
zero. We can try that by changing the control statement to this:
• Unfortunately, this idea does not work because the value of abs (y^2 – x) will
never get to zero.
• A loop that continues iterating without any possibility of stopping is called an
infinite loop.
LOOPS – Infinite Loops and Control+C
• When MATLAB is caught in an infinite loop and nothing is being printed to the
Command Window, it may at first glance appear that the MATLAB system has died.
• Fortunately, in this circumstance MATLAB displays a small sign of life that shows that is
still with us. While MATLAB is working on your program, whether it is making good
progress or is caught in an infinite loop, it displays the word “Busy” just to the right of
the Start button at the bottom left of the Command Window as a signal that it is
working.
• Any time that we suspect that it is wasting time instead of making progress, we can tell
MATLAB to stop running the program that we have given it without killing MATLAB itself.
It is done with a Control+C. A control command is issued by holding the Ctrl key and
hitting the c key. Its meaning is roughly, “Abort!”.
LOOPS – Changing the Loop Flow with Break and Continue
• Suppose, that we want to set all the values in the vector named readings to zero
until we reach the first value that exceeds 100. For example, suppose we the
vector has these values:
• The first value that exceeds 100 is 115, so in this case we would want to set the
values before 115 to zero:
• The meaning of the break-statement is that the loop is ended and control
continues at the next statement following that loop.
• The break-statement can appear only inside a loop. In fact, MATLAB will stop
the program and print an error message, if it encounters the keyword break
anywhere that is not inside a loop.
LOOPS – Changing the Loop Flow with Break and Continue
• The break-statement can be used in a while-statement as well.
LOOPS – Changing the Loop Flow with Break and Continue
• In nested-loops, the break applies only to the innermost loop, meaning that it
will cause the loop it appears in to terminate, but the outer loop will continue.
• Here is an example. Suppose we have the following array.
• We wish to look at each element in row-major order, and set them to zero until
we find the first value that is greater than 90. Here is the result we want:
LOOPS – Changing the Loop Flow with Break and Continue
• To look at each element in row-major order, we need a nested for-loop, so let’s write
one and include a break-statement to stop the processing when we reach a value
greater than 90:
• When we hit a value greater than 90, we break out of the loop. Sounds good, but
which loop do we break out of? Only the inner loop. This does not accomplish what we
wanted. Here is the result:
LOOPS – Changing the Loop Flow with Break and Continue
• The only way to cause the outer loop to terminate is to include a statement in
the inner loop that writes a note and another statement in the outer loop that
reads it. That note, which in common programming terminology is called a flag
which is a value indicating a special condition.
• If the loops are nested three deep, then you need two additional if-statements
containing break-statements; deeper nesting requires more if-statements and
break-statements, but only one flag is needed, no matter how deep the nesting.
LOOPS – Changing the Loop Flow with Break and Continue
• The continue-statement, which consists of the single keyword continue, causes the
innermost loop to continue to the next iteration without completing the current
one. It is used when it is determined, while executing the statements in the body
of the loop, that all the subsequent statements in the body of the loop should be
skipped.
• Unlike the break-statement, the continue-statement has no effect on the number
of iterations that are carried out.
LOOPS – Changing the Loop Flow with Break and Continue
• Suppose, for example, that we want to print five powers of each of the numbers in a list. For each number x we wish to print x,
x^2, x^4, x^(1/2), and x^(1/4). However, we are not interested in complex numbers (for some reason), so, if the number is
negative, since the last two values would be complex, we do not want to print (or calculate) them. Here is way to do it without
using a continue-statement:
• In this simple example, the first three fprintf statements will be executed. for each number in the list, but the last two will be
executed only if x is nonnegative.
• Here is how we do it with a continue-statement: