11 Control Sructures

# 11 Control Sructures - 11 Control Structures In all the...

This preview shows pages 1–2. Sign up to view the full content.

7 - Control Structures - 1 11 - Control Structures In all the code we’ve looked at thus far, the computer has followed commands one by one. A control structure is a command which allows the computer to make a decision and act accordingly. Control structures fall in two categories: branches and loops . Conditional control structures We begin with branches, also known as conditional control structures . A branch tests to see if a condition is true. If it is, then it does something. If not, it skips to the next part of the program. MATLAB branch commands have the following form: if condition expression(s) elseif condition expression(s) else expression(s) end There can be any number of elseif’s . For example, imagine we were purchasing box cars for a railroad company and the price of each car depends upon how many we purchase. If we buy 3 or less, the price is \$22K each. If we buy between 4 and 10, we pay \$20K each. And if we buy more than 10, the price is \$19K each. Here is a function that will return the total cost of railroad cars based on the number that we want to buy: %carprice %calculates the total price railroad cars %based on the number being bought %E77 reader %07-01 D# function [cost] = carprice (number) if number > 10

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 4

11 Control Sructures - 11 Control Structures In all the...

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

View Full Document
Ask a homework question - tutors are online