The Java language has an if statement which allows your programs to make decisions about what code to execute. Here is a very simple example:
boolean isValid = true;

if ( isValid ) {
    System.out.println("it is valid");
} else {
    System.out.println("it is not valid");
}
The if statement in this example tests the boolean variable isValid and based on its value (either true or false) it executes one of two different blocks of code. If the isValid variable has the value of true, the first block is executed. If not, the code inside the else block is executed.
The expression inside the parantheses is called the condition. The condition can be any Java expression, as long as the result of the expression is a boolean result (either true or false).
In the example above, the condition was whether the isValid variable was true or false.
If the block of code to be executed is just a single statement, you do not need the brackets { }around them, in the if statements. Here is an example:
if ( isValid )   System.out.println("it is valid");
else             System.out.println("it is not valid");

Conditional Operators

Java has a set of conditional operators you can use, which result in a value of either true orfalse. These are:
  • ==
  • !=
  • <
  • <=
  • >
  • >=
The == operator tests if two values are equal to each other. For instance:
long var1 = 2;
long var2 = 5;

if(var1 == var2) {
   //...
}
If the two variables, var1 and var2, are equal, the expression var1 == var2 is evaluated to true. Otherwise the expression is evaluated to false.
The != operator does the exact opposite of the == operator. If the two variables are not equal, the expression is evaluated to true. If the two variables are equal, the expression is evaluated to false.
The < operator is evaluated to true, if the variable on the left side of the operator is less than the variable on the right side of the operator. If the left variable is equal to, or larger, the expression is evaluated to false. Here is an example expression:
if(var1 < var2) {
   //...
}
The <= operator works like the < operator, except it also evaluates to true if the two variables are equal to each other. False otherwise.
The > operator works the exact opposite way of the < operator. The operator expression is evaluated to true if the variable on the left side of the operator is greater than the variable on the right side of the operator. False if not. Here is a simple example:
if(var1 > var2) {
   //...
}
The >= operator works like the > operator except it also evaluates to true if the two variables are equal to each other.

Comparing Variables and Constants

In the examples earlier in this text I have only shown comparisons of either constants to constants, or variables to variables. But, you can also compare constants to variables. Here are two examples:
int var1 = 50;

if(var1 > 10) {
   //...
}

if(99 <= var1) {
   //...
}

Methods as Conditions

You can also use the return value of a method as condition in an if statement. Here is how:
public void methodOne (String input) {

    if ( isValid(input) ) {
        System.out.println(input + " is valid");
    } else {
        System.out.println(input + " is not valid");
    }

}

public boolean isValid(String value) {
    if( value.equals("123") ) {
        return true;
    }
    return false;
}
This example actually contains two if statements with methods as conditions. First the
 if( isValid(input) )
which tests the output of the isValid(input) method, for a true or false result.
Second, inside the isValid() method the String.equals() method is used to test for equality to a certain string value. This is the if statement that tests it:
if( value.equals("123") ) {

Chaining if Statements

It is possible to chain if statements, to create a decision tree. Here is an example:
if( name.equals("john")) {
    //...
} else if ( name.equals("jane")) {
    //...
} else if ( name.equals("Linda")) {

} else {

}
In the example above, else if statements are chained, one after another. Actually, this chainedif statement is just an if statement executed in an else block, without the brackets { }, as I showed you that you can, in the beginning of this text.

Powered by Blogger.

- Copyright © 2013 Taqi Shah Blogspot -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -