digital literacy for everyone


[lit]

[generate-title]

[lit] although fig was designed to teach 7 programming concepts, these concepts are common or illustrative to some degree in many, if not all programming languages. learning these concepts will help you understand python, bash, javascript and many others. [[variables]] | [[input]] | [[output]] | [[basic-math]] | /loops/ | [[conditionals]] | [[functions]] loops are one of the oldest concepts in computing; you mark a starting point, and upon reaching the other point you jump back to the first. a while[lit]/[lit]wend loop is the simplest illustration of this. first, lets create a hello-world-type example that simply outputs a line of text: now " press ctrl-c to quit" print the program will quit the moment the text is printed, but we can slow that down slightly by adding a delay of half a second: now " press ctrl-c to quit" ; print ; sleep .5 the program will print the text, then wait half a second, then quit. lets add our loop: while now " press ctrl-c to quit" ; print ; sleep .5 wend the indentation is optional. while marks the beginning of the loop, and wend marks the point at which to loop back. weve got a single leading space in the string, next to the word press. this has nothing to do with the programming language, you will now find out why that space is there. like many languages, fig has a function that produces random numbers. it isnt truly random, it is created using an equation that outputs numbers which seem random until they are studied with a large enough set. we can add a command in our loop that prints one of these "pseudorandom" numbers, using the randint command. we simply tell randint the lowest and highest numbers we want it to return, and it is just like any other command that gives us math output. while now randint 1 15 prints now " press ctrl-c to quit" ; print ; sleep .5 wend just like that, our program is outputting roughly 2 "random" numbers per second, next to a message telling the user how to quit the program. running the program, the output looks like this: 10 press ctrl-c to quit 13 press ctrl-c to quit 14 press ctrl-c to quit 13 press ctrl-c to quit 6 press ctrl-c to quit 1 press ctrl-c to quit 7 press ctrl-c to quit 13 press ctrl-c to quit 2 press ctrl-c to quit 12 press ctrl-c to quit it keeps running until you hit ctrl-c. the colortext command takes a number from 0 to 15, but 0 is black and thats our background colour. the random number we are printing can be used to change the colour instead. this is when it becomes really neat that everything is numbers to the computer-- since numbers represent everything on the computer, you can convert things that dont seem related-- like height into letters, or colours into sounds. we are going to convert random numbers into random colours: [fig] while now randint 1 15 ; colortext now now "press ctrl-c to quit" ; print ; sleep .5 wend [img]loops.png[img] fig has different kinds of loops-- the while loop simply runs until something makes it stop. ctrl-c is one way to break from the loop, the break command does the same thing. we havent tried the break command in this section, because it will stop the loop immediately. for the break command to really be useful, we want to run it only when a certain condition is true-- conditionals are the next section. fig has a numeric loop, called a for loop. for loops also exist in javascript and python. figs for loop requires a variable, a starting number, a number to count to, and how much to add on each loop: for number 1 9 2 now = number ; print next this will start at 1 and stop at 9, adding 2 each time. when it gets to 9, it will stop. 1 3 5 7 9 you can count backwards as well: for number 5 1 -1 now = number ; colortext number ; print next that will output: 5 (printed in magenta) 4 (printed in red) 3 (printed in cyan) 2 (printed in green) 1 (printed in blue) using a for loop to generate numbers, we could use arrget to print each item of an array. but fig also offers the "forin" loop, similar to "for variable in array" in python, to make this easier. if we have a file called text.txt that says: 1 litre milk 1 dozen eggs 2 kg sugar we can print each line of that file like this: f arropen "text.txt" forin each f now = each ; print next with a conditional, we could print only the lines that begin with 1, or only items measured in kg, or only items that start with a vowel. but its the loop which lets us "iterate" or "loop through" a set of lines in a text file, or items in an array. its also a loop that keeps a program running, to ask for more input from the user. but we will demonstrate that in the next section on [[conditionals]].
back to fig concepts: [url]https://codeinfig.neocities.org/figconcepts/index.html[url] fig main page: [url]https://codeinfig.neocities.org/fig/index.html[url] home: [lit]https://codeinfig.neocities.org[lit]