Java Tutorial: Nested If Statements

Key Points:

  • Nested If Statement: if statement nested within a if statement.
  • Basic Syntax:
    if (Boolean_expression 1){
    //if Boolean is true expressions to execute go here

    }
    if (Boolean_expression2){
    //if Boolean is true expressions to execute go here
    }
  • You can also have if else statements within this.
  • It is important to be careful with the placement of your brackets! 
    • NetBeans and other IDE’s often will highlight matching brackets as you select them to guide you.
    • Misplacing one can lead to logic errors!
  • Above is a flow chart for a visual representation.
  • Also you can use logical operators such as:
    • And &&
      • Both statements must be true.
        if(Boolean1_Expression && Boolean2_Expression){
        //do
        }
    • Or ||
      • Only one statements must be true.
        if(Boolean1_Expression || Boolean2_Expression){
        //do
        }

View the Code!