Conditionals in Java

The syntax of a Java conditional statement is

    if  ( conditional statement     {
        statement to execute if conditional is true;
    }
    else    {

        statement to execute if conditional is false;
    }

The conditional operator for "equals" in Java is two equal signs == (one equal sign is the assignment operator), and there is no semicolon after the if line. 

In the example below, notice that there is no "else".  The else statement is optional, depending on your program's logic.

    if  ( age == 65 )      {
        System.out.println ( "Eligible to retire" );
     }

The other conditional operators in Java are:

    >     greater than
    <    less than
    <=    less than or equalt o
    >=    greater than or equal to
    !=    not equal to

 

Exercise 7:

Write an application named Exercise7 that declares an integer "age" and assigns a number (of your choice) to it.  Use a conditional to check whether the number is 21 or over, and if so, print out "eligible to vote".  Test it by assigning different numbers to age.

For the solution, click here.

 

More than one statement can be included inside the if or the else clauses:

    bool eligibleForOvertime;
    int jobCategory;
    string jobType;

    if  (  jobType == "hourly"  )      {
        jobCategory = 1;

        eligibleForOvertime = true;
    }
    else    {

        jobCategory=2;
        eligibleForOvertime = false;

    }

If there is only one statement after the if or else, you don't have to use { }

     if  (  jobType == "hourly"  ) 
        eligibleForOvertime = true;
    else 
        eligibleForOvertime = false;

However, it is generally recommended that you use { } in all cases.

 

You can combine two or more conditional statements with 

        &&    for logical AND

        ||          for logical OR    

To type the OR symbol, use the key that looks like two short vertical lines on top of each other   ¦
Usually this key is located just above the Enter key.

    if  ( age > 21 && registered = true )      {
        System.out.println("Eligible to vote");
    }

    if  ( age > 21 || registered = true )      {
        System.out.println("Eligible to vote");
    }
 

Exercise 8:

Write an application named Exercise8 that declares a string variable day and assigns the name of a day of the week to it.

Use a conditional statement to print out whether the day is a weekday or a weekend day.

Test it by assigning different values to the string.

For the solution, click here.

 

 

You can include several alternate conditionals in the same statement:

    if  ( year == 1 )      {
        class = "Freshman";
    }
    elseif (  year == 2 )    {

|        class = "Sophomore";
    }
    elseif (  year == 3 )    {

        class = "Junior";
    }
    else    {
        class = "Senior";
    }

 In this case, the last "else" is executed only if none of the other conditions is true, so you can also use it as a default statement for catching error conditions, like this:

    if  ( year == 1 )      {
        class = "Freshman";
    }
    elseif (  year == 2 )    {
|        class = "Sophomore";
    }
    elseif (  year == 3 )    {
        class = "Junior";
    }
    elseif (  year == 4 )    {
        class = "Senior";
    }
    else    {
        System.out.println("Error in data -- value must be between 1 and 4");
    }

 

Exercise 9:

Write an application named Exercise9 that declares an integer variable grade and assigns it a value between 1 and 12.  If the value is below 7, print out “Elementary”; then if it is below 9, print out “Junior High”, and if not, print out “High School”.

Test it by assigning different values to the variable.

For the solution, click here.

 

The "freshman ... senior" program shown above can also be written in a somewhat shorter form, by using the switch statement:

    int year = 3; 
    switch ( year ) { 
        case 1: 
        System.out.println("Freshman"); 
        break; 
    case 2: 
        System.out.println("Sophomore"); 
        break; 
    case 3: 

        System.out.println("Junior"); 
        break; 
    case 4: 

        System.out.println("Senior"); 
        break; 
    default: 

        System.out.println("Error"); 
        break; 

Exercise 10:

Write an application named Exercise10 that shows the user his/her Chinese zodiac sign.  To do this, take the user's year of birth mod 12.  (year_of_birth % 12).  Interpret the results like this:

    0    Monkey
    1    Rooster
    2    Dog
    3    Pig
    4    Rat
    5    Ox
    6    Tiger
    7    Rabbit
    8    Dragon
    9    Snake
    10  Horse
    11  Sheep

Output should look like this:

    You were born in 1962, the year of the Tiger.

For the solution, click here.

 

For the next syntax lesson, click here.