Java. The if-else-if statement




The if-else-if statement


Contents


Search other websites:

1. What is the general form of the if-else-if construct?

The general form of the if-else-if statement is:

if (condition1)
    statement1;
else
if (condition2)
    statement2;
else
...
if (conditionN)
    statementN;
else
    statement;

where

  • condition1, condition2, …, conditionN – conditional expression containing the relation operations;
  • statement, statement1, statement2, statementN – one or more operators.

The if-else-if statement works as follows. If condition1 is true, then the statement1 is executed. If condition1 is not satisfied, then a transition to condition2 occurs. In the same way, if condition2 is true, then the statement2 is executed. Otherwise, the following condition is executed. If noone condition is not true during the test, then the last statement is executed.

If after execution of the condition, two or more operators must be executed, they are framed by the block { }.



2. Which control operator is replaced by the if-else-if construct?

The if-else-if construct can replace the switch statement.

For example, a code snippet that defines a day off or a working day can be implemented using a switch statement and the construct if-else-if.

Solution using the switch statement:

int day;
boolean f_DayOff;

day = 7;
f_DayOff = false;

// implementation with the switch statement
switch (day)
{
    case 1: f_DayOff = false; break;
    case 2: f_DayOff = false; break;
    case 3: f_DayOff = false; break;
    case 4: f_DayOff = false; break;
    case 5: f_DayOff = false; break;
    case 6: f_DayOff = true; break;
    case 7: f_DayOff = true; break;
}

Solving the problem using the if-else-if statement

int day;
boolean f_DayOff;

// ...
f_DayOff = false;

// the if-else-if statement
if (day==1) f_DayOff = false;
else
if (day==2) f_DayOff = false;
else
if (day==3) f_DayOff = false;
else
if (day==4) f_DayOff = false;
else
if (day==5) f_DayOff = false;
else
if (day==6) f_DayOff = true;
else
if (day==7) f_DayOff = true;

3. Examples of using the if-else-if statement

Example 1. Develop a program for displaying the name of the month, if it is given by an integer from 1 to 12. In the program, use the if-else-if statement.

A fragment of code that solves this problem:

int month; // the month number
String MONTH = ""; // name of the month

// inputting the month
// ...

if (month==1) MONTH = "January";
else
if (month==2) MONTH = "February";
else
if (month==3) MONTH = "March";
else
if (month==4) MONTH = "April";
else
if (month==5) MONTH = "May";
else
if (month==6) MONTH = "June";
else
if (month==7) MONTH = "July";
else
if (month==8) MONTH = "August";
else
if (month==9) MONTH = "September";
else
if (month==10) MONTH = "October";
else
if (month==11) MONTH = "November";
else
if (month==12) MONTH = "December";

System.out.println(MONTH);

Example 2. Develop a program to determine the number of days in a month, if it is specified by the number from 1 to 12.

The code snippet that solves this problem

int month;
int n_days;

// inputting a month
// ...

if ((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10))
    n_days = 31;
else
if (month==2)
    n_days = 28;
else
    n_days = 30;

Example 3. Given an integer n (1 <= n <= 4) and a real number x. For a given value of the variable n, which is the function number, determine:

  • 1) cos x;
  • 2) tg x;
  • 3) sin x;
  • 4) ctg x.

A fragment of code that solves this problem:

int n;
double f,x;

// inputting x, n
// ...

if (n==1) f = Math.cos(x);
else
if (n==2) f = Math.tan(x);
else
if (n==3) f = Math.sin(x);
else
    f = Math.cos(x)/Math.sin(x);


Related topics