Flow Control!
My roommate and I like to joke about programming, our favorite line being “A computer will ALWAYS do what you tell it to but not necessary what you want it to do.” That is to say if you write a program it will do everything EXACTLY as you write it, and it will do it this way every single time until you change something.
Before we jump into Flow Control there is something else we need to cover first. Operators! Operators are what we will use to compare data. Why would we need to compare data you may be asking? Well… Do you want your code to run EVERY time?! I honestly would not know how to “run the code every time.” One of the two main problems with that statement is that a computer does not know time, in order for a computer to know time we have to define time for it. The other problem being the fact that it would still not make sense to “run every time” after we define time for the computer. “Running every time” is an incomplete thought! You need to say something like “run every time 1 second passes” or “run differently every 5 times you execute.” So we use operators to make comparisons like these. Remember an operator will return true or false after comparing something. This will be explained once we have covered all of the operators.
1. Equal “==” True if something equals something else.
Example: “3 == 3” returns true, while “3 == 5” returns false.
2. Not Equal “!=” True if two things are not equal to each other.
Example: “3 != 5” returns true, while “3 != 3” returns false
3. Less than “<” True if a number is less than another.
Example: “3 < 5” Returns true, while “5 < 3” returns false
4. Greater than “>” True if a number is greater than another.
Example: “5 > 3” Returns true, while “ 3 > 5” returns false
Note: Both Less than and Greater than can add an equals sign to become Greater than or equal to OR Less than or equal to.
Example #1: “5 >= 5” will return true. While 5 is not greater than 5, it is equal to it.
Example #2: “5 >= 6” will return false. 5 Is not greater than or equal to 6.
5. Not “!” This is used to see if one this is not another.Example: “!Running” (where Running is a Boolean variable) will return True if the value of Running is currently false, or false if the value of Running is currently true.
6. And “&&” Will return true if two separate statements are true and false if one or both of them are not.Example #1: “x == 3 && x == 4” We know this will return false because x can't be equal to both 3 and 4.
Example #2: “x == 3 && y == 6” This will return true if the current value of x is 3 and the current value of y is 6. But if x is not currently 3 or y is not currently 6 then this will return false.
7. Or “||” Will return true if one statement is true or the other statement is true.Example: “x == 3 or x == 5” will return true if x is currently equal to 3 or currently equal to 5.
8. Xor “^^” Will return true if only one of the statements is true.Example: “x == 4 ^^ y == 4” If the current value of x is 4 and the current value of y is 7 this will return true. BUT if the current value of x is 4 and the current value of y is 4 it will return false, both statements cant be true if you want to return true.
Phew! Glad that's all over ;) Now to actually get into Flow Control.
Flow Control defined by Google: "In computer science, control flow (or alternatively, flow of control) refers to the order in which the individual statements, instructions or function calls of an imperative or a declarative program are executed or evaluated."
Flow Control defined by Elvish: Flow control defines what order of operations will happen. Do you remember PEMDOS from primary school? (Please Excuse My Dear Aunt Sally, or Parenthesis Exponents Multiplication Division Addition Subtraction?) This is a perfect example of flow control. You work through things in the order in which they are supposed to happen.
Types of Flow ControlThe User's Manual states there are 4 separate ways to control the flow of your script.
Note: These should always all be lowercase when using them in your scripts.1. If
2. Else
3. Else If
4. While
Note: There are four possible scenarios which you can have with your if statements.
#1. Just an If statement
#2. An If statement and an Else statement
#3. An If statement and a (or many) Else if statement(s)
#4. An If statement, a (or many) Else If statement(s), and an Else statement.
You may not have an else or an else if without an initial if statement and you cant put the else statement before the else if statement(s).
Personally I lump If, Else, and Else If into the same category because in my mind they are all one and the same. They rely on each other to exist.Remember when I said that operators will return true or false? Good! Because now we are getting to that.If:If is probably the most important flow control piece you will use while coding! Basically you say “If this one thing is true, then do this other thing.”
Example: If your username is Elvish then say “Hello World!”
So I would say “Hello World!” because my username is Elvish. The statement is TRUE.
But if you read that statement you would NOT say “Hello World!” The statement would be FALSE, because your username is not Elvish.
Example:
- Code: Select all
if(3 == 3){
x = 7;
}
3 is equal to 3, so this statement is true. This statement is true so it will then execute “x = 7;”
Where as:
- Code: Select all
if(3 == 4){
x = 7;
}
3 is not equal to 4, so this statement is false. This statement is false so it will NOT execute “x = 7;”
Note #1: The initial bracket can be placed on the line below the if(3 == 3) but I leave it where it is because I feel as though it is easier to read what is going on in the code.
Note #2: When you want to run code every line of code MUST end with a semi colon. This is to say if you want to change the value of a variable or execute any kind of code. Your if block will not end in a semi colon nor will your if statement which in this case is “3 == 3”
Else If:This can ONLY be used after an if block, and you can use as many else if statements as you want. The else if block will ONLY be read by the Titan One if the initial if block is found to be false. Remember when I said If, Else if, and Else were all tied together? The following examples will show you what I mean!
Example #1:
- Code: Select all
if(3 == 5){
x = 1;
}
else if(3 == 3){
x = 7;
}
In this instance the Titan one would say, 3 is not equal to 5 so skip over the first if block. Then it would see that 3 is in deed equal to 3 and read the following else if block. Changing the value of x to 7.
Example #2
- Code: Select all
if(3 == 5){
x = 1;
}
else if(3 == 3){
x = 7;
}
else if(7 == 7){
x = 10;
}
In this instance the Titan one would say, 3 is not equal to 5 so skip over the first if block. Then it would see that 3 is in deed equal to 3 and read the following else if block. Changing the value of x to 7. The Titan One wold then skip over the rest of the else if statements (and the final else statement if you have one) because it found something in the if, else if, else group that was true. The Titan One will ONLY look for one instance of the group where the statement is true then skip the rest of the group.
In order to do more then one calculation you would have to add another group such as the following:
- Code: Select all
if(3 == 5){
x = 1;
}
else if(3 == 3){
x = 7;
}
else if(7 == 7){
x = 10;
}
if(3 == 7){
x = 4;
}
else if(8 == 8){
x = 2;
}
Now in this instance the value of x would turn out to be 2.
In this instance the Titan one would say, 3 is not equal to 5 so skip over the first if block. Then it would see that 3 is indeed equal to 3 and read the following else if block. Changing the value of x to 7. The Titan One wold then skip over the rest of the else if statements (and the final else statement if you have one) because it found something in the if, else if, else group that was true. BUT then the Titan one would read the next if statement because it is a new group. It would say 3 is not equal to 7, skip! 8 is equal to 8! And then change the value of x to 2.
Else:This is the easiest to follow piece of flow control. If the if statement and none of the else if statements were true, run this. That is to say, the else block does not compare any data, it just runs when no other cases are found to be true.
Example:
- Code: Select all
if(3 == 7){
x = 4;
}
else if(8 == 3){
x = 2;
}
else if(3 == 9){
x = 8;
}
else{
x = 17;
}
In this instance x would come out to 17 because none of the if or else if statements were true, leaving the else block to be read and executed.
While:The while loop is one of the less used ways of creating flow control (at least when it comes to Titan One scripting) But it can be useful none the less!
The while loop will be read until the requirement is met. For example I could say, go forward while the light is green. Once the light turns yellow you would stop going forward. But up until that light turns yellow you can go as far as you want!
Example:
- Code: Select all
int x = 0;
int y = 0;
while(x < 5){
x = x + 1;
y = y + 2;
}
This while loop would run 5 times. X starts out at 0, and every time the while block is run it will add 1 to x. Thus at the end of the calculation x will be 5 and y will be 10.
Run #1: Totals: x=0 y=0. Check: Is x less then 5? YES! Add 1 to x and add 2 to y.
Run #2: Totals: x=1 y=2. Check: Is x less then 5? YES! Add 1 to x and add 2 to y.
Run #3: Totals: x=2 y=4. Check: Is x less then 5? YES! Add 1 to x and add 2 to y.
Run #4: Totals: x=3 y=6. Check: Is x less then 5? YES! Add 1 to x and add 2 to y.
Run #5: Totals: x=4 y=8. Check: Is x less then 5? YES! Add 1 to x and add 2 to y.
Run #6: Totals: x=5 y=10.Check: Is x less then 5? NO! End the while loop.
Note: The while loop will continue to run without re-starting the main loop. This will be explained in the next post where we start to tie these concepts together.