Help using On...Goto and Select Case and SUB needed [SOLVED]

New to FreeBASIC? Post your questions here.
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Help using On...Goto and Select Case and SUB needed [SOLVED]

Postby Trinity » Sep 20, 2017 20:57

Hi ,
I am very interested in direction of program execution , so I would be grateful if anyone with knowledge enough could please help me with my code problem and also if possible expand on what is written in the manual (?)

This code executes correctly for On...Goto without Sub

Code: Select all

Dim choice As String
Dim text As String
DIM p as Integer
DIM x as Integer

Print "Select a Hello"
Print "1. Moon"
Print "2. Stars"

Labelinput:

Do
   choice = Input(1)
Loop Until choice >= "1" And choice <= "2"

p = ValInt(choice)

On p goto LabelHello1,LabelHello2

LabelHello1:
text = "A Hello World from the Moon!"
Print text

Goto Labelinput

LabelHello2:
text = "Another Hello to the World from the Stars!"
Print text

Goto Labelinput

SLEEP


This code executes correctly for using Select Case without Sub

Code: Select all

Dim choice As Integer
Dim text As String

Labelinput:

Print
Input "Select 1 for Moon , Select 2 for Stars: "; choice

Select Case As Const choice
Case 1
    Goto LabelHello1
Case 2
    Goto LabelHello2
Case Else
    Print "number is outside the 1-2 range"
End Select

Goto Labelinput

LabelHello1:
text = "A Hello World from the Moon!"
Print text

Goto Labelinput

LabelHello2:
text = "Another Hello to the World from the Stars!"
Print text

Goto Labelinput

SLEEP


But my code will not run when I try to use SUB. Please notice that : My original intention has been to also find out if I could direct freely to both ordinary "Label" and "SUB" directly from both On...Goto and Select Case but since I had a problem getting the program to jump directly to SUB from On...Goto and Select Case then I have tried using an intermediate redirection using a label to see if that helped. But I am also looking to find out if I can direct to SUB directly using On...Goto and Select Case !
So I am looking for code correction help for using both On...Goto and Select Case

Here is the problem code using On...Goto :
Compiler errors :
error 41: Variable not declared, Hello1 in 'Hello1()'
error 41: Variable not declared, Hello2 in 'Hello2()'

Code: Select all

Dim choice As String
DIM p as Integer
DIM x as Integer

Print "Select a Hello"
Print "1. Moon"
Print "2. Stars"

Labelinput:

Do
   choice = Input(1)
Loop Until choice >= "1" And choice <= "2"

p = ValInt(choice)

On p goto LabelHello1,LabelHello2

LabelHello1:
Hello1()

LabelHello2:
Hello2()

Sub Hello1()
Dim text As String
text = "A Hello World from the Moon!"
Print text
END SUB

Sub Hello2()
Dim text As String
text = "Another Hello to the World from the Stars!"
Print text
END SUB


SLEEP


Here is the problem code using Select Case :
error 41: Variable not declared, Hello1 in 'Hello1()'
error 41: Variable not declared, Hello2 in 'Hello2()'

Code: Select all

Dim choice As Integer
Dim text As String

Labelinput:

Print
Input "Select 1 for Moon , Select 2 for Stars: "; choice

Select Case As Const choice
Case 1
    Goto LabelHello1
Case 2
    Goto LabelHello2
Case Else
    Print "number is outside the 1-10 range"
End Select
Goto Labelinput

LabelHello1:
Hello1()
Goto Labelinput

LabelHello2:
Hello2()
Goto Labelinput

Sub Hello1()
Dim text As String
text = "A Hello World from the Moon!"
Print text
END SUB

Sub Hello2()
Dim text As String
text = "Another Hello to the World from the Stars!"
Print text
END SUB


SLEEP


Besides the code help then I am looking to find out specifics about On...Goto and Select Case and SUB :

First of all then I have learned that both On...Goto and Select Case can be used to direct using standard labels , but can one direct directly to SUB also ? If so then how to use the sub call (ref. my program errors) ?

