Home > Java > javaTutorial > body text

[java tutorial] Java branch structure - if...else/switch

黄舟
Release: 2016-12-26 11:59:59
Original
1291 people have browsed it

Java branch structure - if...else/switch

Sequential structure can only be executed sequentially, and judgment and selection cannot be made, so a branch structure is required.

Java has two branch structures:

if statement

switch statement

if statement

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

Syntax

The syntax of the If statement is as follows:

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

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

public class Test {

   public static void main(String args[]){
      int x = 10;

      if( x < 20 ){
         System.out.print("这是 if 语句");
      }
   }
}
Copy after login

The result of compiling and running the above code is as follows:

这是 if 语句
Copy after login

if...else statement

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

Syntax

if...else is used as follows:

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

Example

public class Test {

   public static void main(String args[]){
      int x = 30;

      if( x < 20 ){
         System.out.print("这是 if 语句");
      }else{
         System.out.print("这是 else 语句");
      }
   }
}
Copy after login

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

这是 else 语句
Copy after login

if ...else if...else statement

if statement can be followed by elseif...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:

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

If statement can have several elseif statements, and they must precede the else statement.

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

Grammar

if...else syntax format is as follows:

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

Example

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 else statement");
      }
   }
}
Copy after login

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

Value of X is 30
Copy after login

Nested if...else statements

It is legal to use nested if-else statements. This means you can use an if or elseif statement within another if or elseif statement.

Syntax

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

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

You can nest it like an if statement else if...else.

Example

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

The result of compiling and running the above code is as follows:

X = 30 and Y = 10
Copy after login

switch statement

switch statement determines whether a variable matches a certain value in a series of values equal, each value is called a branch.

语法

switch语法格式如下:

switch(expression){
    case value :
       //语句
       break; //可选
    case value :
       //语句
       break; //可选
    //你可以有任意数量的case语句
    default : //可选
       //语句
}
Copy after login

switch语句有如下规则:

switch语句中的变量类型只能为byte、short、int或者char。

switch语句可以拥有多个case语句。每个case后面跟一个要比较的值和冒号。

case语句中的值的数据类型必须与变量的数据类型相同,而且只能是常量或者字面常量。

当变量的值与case语句的值相等时,那么case语句之后的语句开始执行,直到break语句出现才会跳出switch语句。3

当遇到break语句时,switch语句终止。程序跳转到switch语句后面的语句执行。case语句不必须要包含break语句。如果没有break语句出现,程序会继续执行下一条case语句,直到出现break语句。

switch语句可以包含一个default分支,该分支必须是switch语句的最后一个分支。default在没有case语句的值和变量值相等的时候执行。default分支不需要break语句。

实例

public class Test {

   public static void main(String args[]){
      //char grade = args[0].charAt(0);
      char grade = &#39;C&#39;;

      switch(grade)
      {
         case &#39;A&#39; :
            System.out.println("Excellent!"); 
            break;
         case &#39;B&#39; :
         case &#39;C&#39; :
            System.out.println("Well done");
            break;
         case &#39;D&#39; :
            System.out.println("You passed");
         case &#39;F&#39; :
            System.out.println("Better try again");
            break;
         default :
            System.out.println("Invalid grade");
      }
      System.out.println("Your grade is " + grade);
   }
}
Copy after login

以上代码编译运行结果如下:

$ java Test
Well done
Your grade is a C
$
Copy after login

 以上就是【java教程】Java分支结构 - if...else/switch的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template