CS 211 Lesson 8

Repetition: for Loops

Quote:

I am not bound to win, but I am bound to be true. I am not bound to succeed, but I am bound to live by the light that I have. I must stand with anybody that stands right, stand with him while he is right, and part with him when he goes wrong. Abraham Lincoln

Lesson Objectives:

Lesson:

I. MATLAB Concepts

A. Review of loops

B. The for loop

C. Nested loops

D. The break statement

E. The continue statement

for Index = 1:length(Vector)
     if Vector(Index) >= 0
          % process the Vector(Index) element in some way
     end   
end

II. Good Programming Practices

for Index = 1:length(Vector)
     if Vector(Index) > 80
          Vector(Index) = Vector(Index) - 10;
     end
end

        Vector = Vector - 10;

III. Algorithms

A. Find the largest value in an array

 

Lab Work: Lab 8

References:  Chapman Textbook: section 4.2