Simon
Simon

Reputation: 335

pseudocode with FOR

for j = n − 1 to 1 step − 1 do

does this means for all j in [1 .. n-1] or [2 .. n-1] ?

Upvotes: 1

Views: 3455

Answers (3)

Raj More
Raj More

Reputation: 48016

for j = n − 1 to 1 step − 1 do is a countdown.

It will start at j and count down to 1 (inclusive).

So if j is 6, then it will go down 5, 4, 3, 2, 1.

twist: Now if your j is a negative value, then you should consider that it will go upto the min negative first, then rollover to the max positive and then count down to 1.

Upvotes: 3

T.E.D.
T.E.D.

Reputation: 44804

Seems pretty clear to me. That means it counts down from n-1 to 1.

Upvotes: 0

Chris Knight
Chris Knight

Reputation: 25074

If n=5 then the for loop is executed 4 times with J equal to 4 then 3, then 2, and finally 1 in that order

Upvotes: 1

Related Questions