Ok then.
There are 3 types of decision constructs. If then else, if else if, and the switch (select if you're using VB). All 3 of these will get you the same results, it's just different ways of doing it.
If then else:
if (expression)
{
statement;
}
Lets examine this: If the expression is true...we do the statement. This can also be written as:
if (expression == true)
{
statement;
}
Notice the second equals. This is the comparison operator and it is imperative that you understand this for languages outside of GM and VB. This operator will return either a value of true or false, depending on the two values being compared.
You can also say expression == false, or !expression.
Extending this further, we can add an else:
if (expression)
{
statement;
}
else
{
statement;
}
What this will do is if the expression is evaluated to false, it will skip the first statement and move to the second. Logically, this is saying, if this is true do this...otherwise, do that.
If else if
The if else if takes the previous statement further and does this:
if (expression1)
{
statement;
}
else if (expression2)
{
statement;
}
else
{
statement;
}
This allows you to check multiple expressions in one construct. Similar to how the if then else went to the else when the expression was false, this will follow through each expression until it finds one that is true, and then executes the related statements. The else at the bottom is optional, and if it's there it will run if none of the other expressions are true.
Switch/Select
This one is a little different. It has some silly functionality depending on the language. First, you can only check one variable against your expressions. Second, depending on your language, you cannot use >, <, >=, or <= in your expressions. This limitation is present in C/C++, Java and C#. The switch is structured as follows:
switch (variable)
{
case x:
statement;
break;
case y:
statement;
break;
default:
statement;
break;
}
Notice the cases. Those are your expressions. This will take the variable that you have specified and check it against each case. (i.e. if variable equals x, then case x will execute). When it finds Should a case not be found that matches, the default case will run, similar to the else expression.
Please note that the Switch and else if are the same thing in different forms with slightly different functionality. Sometimes you will be required to use one over the other, however (such as the case where you need to use a >= for example).
Keep in mind in the switch and else if, when a true expression is found, the rest of the expression following are skipped.
Now, onto boolean logic...
We have 2 binary operators. And, or, represented as && and || respectively. There is also ! which represents not.
In the case of an And statement, both expressions MUST be true for the expression to evaluate true.
if (5 == 5 && 6 == 2)
This is false, because 6 != 2.
if ( 5 == 5 && 5 == 5 )
This however, is true.
As for the or, only one expression has to be true.
if ( 5 == 5 || 5 == 2)
this is true.
if ( 5 == 2 || 5 == 5)
This is also true.
if ( 5 == 2 || 6 == 3)
This is not true.
Also keep in mind if the first expression in an or is true, then the program will skip evaluating the second expression (there's no point, you already know it's true).
Now, lets throw the not in there...
if ( 5 != 2 && 6 != 2)
This is true, because 5 is not equal to 2 and 6 is not equal to 2, which we specified with the !.
I need to run out now, if anyone feels they need to continue this, please feel free to do so. I'll leave some practice logic here for you guys...
Evaluate each:
(true || false)
((false || true) && true)
((false & false) || (true && true))
((true || false) && (false && false)) || (true && false)
((5 >= 3) || (3 == 2)) && (2 <= 7 && 4 != 5)