Interactive · 1 question
Code
Code will be written by numbering each line. Many special tags will be written in bold and caps lock, including but not limited to: TRUE, FALSE, IF, ELSE IF, ELSE, FOR, IN, WHILE, FUNCTION, RETURN, etc. Indentation is also very critical to how to read code, as you will see later. Code comments will be written using //.
Variables
Just as in math, we can make variables and give them values. We denote to denote the assignment of a value to a variable. For example:
1:
creates a variable called and assigns to it the value .
Variables (as implied by the word variable) can change their values. For example:
1:
2:
Each time a is called, we compute the expression on the right of the , then assign that value to the variable on the left. Therefore, after line 1 runs, then computed, and then it is assigned to it. So after line 2 runs.
What is the value of after the following code runs:
1:
2:
3:
4:
Conditionals
A conditional tests if some logical statement is TRUE, and runs some code if it is. We write IF before the statement we want to test. We indent to denote the code that we want to run. For example, let's analyze the following code:
1:
2: IF
3:
4:
On line 1, we assign the value . On line 2, we test if . If it is, we run the indented lines (lines 3,4). In this case, evaluates to TRUE, so we do run lines 3, 4. On line 3, is assigned the value of . On line 4, is assigned the value of .
Using the ELSE tag, we can tell the program to do something else if the condition evaluates to FALSE
1:
2: IF
3:
4: ELSE
5:
After line 1, has value . On line 2, we test if . Since it is FALSE, we skip to line 5. Then gets value . After this terminates, .
If we changed line 1 to 1: . Then the condition in line 2 would evaluate to TRUE and thus line 3 would be ran. So gets value , and then the program terminates.
Finally, there is one more important tag: ELSE IF. This allows us to check multiple conditions in one big conditional. For example:
1:
2: IF
3:
4: ELSE IF
5:
6: ELSE
7:
This program is equivalent to the mathematical function, depending on the value given to in line 1:
Since , this program will output .
Loops
We can tell a program to run code several times. The FOR loop iterates a variable through a list, performing the indented code each time. For example, if we wanted to find the value of , we could equivalently write the following code:
1:
2: FOR IN [1, 2, 3, 4, 5]
3:
First line is ran with . Then it is ran with . This continues up to . At each time, it increments by . So after this code runs, .
Then there is the WHILE loop. It runs until a condition is TRUE. Another program to calculate this sum could be:
1:
2:
3: WHILE
4:
5:
This initializes and runs the program as long as the condition is true, incrementing the sum by and then incrementing by each time. Thus at the end will also have value .
Functions
We can define functions in the mathematical way, using FUNCTION (we use the RETURN tag to determine what the function outputs):
1: FUNCTION
2: RETURN
3:
Then we will have after execution. We can also make a function run some code and return a value at the end (using RETURN). For example the function can be written as:
1: FUNCTION
2:
3: FOR IN [1, 2, ..., ]
4:
5: RETURN
This function runs lines 2-4, then returns the value of the sum at the beginning of line .