so a if statement is a check for a boolean; this is a important thing to remember because it can not only make the ifstatment easier to writer; but also make other things easier to understand.
A "if else" statement is normally written like "else if". This is the combination of a if statement and a else statement. It runs if the statement above did not trigger, then checks if it's if statement conditional is true or their are no other else statements below it.
else statement, triggers if none of the above if statements triggered.
quick example of if, else if, and else
- Code: Select All Code
if (number == 5 || number > 3 && number <4.577584833 ) {...} //if number is 5, or above 3 and below 4.577.... the run this
else if (number == 4.577584833+ 0.3){...} //if number is equal to 4.877584833, run this
else if (A != B && (A&&!B)){...} // the A is not equal to be, and B is false along with A being true. It's confusing has this is set up but trust, A must be true and B must be false to run.
else if ( someRandomFunction() ){...}
else if ( 0 != 0){...}// this will never run but if it were, 0 must not equal to 0.
else{...}//None of the above statements true.
//bad design
//These are something you should not do.
if(myBoolean){...} // okay this will run if myBoolean is true.
if(!myBoolean){...}//this is the problem here. You are going to check both if statements are true, but [u]only 1[/u] can be true. What you should be doing for the second if statement is using "else if()" or a else statements. Both would work.
Now going back to what i said about if statement checks for a boolean. A boolean is not just a variable type but a result of from a logical condition. "A && B || C && D" assuming that all those values are booleans, will return true if A and B are true or C and D are true. That's because we're checking if the logical operation are true. This is why you can do "if(A){...}" and don't have to do "if(A==true){}".
Switch statement are more efficient if their are 4 or more conditional checks. However switch statement are limited by the fact you can't do anything except check if something is equal to something else.
There is also a ternary check which is "Conditional ? result 1: result 2". Basically you give your conditional check; add a question mark after it; if the conditional is true, then result 1 happens/returns; however if conditional is false, then result 2 is used/return.
example
- Code: Select All Code
A = true;
number = !A == false? 1 : -1; // if NOT A is equal to false, number is equal to 1; else is equal to -1;
number = Math.Max(number, number>0? 20:-15); //if number is greater than 0, then Math.Max(number,20); else Math.Max(number,-15);
Now the limitation of the ternary is that your result, is only 1 statement, unlike a if else statement where you have as many as you want. You also might have to return a value depending where it is. (I do believe in some languages you have to return a value.)
The advantage of ternary is that you can do this nearly whenever you want as shown in my example code with Math.max().
I hope you liked this brief look at conditionals. I could of covered switch states more in depth because their so unique but i decided not to.