Learn java Lesson 12: for loop

zyqok 2020-11-13 10:16:51
learn java lesson loop


【 Related links 】

Last one : Xiaobaixue Java The first 11 course :switch Multi branch statement

Next : Xiaobaixue Java The first 13 course :while loop

general programme : Returns a list of

 

From this section on , We're going to talk about cycles ,Java The common loops in programs are for loop ,while loop ,do while loop ,foreach loop etc. .

Let's start with the simplest for The cycle begins with .

【1】 What is cycle ?

See the headline , Most of my friends must be laughing , Don't cycles mean doing one thing over and over again ? Yes ! That's the definition of a loop .

such as : Xiao Zhang is running on the playground in the morning , Suppose Xiao Zhang runs every day 10 circle , So he needs to run a circular playground after running 10 circle . If Zhang every 2 Drink water in a circle , Then Xiao Zhang needs water 5 Time .

 

【2】for Loop structure

for The structure of the loop is as follows :

for( Initial variable ; Loop completion condition ; Step size ) {
// What each cycle does
}

I'll explain each ranking condition separately :

Initial variable : Simply put, the loop is how much to start from , Every day, Xiao Zhang starts from 1 Circle to number one 10 circle , So the initial variable value is 1. Suppose we name the variable the number of laps x, So this is supposed to be  int x = 1;

Loop completion condition : A cycle often has a maximum number of cycles , For example, Xiao Zhang runs here 10 circle , So this 10 It's the completion condition of Xiao Zhang's cycle , So when x Less than or equal to 10 When , Zhang will continue to run , When you run to 11 When you circle , Xiao Zhang will stop running , Because he only runs every day 10 circle .

Step size : Xiao Zhang finished the first run 1 circle , And then he went on to be number one 2 circle , And then 3 circle ,......., The sequence of cycles next to each other is only different 1, there 1 It's running laps, counting steps , Xiao Zhang every 2 Drink water in a circle , So the first 1 If you drink it in a circle, it will be 3 I'll drink when I'm around , The first 3 If you drink it in a circle, it will be 5 I'll drink when I'm around ,......, I didn't drink water once , need 2 circle , So the pace here is 2. When the pace is 1 When , Need to cycle 10 Time , When the pace is 2 When , The loop just needs 5 Time . so , As the pace increases , The less cycles .

What each cycle does : It's easy to understand , Xiao Zhang did it every lap 2 thing . The first is running , This one thing is done every lap . The second thing is to drink water , This is every 2 Circle it once .

 

【3】 Concrete realization

(3.1) Xiao Zhang runs

Let's suppose we do it with Xiao Zhang's running , So his code is as follows :

for(int x = 1; x <= 10; x++) {
System.out.println(" The first " + x +" The lap is over ");
}

The new class Test12, Let's run this code and try :

You can see , The program is finished , Xiao Zhang 10 The lap is over , According to the code, you can compare the above for In contrast to the cyclic structure , Deepen the understanding .

(3.2) Xiao Zhang drinks water

Above is the running event , So how to use the program to express Xiao Zhang's drinking water ?

As follows :

for (int x = 1; x <= 10; x = x + 2) {
System.out.println(" The first " + x + " Drink water in circles ");
}

After the program is executed , The headmaster only drank 5 Secondary water , So it just circulates 5 Time , Because the interval between the steps of drinking water is 2.

【4】 Dead cycle

(4.1) What is an endless loop

The so-called Dead circle , It's just that the program goes on and on , There is no logical termination condition . stay for In circulation , If you cancel the intermediate condition, don't write , So it's a dead circle .

You can see , The execution speed of the program is very fast ,1 Hundreds of thousands of cycles per second , He's not going to stop , And he's going to do it all the time .

(4.2) Be careful with the dead loop

Logically speaking , The dead loop can be executed all the time ?

But in practice , If there is a dead loop in the program , Most of them have problems , And it's often not allowed , Because it will be inefficient consumption of computer resources , Finally, the computer crashed . So when you write about a dead loop , Be careful and careful , If there is such a need , And test it over and over again . Or thinking in an endless loop if you can reduce the frequency of it , For example, every few minutes 1 Time , Otherwise, it would be terrible to run hundreds of thousands of times per second .

【5】 Multiple cycles

All we're going to do is for loop , When for Put another one in the loop for In the cycle , these two items. for Cycles together are called 【 Double cycle 】,

Let us abbreviate a loop as follows :

for () {
}

So the simple structure of the double loop is as follows :  We call it inside for Circulation is internal circulation , The outer one is the outer loop .

for () {
for() {
}
}

Suppose Xiao Ming runs every month on average 30 God , How many laps does Xiao Ming need to run every month ? We can use a double loop as follows :

Let Xiao Ming run every day x, Xiao Ming's days per month are y, Then it can be expressed as follows :

for (int y = 1; y <= 30 ; y ++) {
for (int x = 1; x <= 10 ; x ++) {
System.out.println(" Xiao Ming runs , The first " + y + " Tiandi " + x +" circle ");
}
}

Let's take a look at the code execution : You can see , Xiao Ming's running every lap every day is recorded in cycles .

When you continue to nest in the smallest loop , Form three for When loops are nested , Now it becomes 【 Triple cycle 】,

The structure is as follows :

for() {
for() {
for() {
}
}
}

For example, Xiao Ming runs every year 12 Months , Then we can find out Xiao Ming's running record in one year through triple cycle .

It's based on this , The quadruple cycle , Five cycles ,........

The more cycles , The more complex the code structure , The readability of the code will get worse and worse , So if there is 3 Over and over again , It should be possible to consider whether you can optimize the code or change your mind .

 

【 Related links 】

Last one : Xiaobaixue Java The first 11 course :switch Multi branch statement

Next : Xiaobaixue Java The first 13 course :while loop

general programme : Returns a list of

 

版权声明
本文为[zyqok]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple