Вопрос

I was trying to perform a descend loop in Scilab like:

for i=7: 1 
    disp(i);
end

But apparently it didn't work and there are no errors too.

Is this possible? And if yes, how to do it?

Это было полезно?

Решение

Syntax

As the for-loop documentation states, the for loop's syntax is:

for variable=expression do instruction, ,instruction,end

If expression is a matrix or a row vector, variable takes as values the values of each column of the matrix.

Your example

Your for-loop uses 7:1 as expression, what will result in a valid but empty vector:

-->7:1
 ans  =

     []

The for-loop then takes each value in the vector and then finishes, but since your vector is empty it finishes directly. So it is valid syntax.

Working example

The documentation gives the following example of a working decreasing loop:

for j=4:-1:1
   disp(j);
end // decreasing loop
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top