Secondly then it says in description in the manual about On....Goto that : "It is recommended that the structured Select Case conditional statement be used instead of On...Goto."
Why is that recommended ? are the On....Goto going to be phased out ? / will it be unsupported in the future ?

I am sorry if asking too many questions . Anyway help with getting the code to run is the top priority if anyone will be so kind as to assist me that is :-)
Last edited by Trinity on Sep 22, 2017 8:53, edited 1 time in total.
Josep Roca
Posts: 423
Joined: Sep 27, 2016 18:20
Location: Valencia, Spain

Re: Help using On...Goto and Select Case and SUB needed

Postby Josep Roca » Sep 20, 2017 21:11

when the compiler finds the calls to Hello1 and Hello2, it still doesn't know anything about them because the code for them is placed after the calls.

Either move them before the calls:

Code: Select all

Dim choice As Integer
Dim text As String

Sub Hello1()
Dim text As String
text = "A Hello World from the Moon!"
Print text
END SUB

Sub Hello2()
Dim text As String
text = "Another Hello to the World from the Stars!"
Print text
END SUB

Labelinput:

Print
Input "Select 1 for Moon , Select 2 for Stars: "; choice

Select Case As Const choice
Case 1
    Goto LabelHello1
Case 2
    Goto LabelHello2
Case Else
    Print "number is outside the 1-10 range"
End Select
Goto Labelinput

LabelHello1:
Hello1()
Goto Labelinput

LabelHello2:
Hello2()
Goto Labelinput

SLEEP


or use forward declarations:

Code: Select all

Dim choice As Integer
Dim text As String

DECLARE SUB Hello1()
DECLARE SUB Hello2()

Labelinput:

Print
Input "Select 1 for Moon , Select 2 for Stars: "; choice

Select Case As Const choice
Case 1
    Goto LabelHello1
Case 2
    Goto LabelHello2
Case Else
    Print "number is outside the 1-10 range"
End Select
Goto Labelinput

LabelHello1:
Hello1()
Goto Labelinput

LabelHello2:
Hello2()
Goto Labelinput

Sub Hello1()
Dim text As String
text = "A Hello World from the Moon!"
Print text
END SUB

Sub Hello2()
Dim text As String
text = "Another Hello to the World from the Stars!"
Print text
END SUB

SLEEP
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Help using On...Goto and Select Case and SUB needed

Postby Trinity » Sep 20, 2017 21:26

Josep Roca wrote:when the compiler finds the calls to Hello1 and Hello2, it still doesn't know anything about them because the code for them is placed after the calls.

Either move them before the calls:

Code: Select all

Dim choice As Integer
Dim text As String

Sub Hello1()
Dim text As String
text = "A Hello World from the Moon!"
Print text
END SUB

Sub Hello2()
Dim text As String
text = "Another Hello to the World from the Stars!"
Print text
END SUB

Labelinput:

Print
Input "Select 1 for Moon , Select 2 for Stars: "; choice

Select Case As Const choice
Case 1
    Goto LabelHello1
Case 2
    Goto LabelHello2
Case Else
    Print "number is outside the 1-10 range"
End Select
Goto Labelinput

LabelHello1:
Hello1()
Goto Labelinput

LabelHello2:
Hello2()
Goto Labelinput

SLEEP


or use forward declarations:

Code: Select all

Dim choice As Integer
Dim text As String

DECLARE SUB Hello1()
DECLARE SUB Hello2()

Labelinput:

Print
Input "Select 1 for Moon , Select 2 for Stars: "; choice

Select Case As Const choice
Case 1
    Goto LabelHello1
Case 2
    Goto LabelHello2
Case Else
    Print "number is outside the 1-10 range"
End Select
Goto Labelinput

LabelHello1:
Hello1()
Goto Labelinput

LabelHello2:
Hello2()
Goto Labelinput

Sub Hello1()
Dim text As String
text = "A Hello World from the Moon!"
Print text
END SUB

Sub Hello2()
Dim text As String
text = "Another Hello to the World from the Stars!"
Print text
END SUB

SLEEP


Thank you very much Joseph , I have spent hours trying to solve that problem because there is no direct mention of the necessity of the use of DECLARE in the manual for SUB other than a "see also" declare at bottom of page , ref. : https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgSub

