Home > Java > javaTutorial > body text

Detailed explanation of flow control statement examples in java

怪我咯
Release: 2017-06-25 10:10:59
Original
1296 people have browsed it

Structural classification of programs:
Sequential structure: execute the code once in the order in which it is written
Selection structure: selectively execute different codes according to different conditions
Loop structure: Repeatedly execute a certain piece of code under certain conditions

Selection structure:

Also called a branch structure, Java provides a way to selectively execute a certain piece of code according to different conditions. Two if structures and switch structures

if is divided into three formats:

The first format of if

if (Condition){
  Statement block
  }

Execution process:
Judge the condition first, if it is true, the statement block will be executed, otherwise the statement block will not be executed

Code demonstration:

 1 public static void main(String[] args) { 2         System.out.println("开始"); 3         // 定义两个变量 4         int a = 10; 5         int b = 20; 6  7         if (a == b) { 8             System.out.println("a等于b"); 9         }10 11         int c = 10;12         if (a == c) {13             System.out.println("a等于c");14         }15 16         System.out.println("结束");17     }
Copy after login

The second format of if:

if (condition){

Statement block 1

}else{

 Statement block 2

}

Execution process:
Determine the condition first True or false, if true, only statement block 1 will be executed, otherwise, only statement block 2 will be executed.
Code demonstration:

 1 public static void main(String[] args) { 2         System.out.println("开始"); 3         // 判断给定的数据是奇数还是偶数 4         // 定义变量 5         int a = 100; 6         // 给a重新赋值 7         a = 99; 8  9         if (a % 2 == 0) {10             System.out.println("a是偶数");11         } else {12             System.out.println("a是奇数");13         }14 15         System.out.println("结束");16     }
Copy after login

The third format of the if statement:

if(condition 1){

 Statement block 1
}else if(condition 2){

 Statement block 2
}else if (Condition 3) {
  Statement block 3
}
...
else if (Condition n){

  Statement block n
}else{

 Statement block n+1
}

Execution process:
First judge condition 1, if it is true, execute statement block 1. After execution is completed, jump out of the entire if structure Execute the statement below if, otherwise judge condition 2,
If condition 2 is true, execute statement block 2, jump out after execution
The entire if structure executes the statement below if, if condition 2 is also false, then judge Condition 3, if true... If all conditions n are judged to be false,
then execute the statement block n+1 in else
Code demonstration:
x and y The relationship satisfies the following:
x>=3 y = 2x + 1;
-1<=x<3 y = 2x;
x<=-1 y = 2x – 1;
According to Given the value of x, calculate the value of y and output it.

 1 public static void main(String[] args) { 2  3         int x = 5; 4         int y; 5         if (x >= 3) { 6             y = 2 * x + 1; 7         } else if (x >= -1 && x < 3) { 8             y = 2 * x; 9         } else  {10             y = 2 * x - 1;11         }12 13         System.out.println("y的值是:"+y);14     }</p>
<p><strong>Select structure switch</strong></p>
<p>Syntax structure<br><br> switch(expression){<br> case target value 1: <br>   Statement block 1<br>   break;<br> case Target value 2:<br>   Statement block 2<br>   break;<br> case Target value 3:<br>   Statement block 3<br>   break;<br> ...<br> case target value n:<br>   Statement block n+1<br>   break;<br> default:<br>    Statement block n+1<br>   break;</p>
<p> }</p>
<p><strong>Execution process:</strong> <br> First calculate the result A of the expression, and use A to compare it with the corresponding target value in sequence. Once an equal target value is found, stop Compare, <br> execute the corresponding statement block, and then execute break to jump out of the entire switch structure. If no target value is equal to A when comparing from top to bottom, execute <br> statement block n+1 to end the switch <br><br> <strong>Requirements:</strong> <br> Expression: The result of the expression, in the JDK1.1~1.4 era, the data type of the expression result can only be char byte short int <br> In the JDK1.5~1.6 era The data type of the result of the expression can only be char byte short int enum (enumeration) <br> In the JDK1.7+ era, the data type of the result of the expression can only be char byte short int enum (enumeration), String ( String)<br><br><strong>Target value:</strong> <br> The data type of the target value must match the data type of the expression result<br> The target value cannot be repeated<br><strong>Code demonstration :</strong><br> Output the corresponding Monday, Tuesday, Wednesday...Sunday according to the values ​​1, 2, 3...7 entered by the keyboard </p>
<div class="cnblogs_code"><pre class="brush:php;toolbar:false"> 1 public static void main(String[] args) { 2         //创建键盘录入对象 3         Scanner sc = new Scanner(System.in); 4          5         //接收数据 6         System.out.println("请输入一个数字(1-7):"); 7         int weekday = sc.nextInt(); 8          9         //switch语句实现选择10         switch(weekday) {11         case 1:12             System.out.println("星期一");13             break;14         case 2:15             System.out.println("星期二");16             break;17         case 3:18             System.out.println("星期三");19             break;20         case 4:21             System.out.println("星期四");22             break;23         case 5:24             System.out.println("星期五");25             break;26         case 6:27             System.out.println("星期六");28             break;29         case 7:30             System.out.println("星期日");31             break;32         default:33             System.out.println("你输入的数字有误");34             break;35         }    
36     }
Copy after login

Loop structure:

Do the same thing repeatedly, execute the same piece of code repeatedly
The composition of the loop:
Initialization statement
Loop exit (condition)
Loop body
Loop increment

There are three loop structures:
for loop while loop do...while loop
for loop:
Grammar format:
for (Initialization statement; condition; step expression) {

Loop body
}

Execution process:
Step one: Initialization statement
Step 2: Determine the condition. If the condition is true, enter the third step. If the condition is false, enter the fifth step.
Step 3: Execute the loop body
Step 4: Execute the step expression and enter the fifth step. Step 2
Step 5: End the loop

Code demonstration:
Count how many "narcissus numbers" there are
The so-called narcissus number refers to a three A number of digits whose sum of cubes is equal to the number itself.

 1 public static void main(String[] args) { 2         //1.定义一个统计变量,初始值为0 3         int count = 0; 4          5         //2.使用for循环获取所有的三位数 6         for(int x=100; x<1000; x++) { 7             //3.拆分三位数的百位,十位,个位 8             int ge = x%10; 9             int shi = x/10%10;10             int bai = x/10/10%10;11             12             //4.利用公式判断这个三位数是否为水仙花数,如果是,统计变量++13             if((ge*ge*ge+shi*shi*shi+bai*bai*bai) == x) {14                 count++;15             }16         }17         18         //5.循环结束之后,打印统计结果19         System.out.println("水仙花数共有:"+count+"个");20     }
Copy after login

while loop:

Syntax structure:

Initialization statement
while(condition){
Loop body
Step statement
}

Execution process:
Step 1: Initialization statement
Step 2: Determine the condition. If the condition is true, go to the third step. If the condition is false, go to the fifth step. Step
Step 3: Execute the loop body
Step 4: Execute the step statement, and then enter the second step
Step 5: End the loop

do... while loop
Grammar format:

Initialization statement
do{
Loop body
Step statement

}while(condition)

Execution process:
Step 1: Initialization statement
Step 2: Execute loop body
Step 3: Execute step statement
Step 4: Determine the condition, if the condition is true, Go to step two, otherwise go to step five
Step five: End the loop

The difference between the three types of loops:
Although the same function can be completed, there are still Small difference:
do...while loop will execute the loop body at least once.
The for loop and the while loop will only execute the loop body when the condition is established.
The small difference between the for loop statement and the while loop statement:
Usage difference: control the variable controlled by the conditional statement, in After the for loop ends,
can no longer be accessed, but the while loop can continue to be used. If you want to continue using it,
use while, otherwise it is recommended to use for. The reason is that when the for loop ends, the variable disappears from the memory, which
can improve memory usage efficiency.
It is recommended to use loops for -- while -- do..while

Jump statements can control the execution of loops:
There are two types
break : When used in a loop, it means to end the current loop
When used in a switch statement, it means to jump out of the current switch structure

continue: When used in a loop, it means to end the current loop Cycle through the next cycle
It is meaningless to jump out of the loop

This article ends here. I will continue to summarize everything from the basics of Java to the entire javaWeb development. Interested garden friends can continue to pay attention and learn together!!!

The above is the detailed content of Detailed explanation of flow control statement examples in java. 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