当前位置:网站首页>Chapter V: process control
Chapter V: process control
2022-07-25 16:53:00 【Take charge of [email protected]】
The fifth chapter : Process control
5.1、 Branch statement
brief introduction :Java The branch statement of is executed only when part of the program code meets certain conditions .
5.1.1、if else sentence
Basic grammar :
if( Boolean expression ){
Program code block
}else{
Program code block
}
matters needing attention :
- if The following expression must be a Boolean expression , It cannot be of numeric type .
- if hinder else Statement is not necessary .
- Join in if After statement or else The program code block after the statement contains multiple statements , Must be stored in {} Inside . If there is only one sentence in the code block, it can not be used {}
- if else Statements have a special kind of concatenation programming lattice .
public void method(int x){
if(x>0){
System.out.println(" Greater than 0");
}else if(x==0){
System.out.println(" be equal to 0");
}else{
System.out.println(" Less than 0");
}
}
Classic case :
// Judge whether a certain year is a leap year
// Method 1
public boolean idleapYear(int year){
if((year%4==0&&year%100!==0)||(year%400==0))
return true;
else
return false;
}
5.1.2、switch sentence
brief introduction :switch A statement is a multi branch statement , Basic grammar
switch (expr){
case value1:
...
break;
case value2:
...
break;
...
default:
...
break;
}
matters needing attention :
- The parameter value types in the expression include the following
- And int Type compatible types
- String type
- Enumeration type
- switch There can be at most one... In the statement default Clause .
- switch Sentence with that case Matching starts from that place . encounter break Just jump out of the whole switch
- switch Statement can be used if else Statement to implement
Classic case :
public void sortScore(char grade){
switch(grade){
case'A':
System.out.println(grade+"is 85~100");
break;
case'B':
System.out.println(grade+"is 70~84");
break;
case'C':
System.out.println(grade+"is 60~69");
break;
case'D':
System.out.println(grade+"is<60");
break;
default:
System.out.println(" There is no hierarchy ");
break;
}
}
String type parameters
String color="red";
switch(color){
case "blue":
System.out.println(" Blue ");
break;
case "red":
System.out.println(" Red ");
break;
default:
System.out.println(" Other colors ");
break;
}
Enumeration type parameters :
public class SwitchTest{
enum Color{
red,blue,yellow}// Define an enumeration class
public static void main(String [] args){
Color c=Color.blue;
switch(c){
case red:
System.out.println(" Red ");
break;
case blue:
System.out.println(" Blue ");
break;
case yellow:
System.out.println(" yellow ");
break;
default:
System.out.println(" Other colors ");
}
}
}
5.2、 Loop statement
brief introduction : The function of a loop statement is to execute a piece of code repeatedly , Until the conditions are not met .
Four major parts :
- Initialization part : Used to set some initial conditions of the cycle , Set the initial value of the loop control variable
- The loop condition : This is a Boolean expression
- The loop body : This is the main content of the cycle
- Iteration part : To change the value of a loop variable .
5.2.1、while sentence
brief introduction :while Loop statements are the most basic loop statements in loop statements . The basic grammar is as follows :
[ Initialization part ]
while( The loop condition ){
The loop body ( Include iteration part )
}
Precautions for use :
- If the loop body contains multiple statements , Must be placed in braces , If the loop body has only one statement , You don't need braces .
- while The expression is judged at the beginning of the loop , If the return value is false Then the loop body will not be executed once .
- The loop body can be empty
- Ensure that there are conditions for terminating the cycle , Avoid the dead cycle .
Classic case :
public static int max(int [] array){
if(array==null||array.length==0){
throw new IllegalArgumentException(" Invalid array ");
int index=1,location=0;// Initialization part
while(index<array.length){
// The loop condition ,index Is the loop control variable
// The loop body
if(array[location]<array[index]){
location=index;
index++;
}
}
return array[location];
}
}
5.2.2、do while sentence
brief introduction : And while Statements are all circular statements , however do while The loop will first execute the loop body , Then judge the conditions , In any case, the loop body will be executed once .
Grammar format :
[ Initialization part ]
do{
The loop body , Include iteration part
}while( The loop condition );
5.2.3、for sentence
brief introduction :for Circulation and while Like the cycle, we judge the cycle condition first , Then execute the loop body . The grammar is as follows :
for( Initialization part , The loop condition , Iteration part ){
The loop body
}
matters needing attention :
- If for The body of the loop has only one line , May not be used {}
- control for Loop variables are often used only for this loop , It cannot be used elsewhere in the program . Its scope is current for Cycle cannot be in for Use it outside the loop .
- You can use comma statements in the initialization and iteration sections , Comma statements are used to separate statement sequences .
- for The initialization part and iteration part of the loop can be empty
- for Loop statements are generally used when the number of loops can be determined in advance .while Circulation and do while Cycles are used when the number of cycles is not clear .
5.2.4、foreach sentence
brief introduction :foreach The sentence is for A special simplified version of the statement , It can simplify the code of traversing arrays and collections .
Grammar format :
for( Element type Element variables X : Array to be traversed ){
References to variables x Of Java sentence
}
5.2.5、 Multiple cycles
brief introduction : Various loop statements can be nested with each other , Form multiple loops ;
Classic case : multiplication table
public static void main(String[] args){
for (int i = 1; i < 10; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j+"*"+i+"="+j*i+"\t");
}
System.out.println();
}
}
5.3、 Process jump statement
brief introduction :break、continue、return Statements are all process jump statements , But they have different use conditions and functions
- break: To jump out of switch Or the current cycle .
- continue: Jump out of this cycle , Execute next cycle .
- return: Means to exit this method , Return the method called by the previous layer , If the method is not void, You need to provide the corresponding return value .
版权声明
本文为[Take charge of [email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/206/202207251650543039.html
边栏推荐
- 气数已尽!运营 23 年,昔日“国内第一大电商网站”黄了。。。
- 第五章:流程控制
- linux内核源码分析之页表缓存
- China's chip self-sufficiency rate has increased significantly, resulting in high foreign chip inventories and heavy losses. American chips can be said to have thrown themselves in the foot
- Multi tenant software development architecture
- 使用Huggingface在矩池云快速加载预训练模型和数据集
- Test framework unittest test test suite, results output to file
- Who moved my memory and revealed the secret of 90% reduction in oom crash
- 动态规划题目记录
- 7. Dependency injection
猜你喜欢

【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part3):基于规则的问题分类

虚拟内存管理

Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project

备考过程中,这些“谣言”千万不要信!

城市燃气安全再拉警钟,如何防患于未“燃”?

152. Product maximum subarray

Dynamic planning topic record

jenkins的文件参数,可以用来上传文件

复旦大学EMBA同学同行专题:始终将消费者的价值放在最重要的位置

吴恩达逻辑回归2
随机推荐
气数已尽!运营 23 年,昔日“国内第一大电商网站”黄了。。。
Multi tenant software development architecture
[target detection] tph-yolov5: UAV target detection based on Transformer's improved yolov5
jenkins的文件参数,可以用来上传文件
Attachment handling of SAP Fiori
聊聊如何用 Redis 实现分布式锁?
测试框架-unittest-命令行操作、断言方法
2022 latest Beijing Construction welder (construction special operation) simulation question bank and answer analysis
Dynamic planning topic record
第三章、数据类型和变量
复旦大学EMBA同学同行专题:始终将消费者的价值放在最重要的位置
Bo Yun container cloud and Devops platform won the trusted cloud "technology best practice Award"
链游开发现成版 链游系统开发详细原理 链游源码交付
linux内核源码分析之页表缓存
MySQL之联表查询、常用函数、聚合函数
【目标检测】TPH-YOLOv5:基于transformer的改进yolov5的无人机目标检测
IAAs infrastructure cloud cloud network
[target detection] yolov5 Runtong visdrone data set
Ilssi certification | the course of Six Sigma DMAIC
[MySQL] takes you to the database