And it was indeed the DECLARE SUB I was looking for together with the info that you gave in your explanation and your explanation about moving code before the calls. I would generally prefer to use a Declare as that would give me less pain later if trying to move code around without having to consider constraints to avoid errors :-)

Thank you very much :-)
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:

Re: Help using On...Goto and Select Case and SUB needed

Postby vdecampo » Sep 21, 2017 12:34

Just remember that the FreeBASIC compiler recognizes symbols as they occur in your source code. If you reference a SUB or FUNCTION or variable before that symbol has been declared, the compiler will treat it as unknown.

-Vince
MrSwiss
Posts: 3129
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Help using On...Goto and Select Case and SUB needed

Postby MrSwiss » Sep 21, 2017 13:47

As discussed on the other tread, it is recommended to use a loop, instead of GoTo (and all the labels needed).
It is what gave all BASIC's a bad reputation, because it tends to lead to *spaghetti code*.

A way to make the code better readable is: to use indentation as below:

Code: Select all

Dim choice As Long ' a 32bit integer (integer's size depends on FBC's bitness)
'Dim text As String not needed here

Sub Hello1()
    ' shortest method without variable
    'Print "A Hello World from the Moon!" ' ist called a *litteral* String
    ' below with var.
    Dim As String text ' second method
    text = "A Hello World from the Moon!"
    Print text
END SUB

Sub Hello2()
    Dim As String text = "Another Hello to the World from the Stars!"
    Print text
END SUB

Do
    Cls ' clear screen (on every new run)
    Print
    Input "Select 1 for Moon , Select 2 for Stars: "; choice
    Select Case As Const choice
        Case 1
            Hello1
        Case 2 : Hello2 ' second method [:] separate second statement on same line
        Case Else
            Print "number is outside the 1-2 range"
            Print "press [Enter] to quit ";
    End Select
    Sleep 2000 ' give user time to see result, then carry on ...
Loop Until choice = chr(13) ' end of prog. ...
fxm
Posts: 9021
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Help using On...Goto and Select Case and SUB needed

Postby fxm » Sep 21, 2017 14:26

Compile error:
(31) error 20: Type mismatch, found ''' in 'Loop Until choice = chr(13) ' end of prog. ...'

Correct your code please.
Perhaps the simplest:

Code: Select all

Loop Until choice = 0 ' end of prog. ...
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Help using On...Goto and Select Case and SUB needed

Postby Trinity » Sep 21, 2017 15:26

vdecampo wrote:Just remember that the FreeBASIC compiler recognizes symbols as they occur in your source code. If you reference a SUB or FUNCTION or variable before that symbol has been declared, the compiler will treat it as unknown.

-Vince


Thank you very much Vince :-)

MrSwiss wrote:As discussed on the other tread, it is recommended to use a loop, instead of GoTo (and all the labels needed).
It is what gave all BASIC's a bad reputation, because it tends to lead to *spaghetti code*.

A way to make the code better readable is: to use indentation as below:

Code: Select all

Dim choice As Long ' a 32bit integer (integer's size depends on FBC's bitness)
'Dim text As String not needed here

Sub Hello1()
    ' shortest method without variable
    'Print "A Hello World from the Moon!" ' ist called a *litteral* String
    ' below with var.
    Dim As String text ' second method
    text = "A Hello World from the Moon!"
    Print text
END SUB

Sub Hello2()
    Dim As String text = "Another Hello to the World from the Stars!"
    Print text
END SUB

Do
    Cls ' clear screen (on every new run)
    Print
    Input "Select 1 for Moon , Select 2 for Stars: "; choice
    Select Case As Const choice
        Case 1
            Hello1
        Case 2 : Hello2 ' second method [:] separate second statement on same line
        Case Else
            Print "number is outside the 1-2 range"
            Print "press [Enter] to quit ";
    End Select
    Sleep 2000 ' give user time to see result, then carry on ...
Loop Until choice = chr(13) ' end of prog. ...


Oh! , I am probably so gonna get into trouble for asking this , but you wrote "As discussed on the other tread" , what other thread ? (I can't believe that I already has forgotten something that you have told me - insert icon of sad person here ;-) )

As for the code then yes , very nice , very clean , very short code (And thank you for the annotations also !) , but I think that I am from that awful school of (TI-99/4A Extended) BASIC programmers that have another focus (sigh - it's so hard to be the bad boy ;-) ) , so while I love code cleanliness then I am much more focused on re-use-able code that I do not have to dig out of loops and stuff to use. I never knew the term spaghetti code before I came here..
Hmmmmm.... Pondering upon it all I think that my attitude is probably because I have programmed on a system that has 24488 bytes of program space (used for BASIC programs and numeric variables) plus a stack of 11840 bytes used for string variables.. So while I have been an expert in dealing with memory constraints then on the other hand if you got a program with tens or hundreds of thousands of lines of code then you will maybe have another philosophy than mine (?) ;-) :-D

But thank you very much , interesting code anyway :-)

fxm wrote:Compile error:
(31) error 20: Type mismatch, found ''' in 'Loop Until choice = chr(13) ' end of prog. ...'

