Trouble with this psuedo code...

Programming, for all ages and all languages.
Post Reply
earlz
Member
Member
Posts: 1546
Joined: Thu Jul 07, 2005 11:00 pm
Contact:

Trouble with this psuedo code...

Post by earlz »

Ok, everyone thinks I'm probably an idiot not being able to understand this psuedo code but what does this do?
FOR 1 TO 10

I know it's a for loop incrementing by 1, but is it's C equivalent

Code: Select all

for(i=1;i<=10;i++)

or

for(i=1;i<10;i++)
I am pretty sure it's the last one but I'm just not completely for sure
DeletedAccount
Member
Member
Posts: 566
Joined: Tue Jun 20, 2006 9:17 am

Re: Trouble with this psuedo code...

Post by DeletedAccount »

Hi,
Dude its the first one ,if you have picked it up from an algorithm text or something

Regards
Shrek
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Re: Trouble with this psuedo code...

Post by pcmattman »

Shrek is correct assuming the context confirms that.

You'll need to give the context to the psuedocode in order to make sense of the individual line.
User avatar
01000101
Member
Member
Posts: 1599
Joined: Fri Jun 22, 2007 12:47 pm
Contact:

Re: Trouble with this psuedo code...

Post by 01000101 »

it really depends.

If it was meant as: FOR [the first object in an array] to the 10th [last object in an array + 1], then for(i = 0; i < 10; i++) would make sense.

If it meant: FOR 1 "TO" 10, then we don't actually use 10, but go up TO 10, thus "< 10" is the correct answer.

If it was a more elementary: FOR 1-10, then it would be assumed that the person wished for 10 objects to be addressed starting at "1", so <= 10 is the correct choice as 10 *should* be included
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re: Trouble with this psuedo code...

Post by Solar »

Canonically, "FOR 1 TO 10" means 1..10, i.e. including the 1 and the 10.

Anything else would really, really surprise me (and sane programming languages are designed not to surprise).

Code: Select all

for ( int i = 1; i <= 10; ++i )
// or
for ( int i = 1; i < 11; ++i )
Every good solution is obvious once you've found it.
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: Trouble with this psuedo code...

Post by Combuster »

True, in BASIC you have the FOR var = a TO b syntax, in which a and b are inclusive.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
Post Reply