If
all else
fails...¶logical
, you'll see¶As you saw in the previous lesson, while
-loops check a given condition each time the commands in the loop are executed.
It is natural to simply "read" the syntax for while
-loops as you see them, for example reading
while x<10
literally as "while x
is less than 10".
However this isn't quite how MATLAB sees it.
Consider what happens if we execute the following:
x = 0;
disp(x<10)
MATLAB will print the value of x<10
as being 1.
What if we set x
so that the statement doesn't hold?
x = 11;
disp(x<10)
In this case, the "value" of x<10
is 0.
In fact, we can save the value of this expression to a variable:
x = 0;
y = 11;
xStatement = x<10;
yStatement = y<10;
You'll see the variables xStatement
and yStatement
appear in your variable workspace, but look at what type (or class, if using an earlier version of MATLAB) of variables they are.
These are not floats
or chars
(strings) - they are logicals
.
logical
is a variable type used by MATLAB to interpret the truth value of statements.
x<10
is a statement which returns either false
(displayed as 0) or true
(displayed as 1), this value of true
or false
is stored as a logical variable.
As such the syntax for a while
-loop is better read as "while some condition is true
".
Since logical
is a variable type, we can perform logical manipulations (like "and", "not" and "or") with them that you should be familiar with from your first logic/analysis course:
tf1 = true;
tf2 = false; %you can directly create logicals by using the keywords true and false
%we can use the & symbol for "and"
disp(tf1 & tf2) %tf1 and tf2
disp(tf1 && tf2) %tf1 fast-and tf2
%we can use | for "or"
disp(tf1 | tf2) %tf1 or tf2
disp(tf1 || tf2) %tf1 fast-or tf2
%we can use the tilde ~ for "not"
disp(~tf1)
disp(~tf2)
If we have logical operations, we can put more complex conditions into our loops:
x = 2;
while x>0 && x<6-1e-8
x = 3 + x/2;
fprintf('New x value: %.10f \n', x)
end %while, x<0 && x<6-1e-8
%%this sequence converges to 6, so putting x<6 will result in a while loop that never terminates!
So long as the value of the statement that you provide comes out to be true
, MATLAB will run the while
-loop again!
NOTE: This can result in infinite loops - loops that never terminate! The simplist example of these is the code:
x = 0;
while true
x = x+1;
end %while true
DO NOT execute this in the command line, because MATLAB will diligantly sit there adding 1 to x
for all time.
You can force MATLAB to stop running the current script, function, or in general code that you've provided to it by hitting CTRL
+c
when in the command line.
Because of this reason, when you use while
loops you should only do so if you can guarantee that the process will end eventually; if you can't do this then common practice is to set a manual maximum iterations variable and use that to exit the loop:
maxItt = 1000;
currItt = 1;
while <CONDITION> && currItt<=maxItt
<STUFF YOU WANT TO DO>
currItt = currItt+1;
end %while
The condition currItt<=maxItt
will force the while
-loop to end if it goes on too long.
if
statements¶The final type of conditional statement that MATLAB has is the if
-elseif
-else
statement.
The syntax is as follows:
if <CONDITION>
<DO SOME COMMANDS>
elseif <CONDITION>
<DO SOME DIFFERENT COMMANDS>
else
<DO SOME ALTERNATIVE COMMANDS>
end %if <CONDITION>
Upon reaching an if
statement, MATLAB will check the condition immediately after the if
keyword.
If the condition comes back as true, the commands that follow the if
are run, and the commands after the elseif
and else
are ignored.
If the condition comes back as false, MATLAB then checks the elseif
condition; if this condition is true then the commands after the elseif
are run, and the other sets of commands are ignored.
If the elseif
condition is also false, MATLAB runs the commands after the else
until end
.
Notice that after else
there is no condition to check - this is the case when "all other options have failed", so to speak.
Let's look at a simple example:
myNumber = rand(1); %generate a uniform random number between 0 and 1
fprintf('Random number generated was: %.5f\n', myNumber)
if myNumber>2/3
fprintf('The random number generated is greater than 2/3 \n')
elseif myNumber>1/3
fprintf('The random number generated is between 1/3 and 2/3 \n')
%note: if we have to check the elseif condition, we know that the if condition was false.
%Thus myNumber is not greater than 2/3; IE is less than 2/3, should we be running these commands
else
fprintf('The random number generated is less than 1/3 \n')
end %if, myNumber>2/3
Of course, ranther than simply displaying the range that a value falls into, we can perform computations based on the cases in the if
statement.
For example, we could track the distribution of the random numbers MATLAB was generating:
nTrials = 1000;
upperThirdCounter = 0;
middleThirdCounter = 0;
lowerThirdCounter = 0;
for i=1:nTrials
randNumber = rand(1);
if randNumber>2/3
upperThirdCounter = upperThirdCounter + 1;
elseif randNumber>1/3
middleThirdCounter = middleThirdCounter + 1;
else
lowerThirdCounter = lowerThirdCounter + 1;
end %if, randNumber>2/3
%note that we don't use the loop variable i anywhere in this loop.
%but the loop will still run 1000 times, changing the value of i each time (even though we don't use it)!
end %for, i
fprintf('# Values >2/3: %d \n', upperThirdCounter)
fprintf('# Values between 1/3 and 2/3: %d \n', middleThirdCounter)
fprintf('# Values <1/3: %d \n', lowerThirdCounter)