What is this technique called?

General FreeBASIC programming questions.
ShakeyPakey
Posts: 2
Joined: Jun 29, 2020 10:31

What is this technique called?

Postby ShakeyPakey » Jun 29, 2020 10:49

Hello, everyone, I have a small question.

I'm new to BASIC overall and decided to pick it up. While looking through a program coded in it so I can learn, I saw that they used a technique of skipping to parts of code they want to run by typing "GOTO {line number}", or just saying "IF {expression} then GOTO {line number}" at least I believe it's a line number. The author put a number at the start of every line that goes up by 10 each line, and sometimes it goes up by hundreds. This is a snippet:

Code: Select all

10 PRINT "Hello. How old are you?"
20 INPUT A
30 IF A < 13 THEN 100
40 PRINT "You're older than 13."
50 END
100 "You're younger than 13.
110 END


Can someone tell me what this method is called (if anything) and how it can be useful?

Thank you :)
Imortis
Moderator
Posts: 1726
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: What is this technique called?

Postby Imortis » Jun 29, 2020 13:11

That code is quite old. FreeBASIC still supports line numbers but there are much better and easier ways to do the thing you are describing.

Code: Select all

PRINT "Hello. How old are you?"
INPUT A
IF A < 13 THEN
   print "You're younger than 13."
ELSE
   PRINT "You're older than 13."
END IF
Lost Zergling
Posts: 326
Joined: Dec 02, 2011 22:51
Location: France

Re: What is this technique called?

Postby Lost Zergling » Jun 29, 2020 13:17

http://ingeb.org/songs/vemkanse.html
https://www.youtube.com/watch?v=Hl76XbZp3js
This technique is called "the sailing navy" (the beginnings)
ShakeyPakey
Posts: 2
Joined: Jun 29, 2020 10:31

Re: What is this technique called?

Postby ShakeyPakey » Jun 29, 2020 14:05

Imortis wrote:That code is quite old. FreeBASIC still supports line numbers but there are much better and easier ways to do the thing you are describing.

Code: Select all

PRINT "Hello. How old are you?"
INPUT A
IF A < 13 THEN
   print "You're younger than 13."
ELSE
   PRINT "You're older than 13."
END IF


Yes, I am aware that it is quite old & inefficient (this was found in a program written in 1978), and I know of the more efficient way you've stated. I'm just curious that if they're considered labels or line numbers?
Imortis
Moderator
Posts: 1726
Joined: Jun 02, 2005 15:10
Location: USA
Contact:

Re: What is this technique called?

Postby Imortis » Jun 29, 2020 14:42

Technically labels and line numbers are the same thing in FB. So yes, you could say they are labels.
dodicat
Posts: 6648
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: What is this technique called?

Postby dodicat » Jun 29, 2020 15:23

Coding this way is generally called branching.
https://en.wikipedia.org/wiki/Branch_(computer_science)
caseih
Posts: 1518
Joined: Feb 26, 2007 5:32

Re: What is this technique called?

Postby caseih » Jun 29, 2020 15:56

And all languages on all computer architectures ultimately translate to this form form of logic. This is known as being "turing complete." In fact you can write any program you want with only one instruction that involves an arithmetic operation and a branch (goto) based on a test of the result of the arithmetic operation.

A common idiom when programming in assembly language (a step above pure machine language) is to take your IF/THEN block and then invert the condition. So you get something like:

Code: Select all

if a = 5 then
    'do something
end if


which translates to something like

Code: Select all

if a != 5 then goto label1
'do something'
label1:


GOTO may be considered bad form today with modern, structured constructs, but everything ultimately compiles down to a bunch of gotos! Technically conditional branches.

Return to “General”

Who is online

Users browsing this forum: MrSwiss and 5 guests