next up previous contents
Next: Logical operators Up: 2 Input, mathematical functions Previous: The if statement and   Contents

Else and else if

There is also the option of adding else after an if -- the else section should contain parts to be executed if the condition is not true. A trivial example of this is given below, where a value for boo is displayed on the screen.

    boolean boo;

    // some code goes here which gives boo the value true or false

    if ( boo )
    {
        System.out.println("if: boo is true");
    }
    else
    {
        System.out.println("else: boo is false");
    }

    // print value of boo to the screen directly
    System.out.println("boo is " + boo);

There is also an optional else if that can be added. This works like an else but you get to specify another test condition as well. Here is an example:

/** 
 * Purpose: An example to demonstrate the use of `if'
 */
import java.io.*;
public class IfExample2
{
    public static void main (String[] args) throws IOException
    {
        BufferedReader br = new BufferedReader( 
                             new InputStreamReader (System.in));
        System.out.print ("Enter a number between 0 and 10 inclusive: ");
        String temp = br.readLine();
        double x = Double.parseDouble(temp);
        // check user input
        if (x > 10) 
        {
            System.out.println("The number you entered is too high");
        }
        else if (x < 0)
        {
            System.out.println("The number you entered is too low");
        }
        else 
        {
            System.out.println("The number you entered is " + x);
        }
    }
}

This program checks whether input is in the range 0-10. If the input is larger than 10 the first condition is true and the if block of code is executed. If x is less than 10 the second test is performed -- x<0. If the second condition is true the elseif block of code is executed. Otherwise, if both conditions were false the final else block of code is executed.

You may use as many else if's as you like after an if, allowing tests of many different conditions such as x<0. If an else is used it always comes at the end, being executed only if all the conditions preceding it are found to be false.

Exercise 2.2


(10 marks)



Create a program to solve quadratic equations making use of the well-known formula for solving quadratics. Assuming the form of the quadratic is ax2 + bx + c, read in the three numbers a, b, c from the keyboard. If the equation has complex roots, your program should output a message to the screen to this effect and not try to calculate them, otherwise the one or two roots of the equation should be printed to the screen. Include in your lab book a printout of your program and sample output to show that it works when tested for complex roots, a single root, and two roots, by using it to solve the following equations:

(i)
x2 - 4x + 4
(ii)
x2 - 4x + 8
(iii)
2x2 + 13x + 21




next up previous contents
Next: Logical operators Up: 2 Input, mathematical functions Previous: The if statement and   Contents
RHUL Dept. of Physics