What this does
The if... statement is used to make decisions for your code's in Java.
About
I will be explaining about how the if... statement is one of the most used features of Java. It is basically the same in JavaScript to as it is in other programming languages. If you are new to programming in Java like me, you should focus your best time understanding how the if... statement works.
Im still in training of Java but the more I do the better I am so lets not get in the way of "This is easy", yes it is, but what about the new-comers eh?
You use the if... statement to make decisions for your program, think of it as if you were the father of your son(it). Your son won't know much about life so you have to show him the way. Make a wise DECISIONS for his future and that is where the DECISION role comes in.
First I will show you how the if... statement is used(this is called a syntax, the structure of your code):
PHP Code:
if (condition) {
statements
}
The if keyword identifies this as an if... statement. The condition in the parenthesis ( ) is evaluated to determine if true, and if so then the statements inside the curly braces { } are executed(started, operating), otherwise they are skipped and the programs continues with the first line after the if... statement.
An optional else... statement can be included with the if... statement as follows:
PHP Code:
if (condition) {
statements
}
else {
statements
}
in this case, the statements inside of curly brackets { } after the else... keyword are executed(started, operating) if the condition of the if statement is false.
Take a look at the conditions which shows the results expected from the if... statement for different conditions. In this example x and y are two variables that have been initialized to a value. If you've taken algebra courses or know some math about =, <, <=, >, & >= then you are ok when it comes to conditions.
if... statement Results
condition -=-=-=- returns true if
x == y -=-=-=- x and y are equal
x != y -=-=-=- x and y are not equal
x > y -=-=-=- x is greater than y
x >= y -=-=-=- x is greater or equal to y
x < y -=-=-=- x is less than y
x <= y -=-=-=- x is less than or equal to y
Now above was just an example of how to use them solely. But do you know what they actually do now? Lets take a look at it deeper:
== is equal to;
!= is NOT equal to;
> is greater than;
>= is greater OR equal to;
< is lesser than;
<= is lesser OR equal to;
I did NOT make this tutorial. It was my friend. I'm just passing it on.