Page 1 of 1
Trouble with this psuedo code...
Posted: Tue Mar 24, 2009 9:41 pm
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
Re: Trouble with this psuedo code...
Posted: Tue Mar 24, 2009 10:33 pm
by DeletedAccount
Hi,
Dude its the first one ,if you have picked it up from an algorithm text or something
Regards
Shrek
Re: Trouble with this psuedo code...
Posted: Tue Mar 24, 2009 10:39 pm
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.
Re: Trouble with this psuedo code...
Posted: Tue Mar 24, 2009 11:08 pm
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
Re: Trouble with this psuedo code...
Posted: Wed Mar 25, 2009 2:33 am
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 )
Re: Trouble with this psuedo code...
Posted: Wed Mar 25, 2009 4:20 am
by Combuster
True, in BASIC you have the FOR var = a TO b syntax, in which a and b are inclusive.