Conditionals
Home Up Boolean Operators Classes Conditionals Iteration Methods

 

There are two conditional statements in Java, the if and the switch.  The if statement is the general conditional statement, the switch is more specialized.

If statement   (do you want to learn more about boolean operators?)

  1. If-Then Construct

         if (condition)      
             statement;
  2. If-Then-Else Construct

         if (condition)
             statement;
         else
             statement;
  3. Nested If Construct

        if (condition)
            statement;
        else if (condition)
            statement;
        else if (condition)
             statement;
         ....... etc. .......
         else
             statement;

Switch statement    ("testVariable" must be of a data type that has discrete values, e.g. "byte", "short", "int", "long", "char", or "boolean")

switch (testVariable) 
{     
   case valueOne:                      
          statement(s);
          break;
  case valueTwo:
          statement(s);
          break;
  ....... etc. .......
  default:
           statement(s);
}