What are the difference between a BASIC GOTO and GOSUB statement - basic

What are the difference between a GOTO and a GOSUB statements in BASIC programming language?

GOTO simply jumps to another line, GOSUB keeps track of where it came from (on a stack, presumably), so when the interpreter encounters a RETURN, it goes back to the last place GOSUB was called.

The other answers provided give a good explanation on how to use GOTO and GOSUB, but there is an important difference in how they are processed. When a GOTO is executed it starts at the top of the instruction set and flips through all the lines of code until it finds the line it is supposed to GOTO. Then if you use another GOTO statement to get back, it again goes to the top of the instruction set and flips through everything until it gets to the next location.
GOSUB does almost the same thing as GOTO, but it remembers where it was. When you use the RETURN statement it just jumps back without first going to the top of the instruction set and flipping through everything again, so it's much faster. If you want your code to run fast you should put your most called subroutines at the top of the stack and use GOSUB/RETURN instead of GOTO.

When you call GOTO the program will jump to the line in question and carry on executing.
If you use GOSUB, it will do the same, however at some point you can code a RETURN statement and the code will return to the line just after the GOSUB.
So GOTO is go to X while GOSUB is go to X but remember where you are now and so you can return later.

Related

gdb how to step until end of statement ("n" does not do it!)

in GDB with C++ on LINUX:
How to step until the end of the current statement?
The n (next statement) command performs many steps and one never knows, if one already has reached the end of the current statement (or the beginning of the next).
I'm not talking about curly braces.
I'm talking about executing a single assignment.
How to step until the end of the current statement?
There isn't an easy way to achieve this, so don't put multiple statements on a single line.
If there is a single statement on each line, then next command will do what you want.

Execute With...End With Statement in Immediate window, Excel VBA

