To control the flow operations in an algorithm, one must be able to verify some conditions and act accordingly. Other-times it might be required to perform same task repeatedly until certain conditions are met. Conditional execution are the backbone of any algorithm. In this article we will look through common conditional and looping expressions. We encourage readers planning to learn Octave/Matlab to try the exercises marked as “Ex“ along with the examples used in this article.
“if” statement is the most common conditional execution block in any algorithm. It executes the part of block only the boolean expression provided after “if” is true. Boolean expression after “if “is known as expression. To end the loop, “end” statement is used.
>> x = 10; >> if x>0 disp([num2str(x) ' is positive']) end 10 is positive >>
“if” statement can also be used with an “else” statement and thus some action can be performed in both cases either the condition is true or false.
>> x = -2; >> if x>0 disp([num2str(x) ' is positive']) else disp([num2str(x) ' is negative']) end -2 is negative >>
It is often the case algorithm night require to verify more than one condition and then do accordingly. in such case “elseif” condition can be used as many time required.
>> x= 0; >> if x>0 disp([num2str(x) " is positive"]) elseif x==0 disp([num2str(x) " is zero"]) else disp([num2str(x) " is negative"]) end 0 is zero
A “switch” is a series of checks on a variable. outcome of this statement is comparable to “if..elseif..else“, but execution is not. In “switch case” execution happens as per variable, multiple condition checks are not done.
>> x = 3; >> a = 2; >> b = 5; >> switch x case 1 %(if x==1) res = a+b; case 2 %(if x==2) res = a-b; case 3 %(if x==3) res = a*b; otherwise %(if none of the condition are met) res = a/b; end >> res res = 10
“while” is a looping statement with boolean condition. “while” loop keeps on executing the same block of code until the condition is true. while loop is preferable when the number of iteration is not known.
>> limit = 10; >> x = 0; >> while limit > 0 x = x + limit; limit = limit-1; end >> x x = 55
Loop can be terminated in mid also using “break” statement.
>> x= 0; >> while 1 x= x+1; if x>4 break; end disp(num2str(x)); end 1 2 3 4
When number of iteration is known and steps are equidistant, for loop is preferable for algorithm to work efficiently.
>> for i = 1:10 disp(num2str(i)); end 1 2 3 4 5 6 7 8 9 10 >> for i = 1:2:10 disp(num2str(i)); end 1 3 5 7 9
Colon operator is preferred choice in for loop, but not the only way to use a for loop.
Ex: Iterate for-loop across array of row vector. Ex: Different statements used here are directly typed in command window. Create scripts for them with help of previous article. Ex: Create flow diagram for "for" loop.
After learning the conditional execution along with scripting, try to create a solution for any known problem and verify your script. keep learning, keep growing. Let us know your query and questions in comment section.
- Octave:3> Data Visualization
- Octave:2> Functions
- Octave:3> File Operations
- Octave:2> Conditional Execution
- Octave:2> Scripts and Algorithm
- Octave:2> Vectors and Matrices Basics
- Octave:1> Built-In Functions, Commands, and Package
- Octave:1> Expressions and operators
- Octave:1> Variables and Constants
- Octave:1> Getting Started
- GNU Octave – Scientific Programming Language