2

I have some confusion in:

for i = 1 to n

does this pseudocode code means for(i=1; i<n; i++) or for(i=1; i<=n; i++) ?

And if one from them, then what will be the pseudocode for another one?

1
  • 1
    I would interpret that as the second: n included. Obviously that means you'll have to do to n-1 if you don't want that. Commented Jun 12, 2021 at 14:43

2 Answers 2

5

It should be understood to include an iteration where i takes the value of n.

However, there are no strict rules for pseudo code, and so in case there is doubt about an actual piece of pseudo code, then this means that code didn't achieve what it was supposed to do: unambiguously describe an algorithm without any programming language in mind.

Here are some of the arguments why it should be understood to include n:

  • When in plain English we tell someone to count from 1 "to 100", we mean that they should include 100 at the end. This is probably the strongest argument.
  • I know of no programming language that uses the to keyword in a for loop syntax, where the value that follows to is not to be included in the iterations. On the other hand, BASIC like languages (such as ) have that syntax, and the "to" value will be used for the final iteration.
  • On Wikipedia some examples of pseudo code loops are given, and where the to keyword is used, the final iteration is using that value.
  • If the intention of this loop is to visit each value in an array of n elements (a very common case), then there is no ambiguity: for i = 1 to n tells us that the array's first element has position 1, and hence its last position is position n. It is not uncommon to use such 1-based indexing of arrays in pseudo code, as this is more intutive to non-programmers. If however in such array context you see for i = 0 to n in pseudo code, some alarms should go off. That definitely would come across ambiguous.
Sign up to request clarification or add additional context in comments.

Comments

2

It is highly contextual and depends on the person who wrote it. (It also kind of depends on the programming language that the pseudocode is aimed to be converted to.)

If it were in an interview setting, it might be a good idea to ask if this is "inclusive of n or not". Because it could be either case.

(I know this answer might not be that helpful. Sorry about that, but it doesn't seem that this has a definitive answer.)

8 Comments

thanks, so it is better for me to declare clearly which case I want to use (if I write the pseudocode).
no problem. Yes, exactly. That's probably the clearest way of writing it. Or just using the regular for (i=1; i<n; i++) or for (i=1; i<=n; i++) instead of using pseudocode for that part.
Can you provide a reference which uses the syntax 1 to n in pseudocode to mean not including n? I would think it always means including n, I would not expect a credible textbook or other authoritative source to write 1 to n without meaning to include n.
@kaya3 I don't have an example of a language that does that, given that a for ... to ... loop is not a common programming language [loop] option (as pointed out by @trincot in their answer). I agree that it would make sense for this kind of loop to be inclusive (from a linguistic standpoint). But it's still possible for it to be misunderstood by some. So it might be the best idea to be clear about its functionality.
I mean an example of a textbook or other reference material which writes pseudocode in this way. If it does depend on context then there ought to be examples where, in context, it should be understood to mean that.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.