Home > Java > javaTutorial > Java--program flow control

Java--program flow control

零下一度
Release: 2017-06-25 10:56:29
Original
1477 people have browsed it

Java program flow control (Part 1)

Java programs are generally divided into three process control structures: sequential structure, branch structure, and loop structure

  • Sequential structure

The program is executed line by line from top to bottom, without any jumps or judgment statements in the middle.

The sample code is as follows:

 1 public class TestSortStruc { 2     public static void main(String[] args) { 3         //流程控制:顺序结构 4         int i = 15; 5         int j = i + 1; 6         System.out.println(j); 7          8         /*错误示例,因为n的赋值语句使用到了m,所以不能将m在n后面定义 9          * int n = m + 1;10            int m = 10;11            System.out.println(n);*/12     }13 }
Copy after login

  • Branch structure

Selectively execute a certain code block based on conditions.

It is divided into two types of branch statements: if...else and switch..case.

1. Three structures of if statements:

1. if(true){

Execution code block;}

The sample code is as follows:

1 public class TestIf1 {2     public static void main(String[] args) {3         if(true){4             System.out.println("Hello World!!");5         }6     }7 }
Copy after login

 2. if (conditional judgment statement){

##  Execution code block;}

else{

Execution code block;}

The sample code is as follows:

 1 public class TestIf2 { 2     public static void main(String[] args) { 3         int age = 21; 4         if(age>18){ 5             System.out.println("你已经成年了!!"); 6         }else{ 7             System.out.println("你还没有成年!!"); 8         } 9     }10 }
Copy after login

##  3. if (conditional judgment statement) {

  Execution code block;}

## else if (conditional judgment statement) {

Execution code block;}

##  ……

##   else{Execute code block;}

 1 public class TestIf3{ 2     public static void main(String[] args) { 3         int age = 26; 4         
 5         if(age > 130 || age < 0){ 6             System.out.println("估计不是人吧!!"); 7         }else if(age <30){ 8             System.out.println("你还挺年轻!!只有"+ age +"岁"); 9         }else{10             System.out.println("我都是孩子他爹了,你另找心上人吧!!");11         }12     }13 }
Copy after login
Obtain the value through the keyboard, then use the if judgment statement to judge the student's score, and use the Scanner object to allow the user to enter the value on the console

 

The sample code is as follows: 

 1 /*题目: 2  * 从键盘输入自己的考试成绩 3  * 当成绩为100分时,奖励一台外星人电脑 4  * 当成绩在80~99时,奖励一部iPhone7 plus 5  * 当成绩在60~80时,奖励一本考试科目的习题册 6  * 成绩低于60时,没有奖励,需要连续三个月不许玩游戏看电视。*/ 7 //1.导入Scanner包,记住一定要是java.util下的Scanner包 8 import java.util.Scanner; 9 10 public class TestScanner {11     public static void main(String[] args) {12         //2.new 一个Scanner对象sc13         Scanner sc = new Scanner(System.in);14         /*3.从键盘获取用户输入的值,因为要判断成绩,15             所以规定输入的值是int类型的数字,使用nextInt()方法*/16         System.out.println("请输入你的成绩:");17         int grade = sc.nextInt();18         //使用if判断语句进行成绩奖励判断19         if(grade == 100){20             System.out.println("恭喜你,获得一台外星人电脑!!");21         }else if(grade < 100 && grade >= 80){22             System.out.println("恭喜你,获得一部iPhone7 plus!!");23         }else if(grade < 80 && grade >=60 ){24             System.out.println("恭喜你,你需要完成一本本学科的习题册!!");25         }else{26             System.out.println("很遗憾,你在未来的三个月不能玩游戏,看电视!!");27         }28     }29 }
Copy after login
Note: 1.if condition judgments can be nested;

  2. 1) If there is a "mutually exclusive" relationship between multiple conditions, then the order of the conditional statements is free;

    2) If There is an "inclusive" relationship between multiple conditions, so the conditions requiring a smaller range should be written above the conditions with a larger range.

 

2. switch...case statement:

 switch(

expression

){ case constant 1:

Statement 1;

Break;

## case Constant 2 :

          

       

 ……

Case constant n:

  Statement n;

  break;

 default:

        

##        

 }

Based on the value of the expression, select the corresponding case to judge. Once the case condition is met, execute the corresponding case statement.

The data types of the expression value in parentheses after switch include: char, byte, short, int, enumeration, String (String can only be used with JDK1.7 or above type).

The constant followed by case can only be an exact value, not a range of values.

The sample code is as follows:

 1 public class TestSwitch { 2     public static void main(String[] args) { 3         int i=1; 4         switch(i){ 5         case 0: 6             System.out.println("zero"); 7             break; 8         case 1: 9             System.out.println("one");10             break;11         case 2:12             System.out.println("one");13             break;14         case 3:15             System.out.println("one");16             break;17         case 4:18             System.out.println("one");19             break;20         default:21             System.out.println("ending");22             break;23         }24     }25 }
Copy after login

Note: If there is no break or the end has been reached, other case statements will continue to be executed. If you only want to get a certain value, you need to add the break keyword; although default is the end statement, it can It is written before or after any case in the switch, but it cannot be written inside the case, but it is habitually placed at the end. Default does not need to be written.

  • Loop structure

Through the loop condition, a certain code block is repeatedly executed until the The condition is not met.

It is divided into three types of loop statements: while, do..while, and for loop.

 Note: The foreach loop is provided in JDK1.5, which is more convenient for convenience collections and array elements.

The loop structure is the most commonly used and the most important, so it will be sorted out separately in the second chapter.

The above is the detailed content of Java--program flow control. For more information, please follow other related articles on the PHP Chinese website!

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