QB64.org Forum
Active Forums => QB64 Discussion => Topic started by: sarmad on September 21, 2021, 01:22:58 am
-
hello every one .. i try to make a simple program to seperate numbers from 1 to 100 into odd and even and if the number has a root it ll be a text next to number says that this number has a root .. so i did this
Console:Only
10 For i = 1 To 100
15 _Delay 0.5
20 If i / 2 = Int(i / 2) Then GoTo 30 Else GoTo 100
30 If Sqr(i) = Int(Sqr(i)) Then Print i; "has a root" Else Print i: GoTo 1000
100 If Sqr(i) = Int(Sqr(i)) Then Print , , , i; "has a root" Else Print , , , i
1000 Next
i have a problem .. when even number has a root it ll be put also in the group of odd numbers as well .. i dont know why? maybe the idea of programming is completly wrong?
-
i found the problem .. it was in goto 1000 i put it in seperate line to jump line 100 but i kept the post any way cuz maybe there is a simpler way to get this .. thank you
-
Here's an example with structural changes to eliminate the GOTO code altogether. Not necessarily less code, but more readable with the IF...THEN...ELSE...END IF block separated out for indentation. It helps to be able to see the logic structure at a glance. I'm very much about formatting for human readability, but it's less important in smaller, simpler algorithms.
In larger programs, single line complex logical operators, line numbers and GOTOs will quickly become a nightmare to deal with. Especially if you have to debug it. It's best to get out of the habit as soon as possible. I added the MOD statement, which returns the remainder of a division, just because I find it a little easier and more intuitive than comparing a division with an INT of itself, but i/2=INT(i/2) is perfectly acceptable.
IF i
MOD 2 <> 0 THEN ' check remainder of i/2 PRINT i;
' remainder present/ print odd/ no line feed PRINT , , , i;
' no remainder/ print indented even/ no line feed PRINT ' now line feed and continue to next number
-
Don't have to use Console or Delay:
row
= Int((i
- 1) / 4) + 1: col
= (i
- 1) Mod 4
-
Easier?
-
Advanced?
If i
<= 10 Then locateN i
* i:
Print Space$(9);
"square";
' do this first so dont print over " 1 odd" locateN i
Sub locateN
(n
As Integer) ' convert a number N to row and col to Locate a print on default screen row
= Int((n
- 1) Mod 25) + 1: col
= Int((n
- 1) / 25) * 20 + 1