Tutorial by Examples

Conditions are a fundamental part of almost any part of code. They are used to execute some parts of the code only in some situations, but not other. Let's look at the basic syntax: a = 5; if a > 10 % this condition is not fulfilled, so nothing will happen disp('OK') end if a < 1...
In some cases we want to run an alternative code if the condition is false, for this we use the optional else part: a = 20; if a < 10 disp('a smaller than 10') else disp('a bigger than 10') end Here we see that because a is not smaller than 10 the second part of the code, after t...
Using else we can perform some task when the condition is not satisfied. But what if we want to check a second condition in case that the first one was false. We can do it this way: a = 9; if mod(a,2)==0 % MOD - modulo operation, return the remainder after division of 'a' by 2 disp('a is ev...
When we use a condition within another condition we say the conditions are "nested". One special case of nested conditions is given by the elseif option, but there are numerous other ways to use nested conditons. Let's examine the following code: a = 2; if mod(a,2)==0 % MOD - modulo o...

Page 1 of 1