Reviews

If-Then and If-Then-Else Conditional Statements in Java

If-Then and If-Then-Else Conditional Statements in Java


We are searching data for your request:

Forums and discussions:
Manuals and reference books:
Data from registers:
Wait the end of the search in all databases.
Upon completion, a link will appear to access the found materials.

The

if-thenand

if-then-elseconditional statements let a Java program make simple decisions about what to do next. They work in the same logical way as we do when making decisions in real life.

For example, when making a plan with a friend, you could say "If Mike gets home before 5:00 PM, then we'll go out for an early dinner." When 5:00 PM arrives, the condition (i.e., Mike is home), which determines whether everyone goes out for an early dinner, will either be true or false. It works exactly the same in Java.

The if-then Statement

Let's say part of a program we're writing needs to calculate if the purchaser of a ticket is eligible for a child's discount. Anyone under the age of 16 gets a 10% discount on the ticket price.

We can let our program make this decision by using an

if-thenstatement:

if (age < 16)
isChild = true;

In our program, an integer variable called

ageholds the age of the ticket purchaser. The condition (i.e., is the ticket purchaser under 16) is placed inside the brackets. If this condition is true, then the statement beneath the if statement is executed -- in this case a

booleanvariable

isChildis set to

true.

The syntax follows the same pattern every time. The

ifkeyword followed by a condition in brackets, with the statement to execute underneath:

if (condition is true)
execute this statement

The key thing to remember is the condition must equate to a

booleanvalue (i.e., true or false).

Often, a Java program needs to execute more than one statement if a condition is true. This is achieved by using a block (i.e., enclosing the statements in curly brackets):

if (age < 16)​
{
isChild = true;
discount = 10;
}

This form of the

if-thenstatement is the most commonly used, and it's recommended to use curly brackets even when there is only one statement to execute. It improves the readability of the code and leads to fewer programming mistakes. Without the curly brackets, it's easy to overlook the effect of the decision being made or to come back later and add another statement to execute but forget to also add the curly brackets.

The if-then-else Statement

The

if-thenstatement can be extended to have statements that are executed when the condition is false. The

if-then-elsestatement executes the first set of statements if the condition is true, otherwise, the second set of statements are executed:

if (condition)
{
execute statement(s) if condition is true
}
else
{
execute statement(s) if condition is false
}

In the ticket program, let's say we need to make sure the discount is equal to 0 if the ticket purchaser is not a child:

if (age < 16)
{
isChild = true;
discount = 10;
}
else
{
discount = 0;
}

The

if-then-elsestatement also allows the nesting of

if-thenstatements. This allows decisions to follow a path of conditions. For example, the ticket program might have several discounts. We might first test to see if the ticket purchaser is a child, then if they're a pensioner, then if they're a student and so on:

if (age < 16)
{
isChild = true;
discount = 10;
}
else if (age > 65)
{
isPensioner = true; discount = 15;
}
else if (isStudent == true)
{
discount = 5;
}

As you can see, the

if-then-elsestatement pattern just repeats itself. If at any time the condition is

true , then the relevant statements are executed and any conditions beneath are not tested to see whether they are

trueor

false.

For example, if the age of the ticket purchaser is 67, then the highlighted statements are executed and the

(isStudent == true)condition is never tested and the program just continues on.

There is something worth noting about the

(isStudent == true)condition. The condition is written to make it clear that we're testing whether

isStudenthas a value of true, but because it is a

booleanvariable, we can actually write:


else if (isStudent)
{
discount = 5;
}

If this is confusing, the way to think about it is like this -- we know a condition is tested to be true or false. For integer variables like

age, we have to write an expression that can be evaluated to true or false (e.g.,

age == 12,

age > 35, etc… ).

However, boolean variables already evaluate to be true or false. We don't need to write an expression to prove it because

if (isStudent)is already saying "if isStudent is true… ". If you want to test that a boolean variable is false, just use the unary operator

!. It inverts a boolean value, therefore

if (!isStudent)is essentially saying "if isStudent is false."



Comments:

  1. Bell

    the admirable answer :)

  2. Bagami

    Response intelligibility

  3. Tegis

    It is not meaningful.

  4. Oliver

    Certainly. So happens.

  5. Laudegrance

    It's simply incomparable topic



Write a message

Video, Sitemap-Video, Sitemap-Videos