NoteSet_2_v1
NoteSet_2_v1
• The word if is arguably the most important word in programming. But before
we jump into coding, let’s think about real-world examples of the word.
1
o The word if tells the software that you are beginning this conditional
statement.
o The line immediately after the word if is the condition, i.e., the thing
that needs to be true to move into the block of code.
▪ If that condition isn’t met, the software will ignore everything
within the block.
2
x = -4;
if x < 0
disp('x is Negative') %displays in Command Window
end
x = -4;
if x < 0
disp('x is Negative') %displays in Command Window
x = -1*x; %makes x into a positive number
end
disp(x) %displays the final value of x, which is always > 0
• Note that the disp function will display words when enclosed by apostrophes.
o If there are no apostrophes (like the last line), then it will display the
value of the variable.
o If that variable doesn’t exist, there will be an error.
• The if-end statements described in the previous section are fairly limited: it
only checks if something is true and responds accordingly.
3
x = 7;
if x < 0
disp('x is Negative')
else
disp('x is Positive')
end
• Note that the word else does not exist on its own: it needs the word if to work
properly.
o Additionally, despite the fact we have more scenarios, we only require
one end statement.
• Is there a value of x where the code will not perform as expected? More on
this after the following example.
Example #1
• Greg wants to save $300 to buy a Nintendo Switch at the end of 4 weeks.
• His paycheck each week is $100.
o When he gets paid, the government takes out 25% of his paycheck,
i.e., he can save 75% for each paycheck.
• Write a code to test out if we will succeed in saving the money.
4
check_amount = 100; %weekly total
savings_oneWeek = 0.75*check_amount; %weekly savings
weeks = 4; %number of weeks
x = 7;
if x < 0
disp('x is Negative')
else
disp('x is Positive')
end
5
• What value of x will make this code output an incorrect statement?
o Well, if x = 0, the Command Window will say that this is a positive
number…but this isn’t true since 0 does not have an affiliated sign.
o Is there a way to account for this?
• This is where we include another possibility for our if statement, in the form
of elseif.
• Including this line (or several of these lines) allows for multiple conditions.
The modified code below illustrates this.
x = 4;
if x < 0
disp('x is Negative')
elseif x > 0 %elseif requires a condition written after
disp('x is Positive')
else %else does not require a condition
disp('x is 0')
end
6
z = 7;
if z < 10
disp('z is less than 10')
elseif z > 0
disp('z is positive') %this will not display for z = 7
end
z = 7;
if z < 10
disp('z is less than 10')
end
if z > 0
disp('z is positive')
end
Example #2
7
x = 7;
if x == 4 %first specific check
disp('x = 4')
elseif x == 6 %additional specific check
disp('x = 6')
elseif x == -3 %additional specific check
disp('x = -3')
else %everything ELSE that wasn't asked yet
disp('x is NOT 4, 6, or -3');
end
• What if you want to only run a code if multiple conditions are true?
o Maybe you’re running a code that designs the width of a cantilever
beam.
o You need to ensure that the beam does not exceed its maximum stress
(i.e., it breaks) AND that it doesn’t exceed the cost of material.
o You provide a width value, run the code, and see the results if both
conditions are met.
8
o So, how do we do this?
• To execute this concept, we will use && to tie two conditions together.
o For example, Figure 3 shows a code where we have two variables x and
y and we want to check if they are both positive.
o We would say (in words): “if x is greater than 0 AND y is greater than
0, then say that they are both positive.”
▪ Else, display that at least one variable isn’t.
• This may be extended to any number of conditions with && signs appearing
after each condition. This is shown below in Figure 4.
Figure 4: Multiple and conditions. If any one of them fail, then the entire statement is considered false.
9
2.2 Multiple Conditions: or
A, A
A, F
F, A
F, F
• How do we code this? Similar to earlier, the if statement will be typed, but we
use two vertical bars in MATLAB to signify or.
• This is shown in Figure 5, where the | | bars are located above the Enter key
on your keyboard (hold shift to access them).
10
Figure 5: The or command using ||.
• So, what is happening with MATLAB when you use an if/or type of code?
o MATLAB is checking whether a condition is true or false, but how?
if 1
disp('True condition')
end
o Each of the operators we have seen so far (<, >, >=, etc.) secretly output
a one or zero.
o For example, if you wanted to compare the numbers 3 and 4, you will
see the associated responses in Figure 6.
11
• The concept of using 1’s and 0’s is called Boolean Logic or Boolean Algebra.
Figure 6: Left: The output of a true statement is 1. Right: The output of a false statement is 0
• One type of condition that we haven’t discussed is the concept of equality, i.e.,
if two things are equal, execute the code.
• As described in NS#1, the equal sign is used for assigning one expression to
a variable. This command does not output a 1 or 0 for checking equality.
o The code below would produce an error since the if statement is
checking for a one or zero. The single equal sign does not do this.
x = 1;
y = 1;
if x = y %this line is problematic!
disp('x and y are equal')
end
• Instead, we will use two equal signs to ask MATLAB to compare two
values. Below is the fixed version of the code above.
12
x = 1;
y = 1;
if x == y %this is correct now
disp('x and y are equal')
end
• The other condition is the not equal to command, where we run the code if
two expressions are not equivalent. We write this as a tilde sign (~), followed
by an equal sign (this symbol is located below your Escape Key on the top
left of your keyboard, assuming you have a standard keyboard).
o The code below provides an example.
x = 1;
y = 2;
if x ~= y %not equal command
disp('x and y are NOT equal')
end
13
• For example:
o Let’s say you want to write a code where a student can test what letter
grade they receive.
o The student knows their percentage grade (e.g., they received a 86%
in the class) but they haven’t checked the syllabus and do not know
whether this is an A.
o You send them the code and the code asks them to supply their grade.
Once supplied, the code will run and produce an output.
• The command for this is input, where you will type this word in your code
followed by what you want the user to see. Below is an example.
• If you ran this statement, the Command Window will look like Figure 7.
o Note that the code is now paused and waits for the user to type
something in and click enter.
o After clicking enter, the variable x is now defined and you can do
whatever you would like with it.
Figure 7: Using the input function, where the code is paused until the user types in a number.
14
Figure 8: An example of asking the user for a number and then using it for another operation.
15
Section 3: Examples and Exercises
x = 3;
y = 6;
if x == y
z = x;
elseif x ~= y
z = y+x;
end
disp(z)
a) 3
b) 6
c) z
d) 9
e) 12
f) Error
a) Variable assignment
b) Boolean operator for equality
c) Not equal to
d) Or
3) There are four errors in the code below. What are they?
if pressure = 10
disp(pressure is high)
else
disp(pressure)
16
4) What does the following code output in the Command Window?
g = 4;
h = 28;
if h/g > 7
disp('g')
elseif h/g < 7
disp(h)
elseif g/h == 7
disp(g)
else
disp('h')
end
a) h
b) 28
c) 4
d) g
e) Error
17
Answers: Multiple Choice
x = 3;
y = 6;
if x == y
z = x;
elseif x ~= y
z = y+x;
end
disp(z)
So, we have assigned 3 to x and 6 to y. The first part of the if statement checks if
they are equal: they are not, so we will continue.
The next part of the statement is the else-if portion where the condition is “x is
NOT equal to y”. This is true, so we enter this statement. Within, we assign z to the
sum of x and y, so that would be 9.
At the end, we use disp(z). Since the z is not within apostrophes, that means we are
outputting the value of z and not the letter itself. Therefore, the output in the
Command Window is 9, i.e., answer choice d.
18
3) There are four errors in the code below. What are they?
if pressure = 10
disp(pressure is high)
else
disp(pressure)
First, we are checking a condition involving the variable pressure. The first error is
that we never defined it.
Third, the disp(pressure is high) does not make any sense since it is a sentence
and there are no apostrophes surrounding it. (Note, the disp(pressure) under the
else statement would be okay if pressure was defined originally. The spaces in
“pressure is high” make that input definitely not a variable, and therefore, the
apostrophes are needed).
g = 4;
h = 28;
if h/g > 7
disp('g')
elseif h/g < 7
disp(h)
elseif g/h == 7
disp(g)
else
disp('h')
end
Let’s go through line by line to understand what the output would be.
19
First, we assign 4 and 28 to g and h respectively.
The second asks if h/g is less than 7. We know that h/g is 7, so this condition is not
true as well.
The third is asking if g/h is equal to 7. Note that this is now g/h, NOT h/g. g/h is
4/28, which is 1/7. This condition is not true either.
The else statement contains every other possible case, so this is the code we will
run. It will be the letter h in the command window. So the answer choice is a.
20