Information Technology - Computer Programming - Source Code - Homebrew - Open Source - Software - Hardware - 8 bit - 16 bit - 32 bit - 64 bit - x86 - x64 - DOS - Windows - Linux - Arduino - Embedded - Development - Retro - Vintage - Math - Science - History - Hobby - Beginners - Professionals - Experiment - Research - Study - Fun - Games

New examples with FOR loops

Share your Net Basic creations here.
Post Reply
admin
Site Admin
Posts: 106
Joined: Wed Feb 22, 2023 6:51 am

New examples with FOR loops

Post by admin »

I have added the keywords FOR/TO/STEP/NEXT/BREAK.
They are detailed in the documentation.

Examples are updated to reflect the update.

A few New/updated examples:

Code: Select all

1 REM https://rosettacode.org/wiki/Attractive_numbers
10 FOR x = 1 TO 120
20 LET n = x
30 LET c = 0
40 IF n MOD 2 <> 0 THEN 70
50 LET n = INT(n / 2)
60 LET c = c + 1
70 IF n MOD 2 = 0 THEN 40
80 FOR i = 3 TO SQR(n) STEP 2
90 IF n MOD i <> 0 THEN 120
100 LET n = INT(n / i)
110 LET c = c + 1
120 IF n MOD i = 0 THEN 90
130 NEXT i
140 IF n <= 2 THEN 160
150 LET c = c + 1
160 IF NOT(PRIME(c)) THEN 180
170 PRINT x,
180 NEXT x

Code: Select all

1 REM https://rosettacode.org/wiki/Nth_root
10 LET a = INT(RND * 5999) + 2
20 PRINT "nth root of "; a; "..."
30 FOR n = 1 TO 10
40 LET p = .00001
50 LET x = a
60 LET y = a / n
70 IF ABS(x - y) <= p THEN 110
80 LET x = y
90 LET y = ((n - 1) * y + a / y ^ (n - 1)) / n
100 IF ABS(x - y) > p THEN 80
110 PRINT n; " : "; y
120 NEXT n

Code: Select all

1 REM https://rosettacode.org/wiki/Catalan_numbers
10 LET @(0) = 1
20 FOR n = 0 TO 15
30 LET p = n + 1
40 LET @(p) = 0
50 FOR i = 0 TO n
60 LET q = n - i
70 LET @(p) = @(p) + @(i) * @(q)
80 NEXT i
90 PRINT n; " "; @(n)
100 NEXT n

Code: Select all

1 REM https://rosettacode.org/wiki/Prime_decomposition
10 LET loops = 100
20 FOR x = 1 TO loops
30 LET n = x
40 PRINT n; " : ";
50 LET c = 0
60 IF n MOD 2 > 0 THEN 110
70 LET n = INT(n / 2)
80 LET @(c) = 2
90 LET c = c + 1
100 IF n MOD 2 = 0 THEN 70
110 FOR i = 3 TO SQR(n) STEP 2
120 IF n MOD i > 0 THEN 170
130 LET n = INT(n / i)
140 LET @(c) = i
150 LET c = c + 1
160 IF n MOD i = 0 THEN 130
170 NEXT i
180 IF n <= 2 THEN 210
190 LET @(c) = n
200 LET c = c + 1
210 FOR y = 0 TO c
220 IF @(y) = 0 THEN 250
230 PRINT @(y); " ";
240 LET @(y) = 0
250 NEXT y
260 PRINT
270 NEXT x

Code: Select all

1 REM Prime Table
10 PRINT "Generating table of primes below..."
20 HTML
30 PRINT "<center><table><tr>"
40 FOR y = 1 TO 50
50 FOR x = 1 TO 20
60 LET i = i + 1
70 PRINT "<td style = 'border:1px solid black; background-color:yellow;'>"
80 PRINT i; ":<br /> "; PRIME(i)
90 PRINT "</td>"
100 NEXT x
110 PRINT "</tr><tr>"
120 NEXT y
130 PRINT "</tr></table></center>"
Post Reply