Thursday, October 20, 2011

If Statement

1. The if statement syntax

      The if statement is a conditional branch statement. The syntax of the
if statement is either one of these two:
if (booleanExpression) {
    statement (s)
}
or
if (booleanExpression) {
    statement (s)
else {
    statement (s)
}
For example, in the following if statement, the if block will be executed if x is greater than 4.
public class MainClass {

  public static void main(String[] args) {
    int x = 9;
    if (x > 4) {
      // statements
    }
  }

}
In the following example, the if block will be executed if a is greater than 3.
Otherwise, the else block will be executed.
public class MainClass {

  public static void main(String[] args) {
    int a = 3;
    if (a > 3) {
      // statements
    else {
      // statements
    }
  }

}
2. Expression indentation for if statement

If the expression is too long, you can use two units of indentation for subsequent lines.
public class MainClass {

  public static void main(String[] args) {
    int numberOfLoginAttempts = 10;
    int numberOfMinimumLoginAttempts = 12;
    int numberOfMaximumLoginAttempts = 13;
    int y = 3;

    if (numberOfLoginAttempts < numberOfMaximumLoginAttempts
        || numberOfMinimumLoginAttempts > y) {
      y++;
    }
  }

}
3. Using braces makes your 'if' statement clearer

If there is only one statement in an if or else block, the braces are optional.
public class MainClass {

  public static void main(String[] args) {
    int a = 3;
    if (a > 3)
      a++;
    else
      a = 3;
  }

}
Consider the following example:
public class MainClass {

  public static void main(String[] args) {
    int a = 3, b = 1;

    if (a > || b < 5)
      if (a > 2)
        System.out.println("a > 2");
      else
        System.out.println("a < 2");
  }

}
  1. It is hard to tell which if statement the else statement is associated with.
  2. Actually, An else statement is always associated with the immediately preceding if.
Using braces makes your code clearer.
public class MainClass {

  public static void main(String[] args) {
    int a = 3, b = 1;

    if (a > || b < 5) {
      if (a > 2) {
        System.out.println("a > 2");
      else {
        System.out.println("a < 2");
      }
    }
  }

}
4. Multiple selections
If there are multiple selections, you can also use if with a series of else statements.
if (booleanExpression1) {
    // statements
else if (booleanExpression2) {
    // statements
}
...
else {
    // statements
}
For example:
public class MainClass {

  public static void main(String[] args) {
    int a = 0;

    if (a == 1) {
      System.out.println("one");
    else if (a == 2) {
      System.out.println("two");
    else if (a == 3) {
      System.out.println("three");
    else {
      System.out.println("invalid");
    }
  }

}
5. The if Statement in action


public class MainClass {

  public static void main(String[] arg) {
    int a = 0;

    if (a == 0) {
      System.out.println("a is 0");
    }
  }
6. The else Clause

public class MainClass {

  public static void main(String[] arg) {
    int a = 0;
    if (a == 0) {
      System.out.println("in the block");
      System.out.println("in the block");
    else {
      System.out.println("a is not 0");
    }
  }
}

compile results
in the block
in the block
7.Nested if Statements
public class MainClass {

  public static void main(String[] arg) {
    int a = 2;
    if (a == 0) {
      System.out.println("in the block");
      if (a == 2) {
        System.out.println("a is 0");
      else {
        System.out.println("a is not 2");
      }

    else {
      System.out.println("a is not 0");
    }
  }
}
compile results
a is not 0
8.Using && in if statement

public class MainClass {

  public static void main(String[] arg) {
    int value = 8;
    int count = 10;
    int limit = 11;

    if (++value % == && ++count < limit) {
      System.out.println("here");
      System.out.println(value);
      System.out.println(count);
    }
    System.out.println("there");
    System.out.println(value);
    System.out.println(count);
  }
}


compile results
there
9
10
 
9.Using || (or operator) in if statement 
 
public class MainClass {

  public static void main(String[] arg) {
    int value = 8;
    int count = 10;
    int limit = 11;

    if (++value % != || ++count < limit) {
      System.out.println("here");
      System.out.println(value);
      System.out.println(count);
    }
    System.out.println("there");
    System.out.println(value);
    System.out.println(count);
  }
}


compile results
here 
10 

No comments:

Post a Comment