Welcome to my Conditional Statement tutorial
As our java programs get more sophisticated, we will need to use the conditional statements that allow our program to make decisions. The if-statement is a way to make decisions based on conditional expressions. If java does not allow conditional statements, then our programs will run every line of code, which we do not always want; also, the program might not run the way we expected.
Example:
We draw a canvas on the screen with a fixed width, then we draw an ant in the center of this canvas. We want the ant to move based on the command we send to the program. For example, we use the four arrow keys to send commands. When the ant is moving, we need to check if the ant passed the edge of the canvas, then we will reposition the ant. If we do not use conditional statments, we will not see the ant if it pass the edge of the canvas, because it is out of range.
The condition expressions can be anything expression that returns the value
true or
false.
Following are the list of possible conditional expressions:
-
A method that return the value true or false
- Two values, variable, or the method in list 1, that is, compared with the boolean operators, such as == (equal), != (not equal), <= (lest than or equal), >= (greater than or equal), < (less than), > (greater than)
- Two conditional expressions in list 1,2 and 4 (will be listed), that is, can put together with logical operators between them, the logical operators are && (and), || (or)
- The conditional expressions can add the ! (means not) in front of it, it means the inverse of the result that the expression produced. For example, !(true) will result false.
- The conditional expression in the list above, they can put inside a paranthesis, which means its execution priority is higher.
The syntax of if-statement is
if(conditional expression)
{
//code that you want the program to do
//if conditional expressions evaluated to be true
}
The syntax of if-else statement is
if(conditional expression)
{
//code that you want the program to execute
//if the conditional expression evaluated to be true
}
else
{
//code that you want the program to execute
//if the conditional expression evaluated to be false
}
//NOTE : you can only put 1 if and 1 else in a combination
The syntax of if-else-if statement is
if(conditional expression 1)
{
//code that you want the program to execute
//if the conditional expression 1 evaluated to be true
//the program will ignore all the codes until
//the last else-if statement or else statement
}
else if(conditional expression 2)
{
//code that you want the program to execute
//if the conditional expression 1 evaluated to be false,
//and conditional expression 2 evaluated to be true
//the program will ignore all the codes until
//the last else-if statement or else statement
}
//.... you can add as many else-if statement as you like,
//put the code inside { and } behind
//else if (conditional expression N)
//if conditional expression before N evaluated to be false,
//and conditional N evaluated to be true
//the program will ignore all the codes until
//the last else-if statement or else statement
else
{
//code that you want the program to execute
//if all conditional expressions above evaluated to be false
}
//NOTE : you can only put 1 if and 1 else in a combination
Thank you for visiting my tutorial.
I wish you learned from here.