BBC BASIC for Windows
Programming >> BBC BASIC language >> REPEAT loops indentation
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1414851328

REPEAT loops indentation
Post by chrispc on Nov 1st, 2014, 2:15pm

This is perhaps an unimportant matter, but it bothers me; because one way I check my programs is to see if my REPEAT loops are completed properly, and that check is made easier when the loops are indented. So can you please explain this apparent inconsistency:

When I typed a procedure definition into a large program it appeared on the screen as follows:

7030 DEF PROC_PrintUpdatedGrid
7040 PRINT"Now we shall look at the updated grid, after PROC_Eliminations."
7050 X=0
7060 REPEAT
7070 X=X+1
7080 Y=0
7090 REPEAT
7100 Y=Y+1
7110 Z=0
7120 REPEAT
7130 Z=Z+1
7140
7150 PRINT"N(";X;",";Y;",";Z;") = ";N(X,Y,Z)
7160
7170 UNTIL Z=9
7180 UNTIL Y=9
7190 UNTIL X=9
7200
7210 PRINT"END OF DEF PROC_PrintUpdatedGrid"
7220 ENDPROC

But when I copied that procedure definition and pasted it into a new so-far-unused program, it looked like this:

10 DIM N(9,9,9)
20 PROC_PrintUpdatedGrid
30 END
40
50 DEF PROC_PrintUpdatedGrid
60 PRINT"Now we shall look at the updated grid, after PROC_Eliminations."
70
80 X=0
90 REPEAT
100 X=X+1
110 Y=0
120 REPEAT
130 Y=Y+1
140 Z=0
150 REPEAT
160 Z=Z+1
170 N(X,Y,Z)=Z
180 PRINT"N(";X;",";Y;",";Z;") = ";N(X,Y,Z)
190
200 UNTIL Z=9
210 G$=GET$
220 PRINT""
230 UNTIL Y=9
240 UNTIL X=9
250
260 PRINT"END OF DEF PROC_PrintUpdatedGrid"
270 ENDPROC
Why in the first version are the two outside loops not indented (as I would have preferred them to be), whereas when I have copied them into a new program all loops are indented?
Note: in the first example, taken from a larger program, the values of each number N(X,Y,Z) have been calculated before the program reached that procedure. Is that relevant?
I have not run the large program since adding that new procedure because it would take too long; that it why I copied the procedure into the (second) test program to check it would be ok. I am assuming that the procedure would run properly at the end of my large, earlier, program.

Thanks,

Chrispc

P.S. After writing the above, and then checking the version your website shows, I see that it left-justified my text, so that the indentations were not apparent as they were in my WORD version. I hope it still makes sense to you.
Re: REPEAT loops indentation
Post by rtr2 on Nov 1st, 2014, 4:05pm

on Nov 1st, 2014, 2:15pm, chrispc wrote:
Why in the first version are the two outside loops not indented

If the indentation isn't correct when you list the routine in the context of the entire program, then evidently there is a fault earlier on in the code. Try running the Cross Reference utility (slot 5 in the menu, usually) to see if that reports anything untoward.

You prefixed your remarks by saying that you use the indentation to check the correctness of your code, and indeed the faulty indentation is demonstrating that something in your code isn't correct!

Richard.

Quote:
P.S. After writing the above, and then checking the version your website shows, I see that it left-justified my text,

That's because you omitted the code tags. With them, whitespace is preserved and it appears as follows:

Code:
 7030 DEF PROC_PrintUpdatedGrid
 7040 PRINT"Now we shall look at the updated grid, after PROC_Eliminations."
 7050 X=0
 7060 REPEAT
 7070   X=X+1
 7080   Y=0
 7090   REPEAT
 7100     Y=Y+1
 7110     Z=0
 7120     REPEAT
 7130       Z=Z+1
 7140       
 7150       PRINT"N(";X;",";Y;",";Z;") = ";N(X,Y,Z)
 7160       
 7170     UNTIL Z=9
 7180   UNTIL Y=9
 7190 UNTIL X=9 


Re: REPEAT loops indentation
Post by rtr2 on Nov 4th, 2014, 10:17pm

on Nov 1st, 2014, 4:05pm, g4bau wrote:
evidently there is a fault earlier on in the code. Try running the Cross Reference utility

Did that enable you to locate the fault? A possible cause of incorrect indentation is an 'unexpected' (surplus to requirements) ENDIF or ENDWHILE statement. The Cross Reference utility will report that, if present.

Richard.
Re: REPEAT loops indentation
Post by chrispc on Nov 5th, 2014, 09:18am

Thanks for your 2nd thought on the indentation. I looked at the Cross Reference and at first thought how great it was, then noticed that it told me that I had an unexpected ENDIF at row 562. Did that mean LINE 562? Because I haven't got one; I go from 560 to 570. What now?
chrispc
Re: REPEAT loops indentation
Post by rtr2 on Nov 5th, 2014, 10:40am

on Nov 5th, 2014, 09:18am, chrispc wrote:
then noticed that it told me that I had an unexpected ENDIF at row 562. Did that mean LINE 562?

No, it meant what it said - row 562. The column and row numbers are displayed in the status bar:

http://www.bbcbasic.co.uk/bbcwin/manual/bbcwin1.html#cursor

'Modern' BASIC programs don't use line numbers, so they are not a useful way for the utility to report the location of an error.

Quote:
What now?

Be more trusting! The report of an 'unexpected ENDIF' in row 562 is almost certainly correct and would explain, at least in part, the misbehaving indentation.

Richard.