I'm trying to execute With...End With Statement in the Immediate Window,
but I'm not getting it to work.
So my question is how do you execute multiple line of Code concerning With...End With Statement in the Immediate window?
My code I'm trying to execute in the Immediate window:
With Date_Per_Month.Range("A2:H32")
.Offset(1).Resize(.rows.Count - 1, .Columns.Count - 1).select
end with
Ofcourse it works to write a single line of code like this in the immediate window (but that doesn't answer the question).:
Date_Per_Month.Range("A2:H32").Offset(1).Resize _(Date_Per_Month.Range("A2:H32").Rows.Count-1,Date_Per_Month.Range("A2:H32").Columns.Count-1).Select
Tried to concatenate each code line with ":" at the end of lines but it didn't work.
Any help is highly appreciated.
The immediate toolwindow is essentially a console. You can use it to evaluate expressions without an execution context, or in break mode in the context of the current procedure.
Each "line" can be [almost] any executable instruction, but
You can assign to a variable that doesn't exist...
foo = 42
...and then this identifier exists in the "immediate" context and you can use it in subsequent statements:
?TypeName(foo)
Integer
...until you explicitly reset that context:
End
?TypeName(foo)
Empty
But you can't declare a variable:
Dim foo
A With statement is special: it witholds an object reference, but syntactically it's a block statement that needs to be terminated with an End With token: if you try to type With Sheet1 in the immediate pane, you'll get a compile error saying End With is missing - again because each statement in the immediate pane is a standalone instruction.
We could try to inline it:
With Sheet1 : .Cells(1, 1).Value = 42 : End With
But then we get a weird "invalid watch expression" error:
Regardless of the reason, like declaring variables with a Dim statement, defining a With variable makes no sense in the context of the immediate pane, where the next instruction to run is whatever instruction you happen to hit ENTER on: there's no scope, no sequence of operations - an instruction runs immediately, and that's all: whatever the contents of the immediate pane is, no other instruction will run until you hit ENTER on it.
how do you execute multiple lines of code [...] in the Immediate window?
The answer is, you don't - because the immediate toolwindow has no concept of "lines of code".

vim quick get to last set of single quotes

So I am in the process of learning vim. So if I want to get to the last set of the single quotes, how would I do that efficently?
Plug 'OrangeT/vim-csharp', { 'for': 'csharp' }
Here are the ways that I thought of doing it, but I am sure there is a quicker way to do it.
Hitting w a few times
Doing a f ' and repeating it a few times
Another way: $B
$ go to the end of the line
B jump to the beginning of the previous "word"
G to get to the end of the file (not sure if you want that or not)
$ to get to the end of line
?'<enter> to reverse search for a '
n to find the next occurrence (which would be the 2nd to last one)
Update: #minitech suggests a good way in the comment above

Jumping [f]orward to a letter into the next line

Say that in the following paragraph my cursor lies on the first are in the first sentence (wish I could highlight it, but I can't so ...). Upon pressing ff twice that will get me to fools in the first sentence, and then to of. Further pressing it will get me nowhere.
Some people are confident because they are fools. Leonard had the look of
someone who was confident because, so far, he'd never found reason not to be. He
would step off a high building in the happy state of mind of someone who
intended to deal with the problem of the ground when it presented itself.
What would I need to modify so I can "move" across lines, so that further pressing ff will make Vim jump into the next lines?
Nothing to modify except Vim's source code. fFtT are linewise and there's nothing you can do about it. See :h left-right-motions
You could use /f, which is not limited to the current line, instead but it's not as fast as it requires another keypress.
Or a plugin like easymotion.
Did you know that you can hit ; to redo the previous fFtT?
There are a couple of solutions, but none do exactly what you want. Usually in this situation I would do ff followed by a series of ; characters to repeat f. When I reach the end of the line I hit + and then continue with ;. Similarly, you can use , to go backwards and 0<BS> to go back a line.
Another option is to write lines with set wrap and no hard breaks, but for various reasons users may not find that ideal.
Despite this seeming like nonsense it can actually be useful in some situations. For example, the :norm command will skip a line if ff doesn't find anything. You can use this for complex :norm commands over multiple lines to check for a particular character before you proceed with the command.
Edit:
There's a plugin that accomplishes this: https://github.com/dahu/vim-fanfingtastic

Why BASIC had numbered lines? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why did we bother with line numbers at all?
I'm curious about why early versions of the BASIC programming language had line numbering like in:
42 PRINT "Hello world!"
The text editors back then had no line numbering?
EDIT: Yes, I know they are used for GOTOs, but why? I mean having labels was too computationally expensive?
Many microcomputers had a BASIC interpreter in ROM that would start upon bootup. The problem was that there was no text editor or file system to speak of. You had an interactive prompt to do everything through. If you wanted to insert a line of code, you just typed it, starting with the line number. It would insert it into the correct spot in you code. e.g:
>10 print "hello"
>30 goto 10
>20 print "world"
>list
10 PRINT "hello"
20 PRINT "world"
30 GOTO 10
>
(In that example > is the BASIC prompt)
If you wanted to erase a line, you would type something like ERASE 20.
Some really fancy systems gave you a line editor (i.e. EDIT 10)
And if you didn't plan your line numbers and ran out (how do I insert a line between 10 and 11?) some systems gave you a RENUM command which would renumber your code (and adjust GOTOs and GOSUBs appropriately).
Fun Times!
The original BASIC line numbering was actually an integral part of the language, and used for control flow.
The GOTO and GOSUB commands would take the line, and use it for control flow. This was common then (even though it's discouraged now).
They were used as labels for GOTO and GOSUB
Like this:
10 PRINT "HELLO WORLD"
20 GOTO 10
There were no named labels in some early BASIC versions
They were also required if you wanted to insert a line between 2 existing lines of code, because in the early days, you had no full text editors. Everything had to be typed in the "interactive" interpreter.
So if you typed:
15 PRINT "AND THE UNIVERSE"
The program would become:
10 PRINT "HELLO WORLD"
15 PRINT "AND THE UNIVERSE"
20 GOTO 10
When you ran out of line numbers, you could run a "renumbering" tool to renumber all lines in your program, but in the very early days of the Commodore 64 and other home computers, we didn't even have that, so you'd have to renumber manually. That's why you had to leave gaps of 10 or more in the line numbers, so you could easily add lines in between.
If you want to try out the Commodore 64 interpreter, check out this C64 emulator written in Flash: http://codeazur.com.br/stuff/fc64_final/ (no install required)
In BASIC, the line numbers indicated sequence.
Also, many older editors weren't for files, but simply lines ("line editors", e.g. ed, the standard editor). By numbering them this way, you knew which line you were working on.
A simple google reveals what wikipedia has to say about it:
Line numbers were a required element of syntax in some older programming languages such as GW-BASIC.[2] The primary reason for this is that most operating systems at the time lacked interactive text editors; since the programmer's interface was usually limited to a line editor, line numbers provided a mechanism by which specific lines in the source code could be referenced for editing, and by which the programmer could insert a new line at a specific point. Line numbers also provided a convenient means of distinguishing between code to be entered into the program and commands to be executed immediately when entered by the user (which do not have line numbers).
Back in the day all languages had sequence numbers, everything was on punched cards.
There was one line per card.
Decks of cards made up your program.
When you dropped the cards, you'd put them in a card sorter that used those sequence numbers.
And of course, they were referenced by control flow constructs.
On the C64, there wasn't even a real editor (built-in at least). To edit a part of the program, you'd do something like LIST 100-200, and then you'd only be able to edit those lines that were currently displayed on the screen (no scrolling upwards!)
They were labels for statements, so that you could GOTO the line number. The number of the statements did not necessarily have to match the physical line numbers in the file.
The line numbers were used in control flow. There were no named subroutines. You had to use GOSUB 60, for instance, to call the subroutine starting at line 60.
On your update, not all languages had labels, but all languages had line numbers at one time. At one time, everything was punch cards. BASIC was one of the very first interactive languages, where you could actually type something and have a response immediately. Line numbers were still the current technology.
Labels are an extra expense. You have to keep track of the correlation between the symbolic label and the code or data to which it refers. But if every line has a line number (and if all transfer of control flow statements always refer to the beginning of a line), then you don't need a separate symbol table.
Also keep in mind that original BASIC interpreters didn't need a symbol table for variables: There were 26 variables named A-Z. Some were sophisticated and had An-Zn. Some got very fancy and added a distinction between string, integer and floating point by adding "$" or "%" after the variable. But no symbol table was required.
IIRC, line numbers were mostly used as labels for GOTO and GOSUB statements, since in some (most?) flavors of BASIC there was no way to label a section of code.
They were also used by the editor - ie you said:
edit 100
to edit line 100.
As others have pointed out, these line numbers were used as part of subroutines.
Of course, there's a reason that this isn't done anymore. Imagine if you say GOTO 20 on line 10, and then later realize you need to write 10 more lines of code after line 10. All of a sudden, you're smashing up against 20 so you either need to shift your subroutine farther away (higher numbers) and change your GOTO value, or you need to write another subroutine that jumps farther in the code.
In other words, it became a nightmare of true spaghetti code and is not fun to maintain.
It was entered in on the command-line in many instances (or was, on my old Commodore 64) so there might not always have been a text editor, or if there was, it was quite basic.
In addition, you would need to do GOTOs and the like, as well as inserting lines in between others.
ie:
10 PRINT "HELLO"
20 GOTO 10
15 PRINT " WORLD"
where it would go in the logical 10 15 20
Some editors only had an "overwrite" mode and no "insert" mode. This made editing of existing code extremely painful. By adding that line-number feature, you could however patch existing code from anywhere within the file:
100 PRINT "Broken Code"
200 PRINT "Foobar"
...
101 patch the broken code
102 patch more broken code
Because line numbers didn't have to be ordered within the file.
Line numbers were a PART of the language, in some VERY early ones, even the OS was just these simple lines. ALL you had was one line at a time to manipulate. Try writing an accounting system using 1-4k program files and segmenting it by size to get stuff done. To edit you used the line numbers to tell what you were editing. So, if you entered like:
10 PRINT "howdy"
20 GOTO 10
10 PRINT "WOOPS"
15 PRINT "MORE WOOPS"
20
RUN
YOU WOULD GET:
WOOPS
MORE WHOOPS
The blank 20 would effectivly delete that line.

Resources