An if statement checks if something is true and does something if it is.
The code within the brackets after the "if" is called the condition, there are a number of different things you can test for (see operators).
In the example above both "a" and 5 are symbols, functions can also be symbols:
That will test the return value of SomeFunction (see below) against 5.
You can also combine checks to check multiple things:
That example checks that a is equal to 5 AND b is not equal to 3, however b is 3 so the check fails.
That examples checks that a is equal to 5 OR b is not equal to 3, b equals 3 so that part fails* however a is 5 so that part is true, we are using || (or) so only one part needs to be true (if both are true the statement is still true, this is slightly different to the linguistic meaning of "or" meaning only one or the other), so the if statement is true.
Also it is possible to chain the comparisons together without the explicit AND-ing of two different comparisons.
The following are symbols you can use in comparisons and their explanations. Some have been used in examples already.
|==||Left is equal to Right||if (Left == Right)|
|!=||Left is not equal to Right||if (Left != Right)|
|>||Left is greater than Right||if (Left > Right)|
|>=||Left is greater than or equal to Right||if (Left >= Right)|
|<||Left is less than Right||if (Left < Right)|
|<=||Left is less than or equal to Right||if (Left <= Right)|
|&&||and||if (Left && Right)|
|||||or||if (Left || Right)|
|nor||if (!(Left || Right))|
|nand||if (!(Left && Right))|
|exclusive or (xor, eor) - only one or the other is true, not both||if (!(Left && Right) && (Left ||Right))|
|not exclusive or (nxor, neor) - both or neither are true||if ((Left && Right) || !(Left || Right))|
The other main aspect of if statements is brackets, these control the order things are done in:
There are two ways of looking at the above statement:
The first version will check if a is 5 and b is 3, if that is false (i.e. one or both of them are not their respective values) it will check if c is 1. (a == 5 && b == 3) is false as you should know from above, so you replace that group with FALSE:
We know FALSE cannot be true (as it is not true by definition), however c is 1 so that half is true and, as we're using an OR in between the whole statement is true.
The second version checks if a is 5, if it is it checks if either b is 3 or c is 1. The game will do the a == 5 part first but for clarity we'll do it backwards. (b == 3 || c == 1) is true, both halves are true, although only one needs to be, so going into our if statement we have:
(a == 5) is false, because a is 3, so we have:
Obviously FALSE is false so that statement cannot be true, so the check will fail.
This little example shows how using brackets can change the outcome of a check, without brackets the compiler will be the first of the two versions demonstrated but that cannot always be guaranteed so you should always use brackets, even if just to clarify what is going on to other people.
- (b != 3) in the OR example doesn't actually fail as it's never called, the compiler orders the code using a thing called short-circuiting, as the first part is already true there's no point checking the second part as it won't affect the final outcome, but if it did check it it would fail.
else basically does something if an if check fails:
An else if is a check which occurs if the first if check fails to check something else:
You can have an many of these as you like (you can only have one if and one else in a group of checks):
else ifs will only check the value as it was when the ifs start, so you can't do:
To get round this you would simply make the else if an if.
'?' and ':' together are called a triadic operator, they basically act as an if statement inside another statement:
That's a simple example to assign a variable based on another variable, however it can be made much shorter:
The part before the '?' is the conditional, this is exactly the same as a normal conditional. The part between the '?' and the ':' is the value to return if the condition is true, the other part is the value to return if the condition is false. You can stack them up the same as else ifs too:
Can be written as:
This is actually more similar to doing:
But they're equivalent (in this example anyway).
"while" loops do something while the condition specified is true. A condition is exactly the same format as the condition in an if statement, it is just checked repeatedly and the code done if it is true every time it is checked:
That code will check if 'a' is less than 10. If it is, the code inside the braces (a++;) will be executed, thus incrementing 'a'. When the closing brace is reached, code execution jumps back to the check and does it again, this time the check will fail as 'a' is 10 and execution will jump to after the loop. If 'a' started out as 8, the code would be run twice etc.
A "for" loop is essentially a compressed "while" loop. A "for" statement has three sections; initialization, condition and finalization. As a "for" loop, the "while" example above would be written:
That's a simple code to loop through all players:
Any of the conditions can be omitted simply by putting no code in them:
This example makes it a little easier to show how a "for" loop matches up to a "while" loop. There are two very slight differences between the two "for" loops given. The first is that the second example declares 'a' outside the "for" loop, this means it can be used outside the "for" loop, in the first example 'a's scope (the section of code for which a variable exists) is only inside the loop. The second difference is that the a++ in the second example is actually done slightly before the a++ in the first example, 99% of the time this doesn't make any difference, the only time it matters is when you're using 'continue' (see below), 'continue' will call the a++ in the first example, but will skip it in the second example.
A do-while loop is a while loop where the condition comes after the code inside the loop instead of before. This means that the code inside will always be executed at least once because it is done before the check is done:
If that was a standard while loop a would not be incremented as the (a < 10) check is false, but here it's incremented before the check. If a started as 9 the loop would also only be done once, 8 - twice etc.
This is essentially what the loops above compile to, the use of goto is generally discouraged however it's interesting to see exactly what a loop is doing:
OBOE stands for Off By One Error. This is a very common mistake where a loop runs for one too many or two few times. E.g:
This very simple example demonstrates one of the most common OBOEs, at first glance people may think this will loop through all the contents of b and set them to 0, however this loop will actually run 11 times and try access b, which doesn't exist (it would be the 11th slot in b starting from 0), thus can cause all sorts of problems. This is known as an Out Of Bounds (OOB) error.
You have to be especially careful of OBOEs when using do-while loops as they ALWAYS run at least once.
A switch statement is basically a structured if/else if/else system (similar to how for is a structured while). The easiest way to explain it is with an example:
This is functionally equivalent to:
However it is slightly clearer to see what is going on.
An important thing to note here is the different ways in which ifs and switches are processed:
That will call SomeFunction() ONCE and compare it's result 3 times.
That will call SomeFunction() three times, which is very inefficient, a switch is more like doing:
For those of you who know C the PAWN switch is slightly different, the individual conditions are NOT fall-through and are bound by braces so there's no need for break statements.
case statements (the "case X:" parts of the switch statement) can have other options besides a single number. You can compare a value to a list of numbers (replacing fall-through in C) or even a range of values:
This case will trigger if the symbol being tested is 1, 2, 3, or 4, it's the same as doing:
but far more concise. Numbers in lists do not have to be consecutive, in fact if they are it's better to do:
This case will do exactly the same as above but by checking a range instead of a list, it is the same as doing:
This is the equivalent to else in if statements, it does something if all the other case statements failed.
Single line statements
goto is essentially a jump, it goes to a label without question (i.e. there's no condition to need to be true). You can see an example above in the if-goto loop.
The use of gotos is widely discouraged due to their effect on program flow.
break breaks out of a loop, ending it prematurely:
This loop will go 6 times but code after the break will only be executed 5 times.
continue basically skips a loop iteration
That will give the output of:
The continue basically jumps to the closing brace of the loop, as hinted at above you have to be careful when using continue with some loops:
This looks very similar to the other example however this time the continue will skip the a++; line, so the loop will get stuck in an infinite loop as a will always be 1.
return stops a function and goes back to the point in code which called the function in the first place:
That code will give an output of:
Because the print("2"); line will never be reached.
You can also use return to return a value:
That code will give the same output as above, however note that an additional return has been added to the end of the function. The end of a function has a return implied at it, however this return has no value, you cannot return a value and not return a value from the same function so we must explicitly return a value.
The symbol you return can be a number, a variable or even another function (in which case the other function will be called, it will return a value (it MUST return a value if you use it as a return value) and that value will be returned from the first function.
You can also store return values for later use: