Making Decision

 

So far, we have only seen programs run straight from the start to finish. Hence, that is not always the case. As a program
gets more and more complex, often time we would have to deal with decision making. Without any further notice, let me introduce
two very important decision making tool that comes with java. The two tools are the
if statement and switch statement. The two
statements rely on the use of boolean expressions, which is true or false. The boolean expression can be very complicated, however we use simple expressions that compare the value of a variable with the value of some other variable. This comparison uses one of the relational operators. All these operators are binary operators which work on two operands.

Table Relational Operators
Operators Descriptions
= = Returns true if the expression on the left evaluates to the same value as the expression on the right
!= Returns true if the expression on the left does not evaluate to the same value as the expression on the right
< Returns true if the expression on the left evaluates to a value that is less than the value of the expression on the right
<= Returns true if the expression on the left evaluates to a value that is less than or equal to the expression on the right
> Returns true if the expression on the left evaluates to a value that is greater than the value of the expression on the right
>= Returns true if the expression on the left evaluates to a value that is greater than or equal to the expression on the right

Example

Assume we have the following statements initialized

int i = 5;
int j = 10;
int k = 6;
 

now, let say we have the following:

Expression            Value            Explanation

i == 5                    true              The value of i is 5

i == 10                  false              The value of i is not 10.

i > 3                      true               i is 5, which is greater than 3

k > i + j                 false              k is 6, i + j is 15

Note: the relational operator that test for equality is two equal signs in a row ( == ). A single equal sign is the
         assignment operator.

    if( i == 5) is not the same as if ( i = 5)

Simple if statements

In most cases, an if statement lets us execute a single statement or a block of statements only if a boolean expression
evaluates to be true. The if statement has a basic form looks like this

if ( boolean - expression )
    statement...

Example

int x = 0;

if ( y > 10 )
    x = 4;

In the example above, we have x initialized to 0, then we will assign 4 to x only if the expression y > 10 is true.

Simple if-else statements

An if-else statement adds an additional element to a basic if statement. The else block execute if the boolean expression
is not true. It's the basic following format.

if ( boolean-expression)
    statement

else
    statement

Example

int commission;

if( sales >= 1000)
    commission = 0.03;

else
    commission = 0.05;

In the example above, the commission is set to 3% if sales is greater than or equal to 1000. If the sales is less than 1000,
the commission is set to 5%.

We can have a nested if-else statement by using the else-if statement. Basically, it's a series of if-else statements
with another if-else statements in each else part.

Example

int y = 0;

if ( x == 10)
    y = 1;

else if ( x > 10)
    y=3;

else if( x < 3)
    y = 5;

else
    y = 4;

Logic Operator

The two most often logic operator are && and ||. The && stands for conditional And, and the || stands for conditional Or.
We will see some examples on how to use both of the logic operator later.

Comparing Strings

When comparing instead of using the relational operator ==, we use " equals "

Lets see some programming examples

/* This program asks the user to enter a score
Then it will determine the user's grade

*
*/

import java.util.Scanner;

public class Grade
{
    public static void main(String[] args)
    {
        Scanner key = new Scanner(System.in);

        double grade;

        System.out.print("Enter your grade ");
        grade = key.nextDouble();

        //Here we will use the else-if statement

        if(grade >= 90 )
        {
            System.out.print("You got an A");
        }
        else if(grade >= 80)
        {
            System.out.print("You got a B");
        }
        else if(grade >= 70)
        {
            System.out.print("You got a C");
        }
        else if(grade >= 60)
        {
            System.out.print("You got a D");
        }
        else
        {
            System.out.print("You got a F");
        }
    }
}
 

ClickHere to download Grade.java

 

/* The program demonstrate the usage of the switch statement
It asks the user for a number then outputs a grade

*
*/

import java.util.Scanner; // Needed for the scanner class

public class ResGrade
{
    public static void main(String[] args)
    {
        int grade;

        Scanner key = new Scanner(System.in);

        System.out.print("Please rate from 1 to 5\n");
        System.out.print("1 is Very Good and 5 is Very Bad\n");

        System.out.print("Enter the number: ");
        grade = key.nextInt();

        /* Here we have 5 cases and a default, because grade is an integer
        And the inputs are between 1 and 5, we have to name the cases from 1 to 5
        The default one is when the user enters other numbers that is not between 1 to 5
        Note, after every statement we have to put a break, to break out the case.


        *
        */
        switch(grade)
        {
            case 1:
            System.out.println("You gave the restaurant an A");
            break;

            case 2:
            System.out.println("You gave the restaurant a B");
            break;

            case 3:
            System.out.println("You gave the restaurant a C");
            break;

            case 4:
            System.out.println("You gave the restaurant a D");
            break;

            case 5:
            System.out.println("You gave the restaurant a F");
            break;

            default:
            System.out.println("You enter a Invalid input");
            break;
        }
    }   
}

 

ClickHere to download ResGrade.java

 

 <Java 24><Home>