If Statements after very important in coding. They let you check if a certain condition is met, and then do something. Or if that something is not met, do another something. OR if that something is not met but this something is do this something....
If Statements are pretty easy to get the hang of, and are very powerful. So let's hop in:
CREATING AN IF STATEMENT
In order to create an if statement, you first have to have something that you will be checking for if it's true or not. Remember earlier when I talked about booleans? basically we'll need some sort of variable that can store true or false (or 1 or 0).
so let's set a simple variable to a boolean:
and make an if statement checking if that is indeed true:
notice:
the word if
the ()
where you put what you're checking is true (inside the ())
there is no;
the {}
so if the variable im_a_good_student is in fact equal to true, the code inside the {} of the if statement will be run, and this case print that string to the screen. If it is not true, the if statement will simply be skipped over.
It's important to note you don't always have to directly set and check a variable to be true. you can actually call functions inside an if statement, provided they will return a boolean.
A lot of different Engine functions (and I mean a lot) return booleans. for example pretty much anything function that starts with the word "is" returns a boolean, such as: IsMeleeing(), IsPlayer(), IsWeapon(), etc.
so instead of doing it the long way like this:
we can take the shortcut:
ELSE
Sometimes if a certain condition in our if statement is not met, we don't just want to skip over the code, but do something else entirely...
lucky for you that can be accomplished pretty easily:
notice:
the word else
there is no condition I'm checking for is true or not inside the else statement. This is because the code in the else statement is only activated it the if statement above is not true
the {}
you must have an if statement before you have an else.
ELSE IF
So far these all make pretty logical sense if we look at them English-wise. if something, do something. else do this other thing. now let's keep that going with the hybrid baby of if and else, the else if().
just like the else, there must be an if statement before you can have an else if. the else if is similar to the if statement, as it checks if a condition is met, and will be skipped over if it isn't.
in this case first it will check if im_a_good_student is true. if it is, it will execute the code inside the first if statement, and nothing else. if im_a_good_student is not true, it will then move to the else if statement and check if math_is_hard is true. If it is, then it will execute the code inside the else if statement, and nothing else. finally if neither the if statment or any else if's are met, than the else code will activate.
note that you can have as many else if's in a chain as you want, just remember it needs to start with an if statement:
AND'S/OR'S
We can also check for more than one condition to be met inside an if statement before the if code is executed.
in GSC language:
and = && or = ||
note that for or those are not lowercase L's or capital i's, but the vertical line which you can make by holding shift and pressing the "\" button on your keyboard.
so as you might expect, having an and means all conditions must be met, and having an or means just one of the conditions has to be met:
in the above case, the first code with the and in the if statement will only get activated once a year. for the bottom case with the or in it, it would get run on the 29th of every month, and on every day in the month of may, because just one of those conditions has to be met.
it should be noted you can chain more than one and or or in the same if statement: