当前位置:网站首页>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
边栏推荐
- 复旦大学EMBA同学同行专题:始终将消费者的价值放在最重要的位置
- 在 NgModule 里通过依赖注入的方式注册服务实例
- Roson的Qt之旅#100 QML四种标准对话框(颜色、字体、文件、提升)
- jenkins的文件参数,可以用来上传文件
- win10如何删除微软拼音输入法
- unity 最好用热更方案卧龙 wolong
- ILSSI认证|六西格玛DMAIC的历程
- Fastadmin TP installation uses Baidu rich text editor ueeditor
- Jenkins' role based authorization strategy installation configuration
- 测试框架-unittest-命令行操作、断言方法
猜你喜欢

气数已尽!运营 23 年,昔日“国内第一大电商网站”黄了。。。

Hcip notes 11 days

基于redis6.2.4的redis cluster部署

如何安装govendor并打开项目

Hcip notes 12 days
![[xiao5 chat] check the official account < the service provided by the official account has failed, please wait a moment>](/img/b2/cbba006e5d1ada959f494336e93f39.png)
[xiao5 chat] check the official account < the service provided by the official account has failed, please wait a moment>

基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用

Various useful forms of London Silver K-line chart

MySQL之联表查询、常用函数、聚合函数

2D semantic segmentation -- deeplabv3plus reproduction
随机推荐
HCIP笔记十二天
一百个用户眼中,就有一百个QQ
[Nanjing University of Aeronautics and Astronautics] information sharing for the first and second examinations of postgraduate entrance examination
从数字化到智能运维:有哪些价值,又有哪些挑战?
Rainbow plug-in extension: monitor MySQL based on MySQL exporter
SAP Fiori 的附件处理(Attachment handling)
文字翻译软件-文字批量翻译转换器免费
152. 乘积最大子数组
jenkins的Role-based Authorization Strategy安装配置
easyui datagrid控件使用
免费的低代码开发平台有哪些?
激荡20年,芯片产能从零起步到反超美国,中国制造的又一大成就
博云容器云、DevOps平台斩获可信云“技术最佳实践奖”
【知识图谱】实践篇——基于医疗知识图谱的问答系统实践(Part3):基于规则的问题分类
Why 4everland is the best cloud computing platform for Web 3.0
2022年最新北京建筑施工焊工(建筑特种作业)模拟题库及答案解析
MySQL view
slf4j 搭配 log4j2 处理日志
什么是链游系统开发?链游系统开发如何制作
[book club issue 13] +ffmpeg video capture function