Home  >  Article  >  Java  >  How to write if statement in java

How to write if statement in java

王林
王林Original
2019-11-30 10:13:404132browse

How to write if statement in java

if statement

The syntax of the if statement is as follows:

if(布尔表达式){
   //如果布尔表达式为true将执行的语句
}

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

Free video tutorial recommendation: java video tutorial

if...else statement

The if statement can be followed by an else statement. When the Boolean expression value of the if statement is false, the else statement block will be executed.

if…else is used as follows:

if(布尔表达式){
   //如果布尔表达式的值为true
}else{
   //如果布尔表达式的值为false
}

if...else if...else statement

if The statement can be followed by an else if...else statement, which can detect a variety of possible situations.

When using if, else if, else statements, you need to pay attention to the following points:

The if statement has at most one else statement, and the else statement is after all else if statements.

The if statement can have several else if statements, and they must precede the else statement.

Once one of the else if statements is detected as true, the other else if and else statements will skip execution.

if...else syntax format is as follows:

if(布尔表达式 1){
   //如果布尔表达式 1的值为true执行代码
}else if(布尔表达式 2){
   //如果布尔表达式 2的值为true执行代码
}else if(布尔表达式 3){
   //如果布尔表达式 3的值为true执行代码
}else {
   //如果以上布尔表达式都不为true执行代码
}

Nested if...else statement

Use nesting The if...else statement is legal. This means you can use an if or else if statement inside another if or else if statement.

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

if(布尔表达式 1){
   ////如果布尔表达式 1的值为true执行代码
   if(布尔表达式 2){
      ////如果布尔表达式 2的值为true执行代码
   }
}

For more related articles and tutorials, you can visit: Java Language Introduction

The above is the detailed content of How to write if statement in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn