• Congratulations to the Class of 2024 on your results!
    Let us know how you went here
    Got a question about your uni preferences? Ask us here

for..next loops in the hsc (1 Viewer)

raymes

Member
Joined
Jan 21, 2004
Messages
116
Location
Sydney
Gender
Male
HSC
2004
can we use for..next loops in our hsc algorithm responses?
it first i thought you couldnt
i thought i better check
 

hornetfig

Member
Joined
Jun 27, 2004
Messages
65
Location
Sydney. oddly
Gender
Male
HSC
2003
we were always told no. but then then you see example after example that does use them, and then you wonder.

Better to be safe than sorry I suppose; I think it would be safe to say you wouldn't be penalised for not using it.
 

J0n

N/A
Joined
Aug 28, 2003
Messages
410
Gender
Male
HSC
2004
We better be able to use them - i use them all the time! :D They were used in Q23 of the 2002 HSC, so if a question can use them, why shouldn't we be allowed to answer with them? Hmm... maybe i should start using DO WHILE's instead...

Btw, it seems that the Fowler book and the HSC use FOR ... ENDFOR (could only find VB code FOR it in the Davis book), but i always use FOR ... NEXT because it is similar to VB - i should be allowed to use that instead, right? (providing we can use FOR loops at all)
 

raymes

Member
Joined
Jan 21, 2004
Messages
116
Location
Sydney
Gender
Male
HSC
2004
technically you're not supposed to, theyre not covered in the "methods for algorithm description" document issued by the BOS (older version can be found here http://www.boredofstudies.org/other/1995_SDD_N_Methods_of_Algorithm_Description_BOS.pdf but its exactly the same as the new one). but i wanted to know if it was acceptable to use them. the portion of the document specifying psuedocode just for quick reference:
Pseudocode Guidelines
• The keywords used for pseudocode in this document
are:
for start and finish
BEGIN MAINPROGRAM, END MAINPROGRAM
for initialisation
INITIALISATION, END INITIALISATION
for subprogram
BEGIN SUBPROGRAM, END SUBPROGRAM
for selection
IF, THEN, ELSE, ENDIF
for multi-way selection
CASEWHERE, OTHERWISE, ENDCASE
for pre-test repetition
WHILE, ENDWHILE
for post-test repetition
REPEAT, UNTIL
 

SamD

Member
Joined
Jul 21, 2002
Messages
256
Gender
Male
HSC
N/A
For...Next loops are referred to in the SDD Prelim course, and in the SDD Course Specifications. The course specifications use FOR...ENDFOR but the syllabus says FOR...NEXT. I've covered them in my Prelim text and have said either syntax is OK.

As a consequence it should be OK to use them when writing Pseudocode, however they should be avoided on flowcharts.

HTH
Sam
 

J0n

N/A
Joined
Aug 28, 2003
Messages
410
Gender
Male
HSC
2004
I didn't know there was a new "methods of algorithm description" document - where do we find that??
 

hornetfig

Member
Joined
Jun 27, 2004
Messages
65
Location
Sydney. oddly
Gender
Male
HSC
2003
It's a war of Pascal against BASIC in the Board of Studies!

SamD said:
For...Next loops are referred to in the SDD Prelim course, and in the SDD Course Specifications. The course specifications use FOR...ENDFOR but the syllabus says FOR...NEXT. I've covered them in my Prelim text and have said either syntax is OK.

As a consequence it should be OK to use them when writing Pseudocode, however they should be avoided on flowcharts.

HTH
Sam
 

J0n

N/A
Joined
Aug 28, 2003
Messages
410
Gender
Male
HSC
2004
raymes said:
this document was put out after the new course, but as i said, the specs are the same
http://www.boardofstudies.nsw.edu.au/syllabus_hsc/pdf_doc/softwaredesign_specs.pdf
Oh cool, thanks :D

In that document, above the methods of algorithm description part, there is some stuff about what the languages we study should contain, and in the syntax of a FOR ... NEXT loop, it has:

FOR <control variable> taking <initial value> TO <final value> BY steps of 1 DO

How what would be the pseudocode syntax for a loop like that? e.g. to count backwards from 10 to 1?
 

SamD

Member
Joined
Jul 21, 2002
Messages
256
Gender
Male
HSC
N/A
To count back from 10 to 1 you'd write...