Correct your code please.
Perhaps the simplest:

Code: Select all

Loop Until choice = 0 ' end of prog. ...


You are right , I got a compile error also .
Thank you for your code correction :-)
MrSwiss
Posts: 3129
Joined: Jun 02, 2013 9:27
Location: Switzerland

Re: Help using On...Goto and Select Case and SUB needed

Postby MrSwiss » Sep 22, 2017 0:51

Trinity wrote:Oh! , I am probably so gonna get into trouble for asking this , but you wrote "As discussed on the other tread" , what other thread ? (I can't believe that I already has forgotten something that you have told me - insert icon of sad person here ;-) )

The tread about String KeyWord ... (my example code there)

That is the reason to recommend, using no more than one tread at the time, to ask questions.
Once that is resolved (best marked [SOLVED], in the title of the very first post), by yourself, it
is OK, to open a new tread, asking something different.

As soon as you'll get used to write larger programs, you'll probably understand the usefulness
of (if at all possible), code readability (also adding comments, as needed to later understand
what you've done, lets say: about 6 month ago).

*Spaghetti code* explained:
https://en.wikipedia.org/wiki/Spaghetti_code
Trinity
Posts: 214
Joined: Sep 16, 2017 17:07

Re: Help using On...Goto and Select Case and SUB needed

Postby Trinity » Sep 22, 2017 9:29

MrSwiss wrote:That is the reason to recommend, using no more than one tread at the time

Done ! - [SOLVED] ;-)
MrSwiss wrote:As soon as you'll get used to write larger programs, you'll probably understand the usefulness
of (if at all possible), code readability (also adding comments, as needed to later understand
what you've done, lets say: about 6 month ago).

Though it may not show to you then the concept of the need for annotated code is already clear to me.
By now I got programs that are so old that I am not even sure how to use them any more because of more factors such as only 24488 bytes for program and numeric variables (and only 11840 bytes for string variables) and 360 K floppies and no hard disc , so program space were for writing code and on screen info when using where for the initiated only , and info about using the programs were maybe scratched on paper in short-form where one later wonders what it is about when reading it ;-) :-D So starting programs from back then X years later is quite confusing - not to speak of wanting to find out how I made and how it works if studying the code (big grin + ROTFL)
But I have been thinking a lot about the need for code "readability" and maintain-ability versus my own semi-dormant "Programming paradigm" (?) principles and I think that the keyword for me here is using "blocks" when programming (I do not necessarily mean that as per common definition ?)
(But actually this for me is also a huge discussion about "Programming paradigms" (?) I think is the expression and even a discussion of the definitions and perception of those and not a discussion I am willing to take - at least not now)
Anyway , got to do just a little bit else than writing stuff here so enough for now....
MrSwiss wrote:*Spaghetti code* explained:
https://en.wikipedia.org/wiki/Spaghetti_code

I am a great fan of Wikipedia - use it all the time - there might be intellectual (?) dangers to using Wikis but they are indeed very useful :-)

Thank you for the advice :-)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 2 guests