Hot answers tagged

22

Your calculator is pretty smart at inferring the end of lines for you, and thus you can omit quite a few characters. :Disp "HELLO WORLD //is the same as... :Disp "HELLO WORLD" For( loops have a syntax like this - For(variable, start, end, increment), but you can omit the increment and it will use 1: :For(A,1,5 //is the same as... :For(A,1,5,1) and ...


14

Use Ans If you will only use an expression in the next line, don't store it to a variable! The special Ans variable is a one-byte token that stores the value of the last expression evaluated. Thus: Xsin(A)->R Disp R+tanh(R can be Xsin(A) Disp Ans+tanh(Ans saving two bytes.


11

Replace string!="" with string>"" SB allows you to do greater/less comparisons on strings, based on their codepoints. However, the empty string is considered the smallest string there is. So for situations where you do string!="" you can use either string>"" or ""<string, since every string is greater than "" and "" is less than every string. ...


10

Know your looping constructs QBasic has several looping constructs: FOR ... NEXT, WHILE ... WEND, and DO ... LOOP. You can also use GOTO or (in some situations) RUN to loop. FOR ... NEXT is pretty good at what it does. Unlike in Python, it's almost always shorter than the equivalent WHILE or GOTO loop, even when it gets a little fancier: FOR i=1TO 19STEP ...


9

Use a lookup table encoded in floating-point numbers A slightly advanced tip: Small lookup tables are useful for code golf: it's very often that we need a function that maps, for example, 0 to 1, 1 to 2, 2 to 1, and everything else to 0. However, TI-BASIC arrays are not suited for this purpose: for one thing, they're one-based, and for another, a value ...


7

Write and run the VBA code in the Immediate Window The Immediate Window evaluates any valid VBA executable statement. Simply enter a statement in the Immediate Window as you would in the code editor. It quickly executes VBA code and it can save many additional characters because: Putting the question mark (?) at the beginning of the statement tells the ...


6

