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]] conditionals are one of the most native computing concepts-- if two values compare, branch to a specific location in code. for conditionals in fig, as with loops or inline python, you mark the top of the code to be run conditionally, and the bottom of the code to be run conditionally. username "what is your username? " prints lineinput ifequal username "bobby" email = arropen "user/bobby/email.txt" now = "you have mail:" print now = email ; print fig the code between the ifequal line and fig runs only if the person types in "bobby" as their username. of course the program should also check for a matching password, and probably do something different if there is no email waiting for bobby. you can nest conditionals though, so lets say bobbys password is "a-bad-password". we just add a second conditional inside the first one: [fig] username "what is your username? " prints lineinput ifequal username "bobby" pw "what is your password? " prints lineinput ifequal pw "a-bad-password" email = arropen "user/bobby/email.txt" now = "you have mail:" print now = email ; print fig fig the indentation and extra spaces between lines arent needed, theyre only there to help you focus on the relevant parts of the code. conditionals also make the break command useful. lets go back to one of our examples from the previous section on [[loops]]: while now randint 1 15 prints now " press ctrl-c to quit" ; print ; sleep .5 wend we can change that from " press ctrl-c to quit" to " press enter to get another number, or type 'q' and hit enter to quit " while now randint 1 15 prints now " press enter to get another number, or type 'q' and hit enter to quit " ; prints ; lineinput wend lineinput sets the variable now to whatever the user types in. the loop will continue after each wait for keyboard input, but we can add a conditional break that checks if it is q: [fig] while now randint 1 15 prints now " press enter to get another number, or type 'q' and hit enter to quit " ; prints ; lineinput ifequal now "q" break fig wend running the program, the output looks like this: 13 press enter to get another number, or type 'q' and hit enter to quit 6 press enter to get another number, or type 'q' and hit enter to quit 1 press enter to get another number, or type 'q' and hit enter to quit q if the user types q, the break command will stop the loop and the program will stop. ctrl-c still works either way. you could also check for "quit" or "exit" but it requires more typing from the user. if the user shifts or has caps lock on, they may type "Q" instead of "q". the program will ignore a "Q" so we can force the input to be lowercase using the lcase command: now " press enter to get another number, or type 'q' and hit enter to quit " ; prints ; lineinput ; lcase we can also use ifmore and ifless to check other conditions. but for comparing numbers, you want to convert keyboard input (always a string) to a number first: r = randint 1 10 now "guess a number between 1 and 10 - " prints lineinput val ifmore now r now "you guessed too high!" print fig ifless now r now "you guessed too low!" print fig ifequal now r now "you guessed right!" print fig remember our email example at the beginning of the section? we can use a three-part conditional called "try, except, resume" to run certain code if the file opens or if it doesnt: try email = arropen "user/bobby/email.txt" now = "you have mail:" print now = email ; print except now = "you have no mail." ; print resume this code can be nested between the other conditionals that check username and password. of course you may get tired of saying things like this every time you open a file: try email = arropen "user/bobby/email.txt" now = "you have mail:" print now = email ; print except now = "you have no mail." ; print resume wouldnt it be cool if you could say that just once in a program, then any time you wanted to open a file, you could just use a command like: file = tryopen "email.txt" [[functions]] let you make your own commands, just like this.
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]