## Learn java Lesson 12: for loop

zyqok 2020-11-13 10:16:51
learn java lesson 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. .

【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.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 .