a review of the 7 basic concepts

theres nothing better for a difficult spot in the learning process (for those wondering: this is around the time functions are being learned) than a little review of whats been covered so far, so heres a quick review: note the substantial range of concepts:

(note: although fig examples are used, most of the concepts illustrated here are common to many languages. the ideas of a “main variable” and “shared lines” are less common, but similarities exist to features in other languages.)

 

variables: as you now know, each one of these holds a piece of information. the piece can be large, but the piece count is singular. each variable has a unique name (you supply the name.)

p = 5 # set p to 5
p = "hello there" # set p to "hello there"
# p is the main variable in these lines
x = p # set x to the value of p (copy p to x)
# x is the main variable in that line, holding "hello there"

 

an array is like a variable that can hold several pieces, and has a name and a numeric index.

p = "hi" ; arr # p is "hi" ; create array named p holding "hi"

 

most lines of code in fig begin with a “main variable” that you supply the name for; such lines are called “shared lines” because several commands can share the line (with the main variable.) unshared lines are used by a few commands in fig: the “block commands” (you will find out about those in a moment) and a small number of others, get the line theyre on all to themselves.

 

input: information goes from the outside world to the computer and/or program, through a physical device such as a keyboard or mouse or camera.

by extension, this includes files which are stored on a physical device. by further extension, this includes anything that simulates a file system. if you like, you could say a program or function has “input” in the form of parameters.

an input command gets information from the world and brings it into a program, usually by putting it in a variable.

x = lineinput # get keyboard input, storing whats typed as x

 

output: information goes from the computer to the outside world, to a physical device such as speakers, a screen or a printer. some devices are in fact input and output devices, such as a modem or other network device (and really, a printer.)

output includes files. through “pipes,” the text output of one program can become the input of another program.

an output command sends information from the program to the outside world.

x = "hello there" ; print # x is "hello there" ; put on screen

 

basic math: as everything is a number to the computer, a great deal can be accomplished with numeric manipulation: or simple math. the good news is that a lot of this math can be abstracted into word-based (rather than numeric) commands, and also the computer will do the math for you.

computing does not require fantastic math skills, but it will help to build math skills anyway. this is simply one more advantage of learning the basics of code and 100, 105, 103, 105, 116, 97, 108, 32, 108, 105, 116, 101, 114, 97, 99, 121.

x = 5 ; plus 2 ; print # x is 5 ; add 2 to x ; put x on screen

 

loops: a loop begins on a single line of code, then there are one or more lines of code “inside” the loop; the end of the loop is marked with another line (at least in fig, and some other languages.)

code inside the loop repeats until the loop is “broken” or “finishes.” a for loop finishes when there are no more numbers in the range specified. a forin loop finishes when there are no more items to “loop (or ‘iterate’) through.” a while loop (in fig) just keeps going until you use the break command, which can end the other types of loop early.

the way break is normally used is with a conditional; that way, the loop continues until it finishes, or until the conditional is true.

while
x "hello again" print # set x, then put x on the screen
wend

 

you can “break” while running the program, using ctrl-c on the keyboard.

a group of lines that start a loop, end a loop, and consist of the “inside” of the loop, are collectively referred to as a “command block.” all command blocks in fig can be ended with the fig command. for the for loop, you may use next instead. to end a forin loop, you could use nextin (or next if you like.) while loops may end with wend. use fig except where you prefer one of these.

 

conditionals: a conditional is a command block which starts with commands such as iftrue, ifmore, ifless, or ifequal. iftrue runs the block it begins if the value (or variable) paired with it is non-zero. this means that if you run iftrue on the string value “hello world”, it will run the code inside the block because “hello world” has more than zero characters. -.001 is also “true” despite being negative, because anything other than exactly zero is non-zero.

ifmore compares two values or variables, (or one value with one variable, etc.) and is “true” if the first value is greater. this includes the alphabetical order of strings, by the way. ifless works like ifmore, except it runs if the first value is less than the other. ifequal runs if both values/variables are equal.

try/except/resume is a three-line block command (with at least one line “inside” each of its two sections) that runs the code inside the second section on the condition that the first section creates a program error. this kind of conditional is used to “trap” and then respond to errors– perhaps to ask for different input, or display a custom error message of your own wording.

ifmore 5, 7
x "this line will not run, because 5 is not more than 7" print
fig

try
x = 5 ; divby 0
except
p "you try, but find you still cant divide things by zero" print
resume

 

functions: truth be told, every command in fig is much like (or actually) a function. in fig, the function command lets you create your own custom commands! a function is a block of code with (optional) parameters defined by you in the top line, and an (optional) value returned by the return command when the function is “called” (that is, when the function is used like any other command.)

in a program, a function has two forms: a definition, and a call. the definition (using the function command) outlines ordinary fig code in a block. the “call” is where you use the name of your defined function anywhere in a program: a function can even call itself from inside its definition!

function onceagain
z "this line is printing again" print
x onceagain
fig

 

a function calling itself is an example of “recursion.” there are situations where recursion is an extremely useful design; in this example, the recursion simulates a loop. fig runs on python, and python has an (adjustable) recursion limit: after it gets about 1000 calls “deep,” a function will not be able to continue calling itself from the same point.

most programs will call their functions from somewhere else than inside the function; in other words, most function calls are not recursive.

fun note: the gnu operating system has a recursive acronym: gnu stands for: gnus not unix. the g in “gnu” stands for: gnu. (the people that put the operating system together named it this way because they thought it was funny. the fact that this is what is considered “funny” to programmers is itself kind of funny…)

most programming consists of these (or variations on these) 7 concepts!

  • variables
  • input
  • output
  • basic math
  • loops
  • conditionals
  • functions

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s