Teaching Nested loops
Author Message
Teaching Nested loops

I am trying to teach Qbasic, and is finding troubles on explaining nested
"FOR-NEXT" loop. Anybody can help me to come with a clear and easy to
understand explanation?

Fri, 20 Oct 2000 03:00:00 GMT
Teaching Nested loops

Quote:

>I am trying to teach Qbasic, and is finding troubles on explaining nested
>"FOR-NEXT" loop. Anybody can help me to come with a clear and easy to
>understand explanation?

I always think in terms of loops being 'physically' inside each other.

Try this:

CLS
PRINT "Outside", , "Inside"
PRINT
FOR outside% = 1 TO 10
COLOR 3: PRINT outside%,
FOR inside% = 1 TO 10
COLOR 15: PRINT inside%;
NEXT inside%
PRINT
NEXT outside%

(Untested code)

Cheers,

Ali

Fri, 20 Oct 2000 03:00:00 GMT
Teaching Nested loops

Quote:
>I am trying to teach Qbasic, and is finding troubles on explaining nested
>"FOR-NEXT" loop. Anybody can help me to come with a clear and easy to
>understand explanation?

'In nested FOR..NEXT loops, the one to teach first is a single nested
'loop. The inner loop runs through its entire count each time the
'outer loop advances one count. This continues until the outer loop is
'done. If the outer loop has 4 counts and the inner loop has 6, The
'total number of iterations will be 4 * 6 = 24.

'Sometimes 2 nested loops are used (3 loops total) but rarely ever more.
'For 2 nested loops, the outer loop will be called loop 1 and the
'most inner, loop 3. Loop 2 and 3 work just like the nested loop just
'described--however, when loop 2 and 3 complete their last counts,
'loop 1--the outer loop--advances 1 count and loop 2 and 3 repeat. This
'continues until loop 1 is done.

DEFINT M-N, P
CLS : PRINT "In both cases, loop 1 is the outer (or main) loop."
PRINT "One nested loop (2 total FOR..NEXT loops).": PRINT

FOR m = 1 TO 4
PRINT "loop 1 ="; m; "  loop 2 =";
FOR n = 1 TO 6
PRINT n;
NEXT n
PRINT
NEXT m

PRINT : PRINT "Two nested loops (3 total loops). ";
PRINT "The above is repeated 3 times.": PRINT

FOR m = 1 TO 3
FOR n = 1 TO 4
PRINT "loop 1 ="; m; ", loop 2 ="; n; ", loop 3 =";
FOR p = 1 TO 6
PRINT p;
NEXT p
PRINT
NEXT n
NEXT m
END

Fri, 20 Oct 2000 03:00:00 GMT
Teaching Nested loops

CKX>I am trying to teach Qbasic, and is finding troubles on explaining nested
CKX>"FOR-NEXT" loop. Anybody can help me to come with a clear and easy to
CKX>understand explanation?

Have you tried using a flowchart or some type of visual diagram that
illustrates how the flow of control works in nested loops? Other than
that I would say stick to a simple single loop like:

CLS
FOR I% = 1 TO 10
PRINT "Hello world!"
NEXT I%
END

and once you're sure they understand that, move onto simple nested
loops.
Notice how I indented inside the loop. You should make sure
that you do that as it makes the code much clearer and easier to
follow. You should emphasize indenting lines of code that are inside
any loop. Hope that helps. Good luck!

- Robert Fortune

* OLX 2.1 TD * Do farts contain DNA?

Fri, 20 Oct 2000 03:00:00 GMT
Teaching Nested loops

: CKX>I am trying to teach Qbasic, and is finding troubles on explaining nested
: CKX>"FOR-NEXT" loop. Anybody can help me to come with a clear and easy to
: CKX>understand explanation?

How about an analogy to make things clearer? Basically, what you have is
a repetitive process embedded in a repetitive process. And so I
automatically think of an assembly line which produces, say, cars. So
imagine an assembly line which is going to produce 100 cars in one day,
and the production of these cars requires 10 steps (stages). Here's pseudo
car-production code:

For NumberOfCars = 1 to 100
For CarProductionStage = 1 to 10
If CarProductionStage = 1 then Put Engine In Frame
If CarProductionStage = 2 then Stick Wheels On
[...]
If CarProductionStage = 10 then Paint It
Next CarProductionStage
Next NumberOfCars

The difference between the actual production of cars and this pseudo
code, obviously, is that my simulation produces each car from start to
finish before it goes on start the next one. Nevertheless, if they
understand the difference, then this is a particualrly useful model,
since you can embed plenty of other loops inside it as well--i.e. within
CarProductionStage 1, have 5 steps for putting the engine in, etc..

Any similar scenario ought to work well. Another that comes to mind is
systematically searching a square field for a lost ring. Divide the field
into 100 squares, then for each square in teh field, run through a 2 or 3
step search process. If you don't find the ring, go to the next square, etc..

Hope this helps.
Paul

Sat, 21 Oct 2000 03:00:00 GMT
Teaching Nested loops

Quote:

>I am trying to teach Qbasic, and is finding troubles on explaining nested
>"FOR-NEXT" loop. Anybody can help me to come with a clear and easy to
>understand explanation?

It is just jargon for a loop within a loop within a loop within a loop.....
Nothing more.

____    _    ____      ____  _____
|  _ \  / \  / ___) __ | ___)(_   _)
| |_)  / _ \ \____\/  \|  _)   | |
|____//_/ \_\(____/\__/|_|     |_|

www.basicguru.com/schullian

Sat, 21 Oct 2000 03:00:00 GMT
Teaching Nested loops

Why not use a simple example :

FOR hour = 00 to 24                     ' outer loop
FOR minute = 00 to 59   ' nested loop
PRINT "Elapsed Time=",hour,":",minute
SLEEP 60
NEXT minute
NEXT hour

--
Costas

|
| >I am trying to teach Qbasic, and is finding troubles on explaining
nested
| >"FOR-NEXT" loop. Anybody can help me to come with a clear and easy to
| >understand explanation?
|
| It is just jargon for a loop within a loop within a loop within a
loop.....
| Nothing more.
|
|
|   ____    _    ____      ____  _____
|  |  _ \  / \  / ___) __ | ___)(_   _)
|  | |_)  / _ \ \____\/  \|  _)   | |
|  |____//_/ \_\(____/\__/|_|     |_|

|      www.basicguru.com/schullian
|
|

Sun, 22 Oct 2000 03:00:00 GMT

 Page 1 of 1 [ 7 post ]

Relevant Pages