Using ?, ., @, and unclosed strings Many dialects of BASIC support ? for printing, and SB is no exception. Having an extremely short text output function is a big advantage. In SmileBASIC, . is evaluated to 0.0, so it can be used in place of 0 to save space. For example: SPSET 0,21 can be SPSET.,21, saving 1 byte. (SPSET0,21 is invalid because SPSET0 ...


5

Evaluate() And [] As has been pointed out previously, hard-coded range calls can be reduced using the square brackets [A1] notation. However it has many more uses than just that. According to MSDN documentation, the Application.Evaluate() method takes a single argument, which is a Name, as defined by the naming convention of Microsoft Excel. N.B. [...


5

Combine Next Statements Next:Next:Next May be condensed down to Next k,j,i where the iterators for the For loops are i,j, and k - in that order. For example the below (69 Bytes) For i=0To[A1] For j=0To[B1] For k=0To[C1] Debug.?i;j;k Next Next Next May be condensed down to 65 Bytes For i=0To[A1] For j=0To[B1] For k=0To[C1] Debug.?i;j;k Next k,j,i And as ...


5

Smaller list generation If you need a list {1,2,...,N}, where N is, say, 42, the obvious way to create it is seq(X,X,1,42. However, one byte smaller than that is a neat hack using the binomcdf( (cumulative binomial distribution) command. cumSum(binomcdf(41,0 This only works when N is a constant, because the savings comes from replacing N-1 with its ...


5

Skip unnecessary variable initialization Current consensus is to allow all code to be run on a fresh interpreter. We can take advantage of this—all uninitialized real variables start at 0 in TI-BASIC, and Xmin starts as the possibly useful value -10. So if you ever need to take a running total in a program that doesn't take input from Ans, or you really ...


5

Put repeated expressions equation variables. EX: Remainder(randInt(1,9),1 Remainder(randInt(1,9),5 Remainder(randInt(1,9),10 Can be: "randInt(1,9→u Remainder(u,1 Remainder(u,5 Remainder(u,10 Note: it is hard to find a good use for this, but that does not mean you should forget equation variables :P Source: http://tibasicdev.wikidot.com/selfmodify -...


5

Use shortcuts for PRINT and REM You can use ? instead of PRINT, and ' instead of REM (comment). ' might also come useful when polygloting with languages that support ' as a part of char or string syntax.


5

Scanner abuse As in a lot of languages, knowing which characters can and can't be removed is important. Any space next to a symbol can be removed: IF""=a$THEN?0 Space can usually be removed between a digit and a letter occurring in that order: FOR i=1TO 10STEP 2. There are some differences between QBasic 1.1 (available at archive.org) and QB64: QBasic 1.1 ...


5

Divisibility testing In programs that require you to test whether one integer is divisible by another, the obvious way is to use MOD: x MOD 3=0 But a shorter way is to use integer division: x\3=x/3 That is, x int-div 3 equals x float-div 3. Note that both of these approaches will return 0 for falsey and -1 for truthy, so you may need to negate the ...


4

Eliminate End statements for If blocks at the end of a program Saves two bytes: one for the End and one for the linebreak. It also allows you to use the implied Disp on the last line, often saving an additional byte. [code] If A>5 Then Output(1,1,Ans²+Ans+A Disp 3ln(A End //end of program Can be: [code] If A>5 Then Output(1,1,Ans²+Ans+A 3ln(A //end ...


4

Spaces after keywords are optional because of tokenisation. For example: INPUT answer$ PRINT answer$ IF answer$ = 'Hi' THEN PRINT 'Hi!' ENDIF Can be shortened to INPUTanswer$ PRINTanswer$ IFanswer$='Hi'THEN PRINT'Hi!' ENDIF The corollary of this is, a variable name cannot start with a keyword. So PRINTa is not a valid variable name. However printa is....


4

Know your idioms Here are some snippets I commonly use in code golf: Convert to truth-value (0/1): not(not(Ans, or Ans and 1. Which one to use depends on the parentheses needed. Add one to a truth-value: int(e^(Ans. Saves an open-paren over 1+(Ans. Very useful, because TI-BASIC has one-based arrays. Map {0,1} to {1,-1}: cos(πAns. Saves one byte over 1-2Ans. ...


4

Know your input methods QBasic has several ways to get user keyboard input: INPUT, LINE INPUT, INPUT$, and INKEY$. INPUT is your standard multipurpose input statement. The program stops what it's doing, displays a cursor, and lets the user type some input, terminated by Enter. INPUT can read numbers or strings, and it can read multiple values comma-...


4

Combine Next Statements Next:Next:Next May be condensed down to Next k,j,i where the iterators for the For loops are i,j, and k - in that order. For example the below (69 Bytes) Input n,m,o For i=0To n For j=0To m For k=0To o ?i;j;k Next Next Next May be condensed down to 65 Bytes Input n,m,o For i=0To n For j=0To m For k=0To o ?i;j;k Next k,j,i And as ...


3

Prepare For Pixel Art Pixel art is by far one of Excel's strongest areas, as there is no need to construct a canvas, as it is already there for you - all you need to do is make some small adjustments 1) Make the cells square Cells.RowHeight=48 or, Alternatively, for 1 byte more, but far easier to work with Cells.ColumnWidth=2 2) Color the needed ...


3

Reduce Range("A1") and Like Calls Range("A1").Value(17 Bytes) and the simpler Range("A1")(11 Bytes) may be reduced down to [A1] (4 Bytes)


3

Remove Spaces VBA will auto-format to add a lot of spacing that it doesn't actually need. There' an answer on meta that makes a lot of sense to me why we can discount bytes added by auto-formatting. It's important to always verify you haven't removed too much, though. For an example, here's an answer of mine that was reduced almost 22% just by removing ...


3

STDIN and STDOUT Inputting to Subroutines and Functions via input variables Public Sub A(ByRef B as String) May be reduced down to Sub a(b$) The Public and ByRef calls are the default for VBA and thus implicit, and may (almost) always be dropped. The type literal $ forces b to be of the type String. Other type literals ! Single @ Currency # Double %...


3

If you find yourself using 0→G ;or any other variable ;other lines of code Then, you could use (to save a byte): DelVar G;other lines of code This is because when you delete a variable (G), it becomes its default value, in this case, 0. Then, you could put another line after the DelVar statement, without a newline. Be careful when putting crucial control ...


3

Which list variables to use? When using lists, avoid the default lists L₁ through L₆ in favor of named lists with one-letter names: ᶫA through ᶫZ (where ᶫ is the little L). Either one costs two bytes to reference (though L₁ is a single token, it is a two-byte token) but when storing values in a list, you can drop the ᶫ symbol, saving a byte: {1,2,3,4,5→...


3

Use string indexing instead of MID$ The MID$ function is a common function in many BASICs to get a substring from somewhere in the middle of a string. However, if you just need to get the character at some index, using string indexing is far shorter. For example: PRINT MID$("ABC",2,1) PRINT "ABC"[2] Both of these print C. Strings support array-like ...


3

In QBasic, it is customary to use the DIM statement to create variables, giving them a name and a type. However, this isn't mandatory, QBasic can also derive a type by the suffix of the variable's name. Since you can't declare and initialise a variable at the same time, it's often wise to skip the DIM in codegolf. Two snippets that are functionally identical*...


3

PRINT (?) has some quirks Numbers are printed with a leading and trailing space. Printing adds a linebreak. This behaviour can be altered by adding a comma at the end of the statement to insert a tab instead, or a semi-colon to avoid any insertions: It is not necessary to use & or ; between distinct operations when printing, eg. ?1"x"s$ shall ...


3

LOCATE can be really powerful The LOCATE statement allows you to place the cursor anywhere on the screen (within the usual 80x40 character space limits) and print something at that location. This answer to a challenge really shows this off (and is also combined with a lot of other tips from this topic). The challenge asks us to output every character a user ...


Only top voted, non community-wiki answers of a minimum length are eligible