FOR Count taking 10 TO 1 by steps of -1 DO
....
ENDFOR

Although this is OK, as it's in the Specifications, to my knowledge nobody really does this. I'm 99.999% confident that writing the following would be OK too...

FOR Count = 10 TO 1 STEP -1
...
NEXT

But personally I'd avoid FOR...NEXT loops altogether when writing algorithms.

HTH
Sam
 

J0n

N/A
Joined
Aug 28, 2003
Messages
410
Gender
Male
HSC
2004
Oh, ok, thanks :D
SamD said:
But personally I'd avoid FOR...NEXT loops altogether when writing algorithms.
Why wouldn't you use FOR...NEXT loops? - do you mean just to be safe for the HSC, or is there actually some other reason not to use them?
 

SamD

Member
Joined
Jul 21, 2002
Messages
256
Gender
Male
HSC
N/A
In opinion the logic is not so clear.

Some example issues include:

What is the value of Count once the loop FOR Count=1 TO 10 completes? Is it 10 or is it 11? I think in most programming languages it would be 10, but I may be wrong! And what should it be in psuedocode? Who knows! Hence it's dangerous to use the value of the loop counter after the iterations finish.

You can't get out of a FOR...NEXT loop as elegantly as you can from a WHILE loop. You can reset the loop counter within the loop but that seems a bit bodgy to me.

You can't add an extra condition. For example you can't have FOR X=1 TO 10 OR End of File. This makes it difficult if you realise half way through answering a question that you'd like to get out the loop under certain circumstances.

FOR...NEXT loops are a particular implementation of Pretest loops. So why not just explain the full logic using a WHILE structure. The amount of extra work is pretty trivial.

HTH
Sam
 

Winston

Active Member
Joined
Aug 30, 2002
Messages
6,128
Gender
Undisclosed
HSC
2003
For loops are just While loops which incorporate a initialiser and a incrementer.
 

hornetfig

Member
Joined
Jun 27, 2004
Messages
65
Location
Sydney. oddly
Gender
Male
HSC
2003
SamD said:
What is the value of Count once the loop FOR Count=1 TO 10 completes? Is it 10 or is it 11? I think in most programming languages it would be 10, but I may be wrong! And what should it be in psuedocode? Who knows! Hence it's dangerous to use the value of the loop counter after the iterations finish.
And many compilers will dereference it after the loop has completed it any case. Some even make it illegal to refer to a counted loop variable outside the counted loop.

You can't get out of a FOR...NEXT loop as elegantly as you can from a WHILE loop. You can reset the loop counter within the loop but that seems a bit bodgy to me.
indeed, but this isn't really a practical concern seeing as most environments provide standard break/exit loop (and continue/skip) functions. There's no reason not to really when the whole thing just breaks down to JMP (x86) codes in the end. But I agree in BOS pseudocode there is no real way to terminate a counted loop.

You can't add an extra condition. For example you can't have FOR X=1 TO 10 OR End of File. This makes it difficult if you realise half way through answering a question that you'd like to get out the loop under certain circumstances.
Tip to students: whenever writing these silly algorithms, do them double spaced so when you work out you've chosen the wrong loop type can can cross it out!

FOR...NEXT loops are a particular implementation of Pretest loops. So why not just explain the full logic using a WHILE structure. The amount of extra work is pretty trivial.
This is true but they are also the most commonly used loop statement in programming. You'd almost expect them in BOS pseudo-Pascal pseudocode...

Despite this, I am still suggesting students avoid using them. As Winston says, they are just WHILE loops with a initialiser and incrementer.
 

Winston

Active Member
Joined
Aug 30, 2002
Messages
6,128
Gender
Undisclosed
HSC
2003
Uno what haha i just sort of looked at it again and For loops, the ones mentioned above really don't look to flexible, it's really not as convenient to exit, but as stated there are exit loop statements within languages, however, if everyones refering the for loop as the one in VB, it's not that good. I've totally forgoten about VB so i'm under the impression the above for loop is the one that only exists. It's pretty dodgy, the ones in other languages, like java, C# is a little more flexible:


for(int i = 0, i < someNumber; i++)
{
//Do something
}

For me to break out of this loop, i could very well just set the 'i' variable to the condition which will cause it to discontinue it's loop.

But meh, back on the topic, i'd just discourage students to use the for loop, just stick with your WHILE loop and make your own initialisers and incrementers.
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top