Selasa, 31 Maret 2020

SMALL Basic: Nested-loop and non-nested loop

Hi, SMALL Basic programmer, welcome.

In this tutorial, we will learned something, that is nested loop and non-nested loop.

You know there are 3 components to build loop:
1. For..
2. While..
3. If.. Goto..

And there are 2 types of loops based on mechanics:
1. Nested-loop
2. Non-nested loop

So, in this tutorial we will discussed nested-loop and non-nested loop.

Nested loop we will be with 2 level loops (a.k.a 1 inner loop and 1 outer loop).

Non-nested loop we will be using 1 level loop.

1. Nested-loop
1.1 Nested-loop with For.. Loop
Making or creating nested loop with For.. Loop is very easy.

You only need to define number of loops for the inner loops, and for the outer loops.

For example:
For i = 0 To 1
  For j = 0 To 10
    TextWindow.WriteLine(i + " ," + j)
  EndFor
EndFor


1.2 Nested-loop with While.. Loop
Making or creating nested loop is at first looks medium difficulty. But not, when you are an expert.

While i < 100
  While j < 100
    TextWindow.WriteLine(i +", " + j)
    j = j + 1
  EndWhile
  i = i + 1
  j = 0
EndWhile

1.3 Nested-loop with If.. Goto.. Loop
Creating nested-loop with If.. Goto.. and creating non-nested loop with If.. Goto.. is different.

Creating nested-loop with If.. Goto.. loop is a little harder., because we need to iterator variable that need to be incremented.

For example below:

i = 0
j = 0
Goto start
up_i:
i = i + 1

up_j:
start:
If j < 10 Then
  TextWindow.WriteLine(i + ", " + j)
  j = j + 1
  If j < 10 Then
    Goto up_j
  Else
    Goto up_i
  EndIf
  Program.Delay(1000)
EndIf
 

2. Non-nested loop
2.1 Non-nested loop with For.. Loop


2.2 Non-nested loop with While.. Loop

2.3 Non-nested loop If.. Goto.. Loop

Tidak ada komentar:

Posting Komentar