digital literacy for everyone



[lit] although quasi 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, but if you want to do that in javascript or quasi youll likely want to simulate that using other means-- otherwise the browser wont respond until the loop exits. quasi has a for loop... first, lets create a hello-world-type example that simply outputs a line of text: now " this line again" print quasi has a numeric loop, called a for loop. for loops also exist in javascript and python. quasis for loop requires a variable, a starting number, a number to count to, and how much to add on each loop. lets add our loop: each for 1 15 1 now " this line again" ; print next the indentation is optional. for marks the beginning of the loop, and next marks the point at which to loop back. weve got a single leading space in the string, next to the word "this". that extra space has nothing to do with the programming language, you will now find out why that space is there. like many languages, quasi 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. each for 1 15 1 now randint 1 15 ; prints now " this line again" ; print next running the program, the output looks like this: 10 this line again 13 this line again 14 this line again 13 this line again 6 this line again 1 this line again 7 this line again 13 this line again 2 this line again 11 this line again 8 this line again 2 this line again 5 this line again 15 this line again 9 this line again the colour command takes a number from 0 to 15. 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: each for 1 15 1 now randint 1 15 ; colour now now " this line again" ; print next [img]loops.png[img] you can count backwards as well: number for 5 1 -1 now = number ; colour 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) quasi has different kinds of loops-- the for loop simply runs until it is out of range of the first two numbers you give it. 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-- thats in the next section. quasi has a loop for arrays, called a forin loop. using a for loop to generate numbers, we could use arrget to print each item of an array. but quasi also offers the forin loop, similar to "for variable in array" in python, to make this easier. if we have a grocery list that says: 1 litre milk 1 dozen eggs 2 kg sugar if we have it in an array, we can print each line like this: f "1 litre milk|1 dozen eggs|2 kg sugar" f split f "|" # create array each = forin f 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, or items in an array. the next section is on [[conditionals]].
back to quasi concepts: [url][url] quasi main page: [url][url] home: [lit][lit]