Skip to content

Files

Latest commit

5ae7d90 · Jun 25, 2020

History

History
204 lines (150 loc) · 5.01 KB

16.md

File metadata and controls

204 lines (150 loc) · 5.01 KB

Java 语句初学者教程

原文: 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 不接受数字作为条件运算符。 它仅将布尔表达式视为返回TRUEFALSE的条件。

If语句的语法

if (true)
   System.out.println("Hello! This will always get printed");
if (Boolean Expression) {
    Code block to get executed
}

If语句的示例代码

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 不接受数字作为条件运算符。 它仅将布尔表达式视为返回TRUEFALSE的条件。 例如,if(1 == 1)被接受,因为它是布尔表达式,将返回trueif(x = 1)语句在 Java 中是不允许的。

if-else语句的语法

if (1==2)
	System.out.println("Hello! This will not get printed");
else
	System.out.println("Hello! This will get printed");
if (Boolean Expression) {
    Code block to get executed
}
else{
code block to get executed when above condition is false
}

If Else语句的示例代码

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");
        }
    }
}

当您运行上面的代码时,它将打印出来。

String has the Value Hello
String is not Hi

if-else if语句

if-else if语句由多个if条件和else语句组成。 如果条件为真,则将执行随附的代码。 如果if条件为假,则它将检查下一个if条件。如果下一个条件为真,则将执行附带的代码,否则将执行代码的else部分。 If Else If语句仅将布尔表达式视为有效条件。

与其他语言不同,Java 不接受数字作为条件运算符。 它仅将布尔表达式视为返回TRUEFALSE的条件。

例如if(x = 1)不允许,而if(1 == 1)被接受,因为它是布尔表达式,并将返回true

if-else if语句语法

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");
if (Boolean Expression) {
    Code block to get executed
}
else{
code block to get executed when above condition is false
}

Java 中If Else If语句的示例代码

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");
        }
    }
}

当您运行上述代码时,它将打印。

String is not Hi
String is neither Hello nor Hi

循环语句的类型

  • for循环
  • While循环
  • do–While循环

流控制语句的类型

  • return语句
  • continue语句
  • break语句