MathBits.com
Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use

The "if ... else" Statements

Let's look at situations when the word "otherwise" can be used in your decision making!

if (test condition)  
{
      block of one;
      or more C++ statements;
}
else
{
     block of one;
     or more C++ statements;
}

If the test condition is TRUE, the block of statements following the "if" executes.  If the test condition FALSE, however, the block of statements following the "else" executes instead.  

     Example:

// example of if ... else
String city = Console.readLine("Enter city name:");
int population = Console.readInt("Enter population");

if (population >= 543876)
{
     System.out.println("According to the census, " +
                         city + " is one of \n the 100 largest cities.");
}
else
{
    System.out.println("According to the census, " +
                  city + " is not one of \n the 100 largest cities.");
}

                                                                              


Return to Unit Menu | Java Main Page | MathBits.com | Terms of Use