# Java 语句初学者教程 > 原文: [https://javabeginnerstutorial.com/core-java-tutorial/java-statements-tutorial-for-beginners/](https://javabeginnerstutorial.com/core-java-tutorial/java-statements-tutorial-for-beginners/) 在这里,我们将学习 Java 语句。 ## Java 语句的类型 * 表达声明 * 控制语句 * 分配声明 在本节中,我们将讨论控制语句。 * * * ## 控制语句 * 有条件的执行 * 循环播放 * 流控制语句 * * * ## 条件执行的类型 * `If`语句 * `If-Else`语句 * `If-Else-if`语句 * `switch`语句 * * * ### `if`语句 Java 中的`if`语句**包含仅在应用条件为`true`的情况下才执行的部分代码**。 `if`语句仅接受**布尔表达式**作为条件。 > 与其他语言不同,Java 不接受数字作为条件运算符。 它仅将*布尔表达式*视为返回`TRUE`或`FALSE`的条件。 #### `If`语句的语法 ```java if (true) System.out.println("Hello! This will always get printed"); ``` ```java if (Boolean Expression) { Code block to get executed } ``` `If`语句的示例代码 ```java public class if_condition { public static void main(String[] args) { String string = "Hello"; if ("Hello".equals(string)) { System.out.println("String has the Value Hello"); } if ("Hi".equalsIgnoreCase(string)) { System.out.println("String has the value Hi"); } } } ``` 如果运行上面的代码,它将显示“字符串具有值`Hello`”。 * * * ### `else`语句 `if-else if`语句含有多个`if`条件和`else`语句。如果条件为真,包含的代码会被执行,如果条件为假,则将检查下一个条件。 如果下一个`if`条件为`true`,则将执行包含的代码,否则将执行代码的`else`部分。 `if else if`语句仅采用**布尔表达式作为**有效条件。 > 与其他语言不同,java 不接受数字作为条件运算符。 它仅将*布尔表达式*视为返回`TRUE`或`FALSE`的条件。 > 例如,`if(1 == 1)`被接受,因为它是布尔表达式,将返回`true`。 `if(x = 1)`语句在 Java 中是不允许的。 #### `if-else`语句的语法 ```java if (1==2) System.out.println("Hello! This will not get printed"); else System.out.println("Hello! This will get printed"); ``` ```java if (Boolean Expression) { Code block to get executed } else{ code block to get executed when above condition is false } ``` `If Else`语句的示例代码 ```java public class if_condition { public static void main(String[] args) { String string = "Hello"; if ("Hello".equals(string)) { System.out.println("String has the Value Hello"); } else { System.out.println("String is not Hi"); } if ("HI".equals(string)) { System.out.println("String has the Value Hi"); } else { System.out.println("String is not Hi"); } } } ``` 当您运行上面的代码时,它将打印出来。 ```java String has the Value Hello String is not Hi ``` * * * ### `if-else if`语句 `if-else if`语句由多个`if`条件和`else`语句组成。 如果*条件*为真,则将执行随附的代码。 如果`if`条件为假,则它将检查下一个`if`条件。如果下一个条件为真,则将执行附带的代码,否则将执行代码的`else`部分。 `If Else If`语句仅将**布尔表达式**视为有效条件。 > 与其他语言不同,Java 不接受数字作为条件运算符。 它仅将*布尔表达式*视为返回`TRUE`或`FALSE`的条件。 > > **例如**`if(x = 1)`不允许,而`if(1 == 1)`被接受,因为它是布尔表达式,并将返回`true`。 #### `if-else if`语句语法 ```java if (1==2) System.out.println("Hello! This will not get printed"); else if(1==1) System.out.println("Hello! This will get printed"); else System.out.println("This will get executed when Above conditio is FALSE"); ``` ```java if (Boolean Expression) { Code block to get executed } else{ code block to get executed when above condition is false } ``` Java 中`If Else If`语句的示例代码 ```java public class if_condition { public static void main(String[] args) { String string = "Hello"; if ("HI".equals(string)) { System.out.println("String has the Value Hi"); } else if ("Hello".equals(string)) { System.out.println("String is not Hi"); } string = "NotHello"; if ("Hello".equals(string)) { System.out.println("String has the Value Hello"); } else if ("Hi".equalsIgnoreCase(string)) { System.out.println("String is not Hi"); } else { System.out.println("String is neither Hello nor Hi"); } } } ``` 当您运行上述代码时,它将打印。 ```java String is not Hi String is neither Hello nor Hi ``` * * * ## 循环语句的类型 * `for`循环 * `While`循环 * `do–While`循环 * * * ## 流控制语句的类型 * `return`语句 * `continue`语句 * `break`语句