当前位置:网站首页>Loop structure -- while loop and do while loop
Loop structure -- while loop and do while loop
2022-07-21 19:29:00 【Start】
First of all, let's talk about the benefits of the next cycle : The most obvious is to reduce the duplication and redundancy of code , Greatly improved efficiency
1.while loop
Grammatical structure :
Initial value ;
while( The loop condition ){
// Loop operation code
Iteration part
}
The law of execution :
1) First, execute 1 Code of second initial recognition value
2) Judge cyclic condition , If the cycle condition is true, Is executed {} Inside the loop operation code and iteration part of the code , After iteration , Continue to return to judge the cycle condition , Decide whether to continue the cycle operation according to the result of the cycle condition
If the cycle condition is false, End the whole while Loop structure , So let's keep going while The code after the loop structure
Be careful :
1) The initial value code is executed only once
2) No matter how simple or complex it is written , The result can only be Boolean , Either for true Either for false
3) The function of the iteration part is to change the initial value , Let the cycle condition no longer be satisfied at a certain time , To end the whole while Loop structure
4)while Loop first judge , After execution
Let's take the simplest example :
subject : Output 100 Study hard and make progress every day
int i =1;
while(i<=100){//i<=100 The loop condition
System.out.println(" The first "+i+" All over : study hard , Day day up .");// Cyclic operation
i++;// iteration : The purpose of iteration is to change the initial value , In order to be at a certain moment , Let the cycle condition no longer be satisfied , End loop operation , If there is no iteration , The cycle goes into a dead cycle
}
2.do-while loop
Grammatical structure :
Initial value l;
do{
Cyclic operation
Iteration part
}while( The loop condition );
The law of execution :
1) First, execute 1 Secondary initial value code
2) Get into {} Internal execution 1 Loop operation and iteration part , Judge the condition after execution , If the condition is true, return {} Execute loop operation and iteration part in ,
If the condition is false, End the whole do-while Loop structure , So let's keep going do-while The code after the loop structure
Be careful :
1) The initial value only executes 1 Time
2) Cycle conditions, no matter how simple or complex they are written , Can only be Boolean , Either for true, Either for false
3)do-while Loop is to execute a loop operation first , And then judge the cyclic condition , therefore , If the first judgment cycle condition is not satisfied , The loop operation will also be performed once (do-while The loop operation in the loop will be executed at least once )
Still a small example
subject :/ Use do-while Cyclic output 1-100 Value
// Initial value
int i =1;
do{
System.out.println(i);// Cyclic operation
i++;// Iteration part
}while(i<=100);// The loop condition
To sum up while and do-while The difference between
1) Execution order is different
2) When the initial condition does not meet the cycle conditions :
while A loop will not execute
do-while The loop executes at least once in any case , To put it simply , Don't worry about anything. Let's run the cycle again first !
边栏推荐
- plantuml画链路图
- Penetration test - Application of file containing vulnerabilities and PHP pseudo protocol
- Create high-performance applications and continuously optimize the user experience
- 四种bean拷贝工具对比
- 出现“TypeError: ‘str‘ object does not support item assignment”的原因
- 4. Custom data source of security
- Hcip experiment the next day
- 程序员第一课“hello word”,你知道网工第一课吗?
- Sql优化(九):分页语句优化
- C# 数字信号处理工具包 DSP-Core 重采样(Resample)输出点数是多少
猜你喜欢
Arduino I2C for tca9548a response scanner
MYSQL06_ Seven join operations and union all of sql99
分布式.高性能
Introduction to ESP privilege isolation mechanism
分布式.高并发&高可用
Peripheral driver library development notes 44:ddc114 ADC driver
MYSQL07_获取日期函数、聚合函数、字符串函数
05 正则表达式语法
LBA converted to CHS formula
Hcip experiment the next day
随机推荐
外设驱动库开发笔记44:DDC114 ADC驱动
Web APIs DOM page special effects element size and position
分布式.BASE理论
Sorting out of high-frequency interview questions, answers and knowledge context of Android, a major Internet company in 2022
China phosphorus pentachloride Market Research and investment forecast report (2022 Edition)
Recommended collection | practical operation, data console selection example
程序员第一课“hello word”,你知道网工第一课吗?
L1-008 求整数段和
MYSQL07_ Get date function, aggregate function, string function
Liunx kills processes with the same name in batches
regular expression
Test de pénétration - vulnérabilité d'inclusion de fichiers et application de pseudo - protocoles PHP
Use the mogdb operator to deploy the mogdb cluster (mogdb stack) on kubernetes
Unity - input class
Penetration test - Application of file containing vulnerabilities and PHP pseudo protocol
[introduction to QT] signal slot mechanism
分布式.高并发概念和设计目标
4. Custom data source of security
2022年互联网大厂Android高频面试题及答案及知识脉络的整理
Distributed What index is high concurrency