30p. Conditional statements _printable_

30p. Conditional statements _printable_ - Conditional...

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

View Full Document Right Arrow Icon
1 ©2009 by L. Lagerstrom Conditional Statements • For an introduction to the basic concepts, see the associated video clip. • The conditional structure • The if statement and if-else statement • Using logical operators • The if-elseif statement • Temperature check example • The switch statement ©2009 by L. Lagerstrom The Conditional Structure One of the principal types of program control structures is a conditional (or branching, or if-else) structure. It is called a conditional structure because depending on the value of a certain condition, the script or program will do one thing or another. For example, the condition might be "is the value of x less than 10." If it is less than 10, then one group of instructions will be executed, and if is not less than 10, then another group will be executed. So it is like a branch or fork in the road of the program (thus the name "branching structure"). Once one or the other group of instructions is executed, the program moves on to whatever comes next. There are actually many possible variants of a conditional statement. The example just cited has two branches. Some might have a number of branches. (See the associated video clip for a diagrammatic description of the basic possibilities.) ©2009 by L. Lagerstrom The if Statement In Matlab, as in most programming languages, we use an "if" type of statement to implement conditional structures. (An alternative is the "switch" statement, which we'll see later.) The basic form is: if [some condition is true] [Do some instructions here. ..] end For example: if x<10 y = 25; %Assign the value 25 to y disp('Hello, Fred') %Display "Hello, Fred" end If the value of x is less than 10, then the code inside the if statement is executed. Otherwise, the code is skipped. ©2009 by L. Lagerstrom The if Statement, cont. Our sample if statement: if x<10 y = 25; %Assign the value 25 to y disp('Hello, Fred') %Display "Hello, Fred" end disp('Hello, Ginger') The "end" marks the end of the if statement, so that Matlab knows how far to skip down if the condition is not true. In this case, if x is not less than 10, it will skip down to the disp('Hello, Ginger'). Also note that we indented the lines of code inside the if statement. Although the indentation is not required, it's recommended, because it makes it easier to see what the code is doing. The Matlab Editor will automatically do the indenting for you. The words "if" and "end" are reserved words in Matlab (meaning don't use them for variable names of your own).
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
2 ©2009 by L. Lagerstrom The if-else Statement The next variation of an if statement to cover is the "if-else" statement. Here's a basic example: if x<10 y = 25; disp('Hello, Fred') else y = 38; disp('Hello, Ginger') end In this case we have two "clauses", an "if" clause and an "else" clause. If x<10 is true, then the code in the "if" clause is executed, while if x<10 is false, then the code in the "else" clause is executed. So it's an either-or situation: one of the two clauses is guaranteed to
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 / 6

30p. Conditional statements _printable_ - Conditional...

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