Coding in fig Lesson 1: Variables

Description

How to set variables
Mr. Fig
Note by Mr. Fig, updated more than 1 year ago
Mr. Fig
Created by Mr. Fig over 7 years ago
40
0

Resource summary

Page 1

Lesson 1: VariablesTo fully appreciate the power and feel of coding, it is necessary to write or at least "run" code. You can get a glimpse of this from watching someone else run code, but you will not experience the confidence or joy of being able to tell the computer something and have it do what you say unless you actually do it. One of the goals of fig is to avoid "formulas" by default. In fact you can create all kinds of formulas using fig, and if it feels too limiting you can add Python code inline to a fig program. Python allows full formulaic expressions, of the kind fig deliberately avoids. By allowing this kind of extension, fig is both friendly and powerful. In fact you could simply start with Python if you had the interest in doing so, but fig will provide a number of friendly shortcuts that allow you to quickly progress without getting slowed down by too many implementation details.Variables are one of the most universal aspects of programming, and unlike the Basic example in the previous section, fig requires you to either use Python or a variable to create a "Hello, world" program.A few keywords in fig are designed to stand alone, but in general most lines of fig code begins with a name supplied by the user. You can use a letter, like "z" or the word "Now" (fig is not case-sensitive, so "Now" and "now" and "NOW" refer to the same variable) but this "main variable" is a feature of fig that often saves you the trouble of referring to it several times in the same line.To return to our Basic example:print "Hello, world!"Fig is designed to be read left-to-right, and does not use parentheses (at least they do not have syntactic importance; they are decorative, and strictly for the user to determine the significance of.) In fig, the above line would start with a variable. For the moment, let's just use "Now":now "Hello, world!"In Basic, this would be now$ = "Hello, world!" and in Python it would be the same, except for the $ after the word "now."In fig this creates (or resets) a variable named now – which also gets set to a numeric value of zero (0.) The next part of the line changes the value from 0 to a "string" of characters in quotes, in this case the phrase "Hello, world!" Sofar, nothing visible is done with this information. The program sets the value of a single variable, and does nothing else. But it goes left-to-right very clearly: The first bit of text names and creates a variable. The second bit of text changes the information (the value) that is stored by the variable. The next thing we add to the line will probably do something with that variable, too. now "Hello, world!" printOkay, so now we've added the print command, same as Basic and Python. And so our variable now is output ("printed") to the screen.You can do other things with the now variable; you could make it all upper-case, before or after it prints, using the ucase command:now "Hello, world!" print ucaseThis changes the value to upper case, but(left to right) it prints before that happens, so no change is yet visible. This:now "Hello, world!" ucase print...changes the value before using print and so you can see the change in the output.Fig is different than Basic in that the most obvious or typical example (Hello, world) is made purely of output statements in many languages. In fig, you begin with variables right away. This is primarily done for friendliness, but it also means that your first program will likely contain 2 of the 7 core concepts, (variables and output) instead of just output. Although this is functionally similar or identical to a formula, I think it's a better introduction to what programming is really about.Fig doesnt actually avoid the functionality of a formula, but the parenthetical structure that makes it look "too much like math" to the math-phobic. It is still mathematical in nature, but closer to natural or spoken language in syntax and structure.Copying variables is much like setting them. If you want to copy the value of the variable now into say, another:now "Hello, world!" ucaseanother nowSo now is still "Hello, world!" but so is another, and you can use/change each variableindependantly (or use one as a backup, to check later if the other has changed.)Each of the two lines above start with a "main variable" and the program code on each line works with that variable: Putting a value after the main variable (this is optional) will set it Putting another variable (also optional) after the main variable will copy its value into the main variable. Some commands will read/use, but not change the main variable; print is an example. Some commands will write/change the main variable, but not read it. Some commands will read and change the main variable-- ucase is such an example. Some commands will not read or change the main variable, such as cls; it does the same thing that cls in Basic does. Most commands in fig share a line / begin a line with a main variable, which is not optional. When fig gains a new feature, it is generally a "shared-line" command, or not a program keyword at all.However, a few keywords do not share a line; they get their own line and do not require (or use) a main variable; commands/keywords of this type are not generally added to fig.These "own-line" commands are:graphics / textmodefor / forinwhilebreakpassfunctionpythonfig / next / nextin / wendiftrue / ifequal / ifmore / ifless / elsetry / except / resumeThese commands are so important, they get their own line-- if only to stand out as part of a command block (a multi-line series of commands) or as a command that significantly changes the behavior or mode of the program.All other commands require a line that starts with a main variable:now cls # cls is a command, now is main varIn the above example, # is a special case-- it tells fig to ignore the rest of the line so that it can be used to "comment" the program. Technically # is a command or keyword, but it can be on a shared-line with a main variable, or begin a line, or be on a line by itself. However, it also isnt really part of the program; it just begins a note inside the program code.If you put a # in front of a line of working code, it will turn that line into a comment and thus disable the line or stop it from running as part of the program. This can be useful for testing the functionality of a single line or group of lines, and using it this way is called "commenting out" a line.What else can be said about variables? So far, we have only demonstrated keywords that work without any additional details in the way they are written: most keywords, like cls and print and ucase are simply tacked on to the rest of a line, at the right of the line. The program goes from left to right, and then to the next line.Some keywords go on their own line, such as textmode or while, and do not share a line with a "main variable" to start, or with other commands. But we haven't talked about parameters yet.Apart from the start of a line, or the second "word" in a line (where it is used to copy one variable's value into another) the place you are most likely to see a variable is as the parameter for a command that has them.Most fig commands have zero parameters, or one parameter. A few have more than one, but each command has the same (fixed) number of parameters in every context, except for function (used to define new fig commands.)Therefore cls always has zero parameters; while always has zero parameters; ucase always has zero parameters, and textmode always has zero parameters. The keywords left and right always have a single parameter; it defines how many characters you want to keep from one side of a string.To demonstrate:now "hello" print left 2 print First now is created, set to "hello", then printed, then left 2 is run before the result is printed again. The output is:hellohe We've used four spaces (not that you can tell on this site feature) to separate each command, though one space is sufficient. The left command includes 2 as a parameter, as the number of characters to take from the side of the string that's stored in the main variable. It then changes the main variable to the leftmost 2 characters.Since print is called before and after left 2 runs, it displays the value of now before and after as well. Like most commands that have parameters, left does not have to use a "constant" value like 2, but can use a variable instead:howmany 2now "hello" print left howmany printUsing variables as parameters allows the program to have more control at run-time (it allows it to be more flexible and powerful) and is somewhat the essence of things being "programmable." It allows the program to take input and have it affect the output, even before we get into conditionals.As mentioned, the extra spaces are optional command separators (purely for the visual aspect of the code) and each of these lines are valid and do the same thing:now "hello" print left howmany printnow "hello" print left howmany printnow "hello" : print : left howmany : printnow "hello" ; print ; left howmany ; printThe colon : is the traditional command separator in Basic, and in C and Python and JavaScript, a semicolon ; is used.It is worth talking about variable types (or rather data types, since any variable in fig could hold one or more type of value) but the subject is simple enough that the point of mentioning it might not be obvious:* string type: data "in quotes" is a string of characters, which may include numbers but they will not be treated as a numeric value. "5" plus "5" returns "55"10* integer type: an integer, also known as a "whole number," is a numeric and has no decimal point. 5 plus 5 returns 10* float type: numeric and includes a decimal point; allows decimal values. Like Basic, fig handles floats and integers pretty seamlessly, but strings have to be converted using the val command to be treated as numerics. 5 plus 5.7 returns 10.7* array type: while variables hold one value at a time, a variable can be converted to an array holding the same value, plus others.now 5 arr # create an array holding 5 now 5 arr times 100 # array holds 100 5sNormally the times command is used for basicmultiplication, but when used on a string it sticks several copies of that string together into one:p "hello" times 4 # "hellohellohellohello" When you start a line with a main variable that you've already used, it sets it to zero:p print # displays 0p 5 print # displays 5p print # displays 0 againp plus 7 print # displays 7Arrays are the exception to this rule. Since they might hold a lot of information, they do not get cleared on use as a main variable unless you explicitly clear them:p 5 arr print # displays [5]p print # displays [5]p 0 print # displays 05 plus "5" plus 5.0 returns [5, '5', 5.0]Arrays in fig are the same as arrays in Python, known in Python as "lists." Lists in Python (like in fig) are not restricted to a single type per array; they can hold a mix.Apart from converting a variable to array using arr, fig has several ways of creating an array: the split command can split a string into array elements. arropen and arrcurl load the lines of a file or webpage into an array, respectively. arrstdin creates an array from information "piped" in from another program. This allows you to mix the functionality of various programs together, even if they aren't designed as a group or suite. command creates an array of parameters the fig program was called with. These are not parameters of individual commands, but of the program itself. (This allows you to put together your own "language" from fig programs and others, such as bash utilities) inline python can be used to create arrays arrshell runs command line programs, and returns the output of those programs as an array.

Show full summary Hide full summary

Similar

computer systems and programming quiz
Molly Batch
Fetch-decode-excecute
Brodie McMeowface
Types of systems
Brodie McMeowface
DATA
Haha_fizul
Hardware mindmap
demi cheo
INFORMATION SYSTEM
ahmed hany
expert system
limy12345
Data
Jing Heng
Fetch-decode-excecute
джордж гаврилович
computer systems and programming quiz
Jack Wheat
DATA_v1
chong.lee.soong