Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java Conditional Statements – if...else

Java Conditional Statements - if...else

Conditional statements in Java allow a program to execute different code blocks based on different conditions.

An if statement contains a boolean expression and one or more statements.

Syntax

The syntax of the if statement is as follows:

if

(

boolean expression

)

{

//

statement(s) to be executed if the boolean expression is true

}

If the value of the boolean expression is true, the code block in the if statement is executed; otherwise, the code after the else block is executed.

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

args

[

]

)

{

int

x

=

10

;

if

(

x

<
20
)
{
System

.

out

.

print

(

"

This is the if statement

"

)

;

}

}

}

The compilation and execution results of the above code are as follows:


这是 if 语句

if...else Statement

An if statement can be followed by an else statement; when the boolean expression of the if statement is false, the else block is executed.

Syntax

The usage of if...else is as follows:

if

(

boolean expression

)

{

//

if the value of the boolean expression is true

}

else

{

//

if the value of the boolean expression is false

}

Example

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

args

[

]

)

{

int

x

=

30

;

if

(

x

<
20
)
{
System

.

out

.

print

(

"

This is the if statement

"

)

;

}

else

{

System

.

out

.

print

(

"

This is the else statement

"

)

;

}

}

}

The compilation and execution results of the above code are as follows:


这是 else 语句

if...else if...else statement

An if statement can be followed by an else if...else statement, which can detect multiple possible situations.

When using if, else if, else statements, note the following points:

Syntax

The syntax format of if...else is as follows:

if

(

boolean expression

1

)

{

//

execute code if boolean expression 1 is true

}

else

if

(

boolean expression

2

)

{

//

execute code if boolean expression 2 is true

}

else

if

(

boolean expression

3

)

{

//

execute code if boolean expression 3 is true

}

else

{

//

execute code if none of the above boolean expressions is true

}

Example

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

args

[

]

)

{

int

x

=

30

;

if

(

x

==

10

)

{

System

.

out

.

print

(

"

Value of X is 10

"

)

;

}

else

if

(

x

==

20

)

{

System

.

out

.

print

(

"

Value of X is 20

"

)

;

}

else

if

(

x

==

30

)

{

System

.

out

.

print

(

"

Value of X is 30

"

)

;

}

else

{

System

.

out

.

print

(

"

This is the else statement

"

)

;

}

}

}

The compilation and execution results of the above code are as follows:


Value of X is 30

Nested if...else Statements

It is legal to use nested if...else statements. That is, you can use an if or else if statement inside another if or else if statement.

Syntax

The syntax format of nested if...else is as follows:

if

(

boolean expression

1

)

{

//

// execute code if boolean expression 1 is true

if

(

boolean expression

2

)

{

//

// execute code if boolean expression 2 is true

}

}

You can nest else if...else just like if statements.

Example

Test.java File Code:

public

class

Test

{

public

static

void

main

(

String

args

[

]

)

{

int

x

=

30

;

int

y

=

10

;

if

(

x

==

30

)

{

if

(

y

==

10

)

{

System

.

out

.

print

(

"

X = 30 and Y = 10

"

)

;

}

}

}

}

The compilation and execution results of the above code are as follows:


X = 30 and Y = 10