ECE105 Day6 Branches WhileLoops
ECE105 Day6 Branches WhileLoops
ECE105 Day6 Branches WhileLoops
licensing details
DAY 6
RELATIONAL OPERATORS
LOGIC OPERATORS
IF/ELSE
SWITCH/CASE, TRY/CATCH
WHILE LOOPS
ECE105 – Cody Anderson
The real power of programming
Our codes so far have been sequential: they carry out
each line in order
Operator Name
== Equal to
~= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
Important distinction:
= assignment operator
== evaluate if two variables are equivalent
Rounding errors
MATLAB double type is precise
to ~16 significant figures
Original matrix
|| OR (for scalars)
xor( ) Exclusive OR Output true if one and only one input is true
*The scalar operators (&& and ||) only work for scalar inputs,
but they are more efficient
Logic Operator Examples
false false
true
true
true
true
Branching statements
if/else
switch/case
try/catch
if/else statements
At a fork, only one path gets taken
Paths are evaluated in order, ignoring all
other paths
If Condition #1 is true, that branch is taken
(even if a later condition is true)
If Condition #1 is false, move on to evaluate
Condition #2
And so on…
Once an else statement is reached, that
branch is taken (no conditions)
Can have any number of elseif’s
It’s possible to never choose a branch (must
have no else statement)
if/else statements
• a = 3; b = 1; 4
• a = 5; b = 0; ‘cat’
• a = -2; b = 1; -9
Example: Zeros of quadratic equation
Based on discriminant (value under the sqrt), a
quadratic equation could have 0, 1, or 2 real zeros
Nested if statements
Can have any number of
nested if/else branches
Follow the default tabbing;
otherwise it is difficult to
understand
Evaluate each condition one
at a time, top-to-bottom
For example, if
age = 25
You are not single
salary = 70,000
Then you would be living
in your own house
switch/case branches
Similar to if/else statements, but more efficient when a
branch could be selected under multiple conditions
Value to
compare to
the cases
If num== any of
values listed in the
cell array, then that
branch is chosen
“otherwise” performs
the role of “else”
try/catch branches
Useful for debugging or to compensate for user error
If no errors, the try branch is processed and the catch branch ignored
Once an error occurs, jump to the catch branch
While loops
Go through the loop until a condition becomes
false
A while loop is a block of commands that are repeated
indefinitely as long as the condition is true
Once the condition is false, the code continues after the end
statement
The condition is only evaluated at the top of the loop:
If the condition becomes false in the middle of the code block, the
code block is taken to completion
Infinite loops are possible. To escape one, press ctrl+c in the
command window.
while loop
example
To track the progress of
each variable through a
loop, use a table like the
one below
[0 0 5]
q
5
6
7
8
9
There will never be a time when
the condition is false 10
…
To halt an infinite loop (or any
MATLAB operation), press Ctrl+c
in the Command Window
*that’s Command+c
on a Mac
Except where otherwise noted, this work is licensed under the Creative
Commons Attribution-NonCommercial 4.0 International License.