Scilab programming – WHILE loops

In this article we are going to learn about:

  • WHILE loops in Scilab
  • infinite loops in Scilab

How to install Scilab

Before going through this article, it is recommended to have a basic understanding about:

Similar to FOR loops, in Scilab we can use the WHILE loop. It’s syntax is a bit different and depending on the need, WHILE loops could be more easy to implement than FOR loops.

WHILE loop syntax:

Scilab programming - WHILE loop synthax

The condition is usually a comparison of a variable to a constant. The instructions are a set of Scilab operations that are execute as long as the condition is true.

As example we’ll use the same function from the FOR loop article.

\[\begin{equation*} \begin{split} f(x) = x^2 + \sqrt(x) \end{split} \end{equation*}\]

We need to evaluate this function for x = 1, 2, 3, 4, 5

The easiest way is to open SciNotes (Scilab script editor), write the following instructions, save the file and run the script (press <F5> key):

x = 1;
while x<=5
    f(x) = x^2 + sqrt(x);
    x = x + 1;
end

The first line (x = 1;) is initialising the variable x with 1. The second line contains the keyword while which opens the loop, followed by the condition (x<=5). On the third line we enter the instruction to be executed as long as the condition is true. This is when the function is calculated for the current value of x. On the fourth line we increment the variable x with 1, so the next step x will be equal to 2. On the fifth line with the keyword end we close the while loop.

In the image below you can see a logical diagram of the while loop, the events that happen during the loop and the corresponding Scilab instructions for each event.

Scilab programming - WHILE loop diagram

Image: Scilab programming – WHILE loop diagram

The result of the while loop will be a vector f which contains the function evaluation for x = 1, 2, 3, 4, 5

-->f
 f =
 
 2. 
 5.4142136 
 10.732051 
 18. 
 27.236068 
 
-->

For a better understanding of the while loop we are going to go step by step through the instructions and evaluate both the function and the condition.

Step Value of x Condition (x<=5) Actions
1 1 TRUE f = 2 x = 2
2 2 TRUE f = 5.4142136 x = 3
3 3 TRUE f = 10.732051 x = 4
4 4 TRUE f = 18 x = 5
5 5 TRUE f = 27.236068 x = 6
6 6 FALSE not executed, loop exited

We could use nested WHILE loops as well. Nested loops means one loop inside another one. Let’s use the same example as for the FOR loop in which we need to generate a 5 x 5 matrix.

Infinite loops

If by mistake or intentionally, the condition of the while loop is never going to be FALSE, the loop is going to be executed forever. To break such an infinite loop we need to left-click on the Scilab console (activate it) and press <Ctrl> + <C> keys.

Example of WHILE infinite loop:

while (1)
    disp("Infinite loop!")
end

Depending on the application, you’ll find more comfortable to use the WHILE loop instead of the FOR loop. Most of the time they can do the same thing but it’s worth having programming experience with both of them.

For any questions, observations and queries regarding this article, use the comment form below.

Don’t forget to Like, Share and Subscribe!

Leave a Reply

Ad Blocker Detected

Dear user, Our website provides free and high quality content by displaying ads to our visitors. Please support us by disabling your Ad blocker for our site. Thank you!

Refresh