Chapter 06

Chapter 06 - Chapter 6 Loops In the last chapter we...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 6 Loops In the last chapter, we introduced the idea of a control construct, a method by which Matlab will depart from its customary sequential processing. Initially, we accomplished this task through the use selection. That is, we used either an if or a switch statement to choose among a series of alternative commands to be executed. In this chapter, you will be introduced to another control construct: loops. 6.1 Repetition Imagine that you have just walked up to a large breakfast buffet. As you survey the offerings, you are reminded that you do not like green eggs or ham. Seeking to avoid digestive discomfort, you begin walking down the buffet line checking each dish in turn to ensure it does not include the offending foods. If we were to write this process as a program, you might create a long series of if statements to evaluate each dish for the presence of green eggs or ham. In programming, we often need to do something more than once. Add 10 to a series of numbers. Perform an element-by-element operation. Search through a set of numbers to find its maximum value. Review a data set to ensure no values fall below a specified safety limit. Each of these operations involves performing a single set of actions repetitively, though with small variations. In sequential processing, we would have to write a lengthy program that repeated those actions for each element. . It would be much more efficient if we could write the code for those actions once and then simply repeat them. A loop is a control construct that does just that. Matlab supports two types of formal loops: the for loop and the while loop. 6.2 The for Loop The for loop is designed to execute a series of statements a finite number of times. Its syntax is straightforward: for variable = expression statements end The variable above is known as an index and may be referenced like a variable by your Matlab program. The most basic function of an index is to keep track of how many times your loop has been executed. Traditionally, most programmers use the letter i for the index. In Matlab, however, i is used to represent imaginary numbers. To avoid
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
confusion, we’ll use ii as our index variable. As a Matlab programmer, you’re free to use any valid Matlab variable name for your index. The expression is used to determine how many times the loop should be executed. The expression takes the form of a vector. Usually this is created in shorthand form by means of the colon operator. The statements are the set of commands Matlab is expected to execute each time the loop is repeated. Finally, every loop is concluded with the word end . To understand how loops function, consider the following commands: for ii = 1:10 fprintf(‘\n Loop Count: %d’, ii); end When Matlab reaches the loop initially, a control structure is created. In this example, that structure is an array from 1 to 10: [1 2 3 4 5 6 7 8 9 10] as specified in the expression. Being the first time through the loop, our index variable,
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 17

Chapter 06 - Chapter 6 Loops In the last chapter we...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online