For in Scilab
In Scilab, loops can be constructed using various methods. Here are some basic types of loops equivalent to the original example.
To run this Scilab script, save it to a file (e.g., for_example.sce
) and execute it in Scilab:
Let’s break down the differences and similarities:
Scilab uses
while
for condition-based loops, similar to the first example in the original code.The classic
for
loop in Scilab uses a different syntax. Instead of specifying initialization, condition, and increment separately, Scilab uses a range (1:3
means from 1 to 3).Scilab doesn’t have a direct equivalent to the
range
function over integers. We simulate this by using a regularfor
loop and adjusting the output.An infinite loop in Scilab is created using
while %T
(%T represents true).Scilab supports
break
andcontinue
statements similar to other languages.Scilab’s indexing starts at 1, not 0, so we adjust our calculations and output accordingly.
Scilab uses
disp()
function for output instead ofprint
orprintln
.
These examples demonstrate various looping constructs in Scilab, covering most common use cases for iteration.