Cake
  • Log In
  • Sign Up
    • Learning how to program in the stastics-based language R involves using loops such as for and while, as well as if statements. It’s standard stuff that I assume most programming languages include. Even in graduate school for my maths masters, I took a math-based programming course, sans computer, that focused on creating logical programs using pencil and paper. And discussion of loops was covered in the course. So when today I started a new lesson creating functions, I noticed the lines to the left of each line of code and thought of a rudimentary course in BASIC programming that I took in the time Pre-Internet.

      If you’re old enough to remember BASIC, this simple program should be familiar:

      10 A=5

      20 IF (A<10) GOTO 50

      30 A=A+1

      40 GOTO 20

      50 END

      In BASIC, statements and functions direct the computer to a specific line of code to execute. It was an incredibly logical approach to learn a bit of programming: BASIC is in fact an acronym for "Beginner's All-purpose Symbolic Instruction Code."

      Noticing those “line numbers” to the left of my code has me wondering if any modern programming languages still use calls to specific line numbers. Because it was used in some languages on mainframes, such as COBOL:

      If you look at the second blue PERFORM above, the statement is to goto line 200 if the variable “present value” is not equal to zero.

      A few years after I had learned BASIC, I found out that there were now programming languages that didn’t use line numbers. It was a mind-blowing idea for me: how do you know where to goto next? I expect my mind to be further blown when I create programs in R that do just that.

    • I haven't coded in a meaningful way for years but at least for me it's a lot easier to remember variable names than locations. And the first language I cut my teeth on was MUMPS, here's an example I found online which is pretty easy to follow:

      s ^cars=20
      s ^cars("Tesla",1,"Name")="Model 3"
      s ^cars("Tesla",2,"Name")="Model X"
      s ^cars("Tesla",2,"Doors")=5
      
      w !,^cars 
      ; 20
      w !,^cars("Tesla")
      ; null value - there's no value assigned to this node but it has children
      w !,^cars("Tesla",1,"Name")
      ; Model 3
      fib ; compute the first few Fibonacci terms
          new i,a,b,sum
          set (a,b)=1 ; Initial conditions
          for i=1:1 do  quit:sum>1000
          . set sum=a+b
          . write !,sum
          . set a=b,b=sum


      And a database one which is its bread an butter:

      ;Let's call back to our ^TEMPS global from earlier:
      ; A log of temperatures by date and time
      s ^TEMPS("11/12","0600",32)=""
      s ^TEMPS("11/12","0600",48)=""
      s ^TEMPS("11/12","1400",49)=""
      s ^TEMPS("11/12","1700",43)=""
      ; Some more
      s ^TEMPS("11/16","0300",27)=""
      s ^TEMPS("11/16","1130",32)=""
      s ^TEMPS("11/16","1300",47)=""

      ;Here's a loop to print out all the dates we have temperatures for:
      n date,time ; Initialize these variables with ""

      ; This line reads: forever; set date as the next date in ^TEMPS.
      ; If date was set to "", it means we're at the end, so quit.
      ; Do the block below
      f s date=$ORDER(^TEMPS(date)) q:date="" d
      . w !,date

      ; Add in times too:
      f s date=$ORDER(^TEMPS(date)) q:date="" d
      . w !,"Date: ",date
      . f s time=$O(^TEMPS(date,time)) q:time="" d
      . . w !,"Time: ",time

      ; Build an index that sorts first by temperature -
      ; what dates and times had a given temperature?
      n date,time,temp
      f s date=$ORDER(^TEMPS(date)) q:date="" d
      . f s time=$O(^TEMPS(date,time)) q:time="" d
      . . f s temp=$O(^TEMPS(date,time,temp)) q:temp="" d
      . . . s ^TEMPINDEX(temp,date,time)=""

      ;This will produce a global like
      ^TEMPINDEX(27,"11/16","0300")
      ^TEMPINDEX(32,"11/12","0600")
      ^TEMPINDEX(32,"11/16","1130")

    • Most modern integrated development environments (IDE) contain a text editor that shows line numbers, but these are ust there to help programmers find a specific piece of code. They aren't meaningful to the program itself, or to the compiler or parser that runs it.

      For example, when trying to run the code it might fail with an error that points to a specific line number, making it easier to find the bug that caused the crash.

      Jumps to specific line numbers are no longer used in modern programming languages, among other reasons because they lead to code that is hard to maintain and understand, and errors that are difficult to track.

      What's typically used instead are functions (sometimes also called methods or procedures) that return a value and can simply be used everywhere the return value needs to be used.

      As just one toy example, consider a function that takes two numbers x and y, compares them and returns the larger number. This function could be declared like this:

      fun max(x:Integer,y:Integer):Integer {
      if (x > y) { return x } else {return y }
      }

      And then be used in other code, like so:

      var z = max(1,3) + max(2,4) //value of variable z will be 7 (3+4)

      The resulting code will be easier to read than something that tries to achieve the same using GOTOs to random line numbers, and probably less likely to contain weird bugs.

    • Sorry to do this, but my professional deformation precludes me from not mentioning it. You have a bug in the program. I think you meant for line 20 to go:

      20 IF (A>10) GOTO 50

      Phew. I feel better now.

      But, as others mentioned, ALL the programming languages use line numbers, just not for labels as BASIC, but for debugging and navigation.