How to use for loop (descending order)

question
#1

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.

1 Like
#2

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

2 Likes
#3

As @mauro3 said, change your code like this

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

You can also use

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

(Fixed typo noted by @rdeits)

2 Likes
#6

thanks!!
it worked well.