How to use for loop (descending order)

i want to execute for_loop like (n→n-1→n-2…)

i wrote the following code but it didn’t work.

n=3
for i in n:1
...
end

Please tell me how to solve it.

2 Likes

for i in n:-1:1 without a step, the default step 1 is used.

2 Likes

As @mauro3 said, change your code like this

n=3
for i in n:-1:1
...
end

You can also use

for i in reverse(1:n)
    ...
end

(Fixed typo noted by @rdeits)

5 Likes

thanks!!
it worked well.