# 2021-9-18-learning log 7-loop statement for loop statement

1. Circular statement

Features: do something repeatedly, with clear start and stop signs.

Composition of cycle structure:

① Initialization statement: used to indicate the starting state when the loop is opened. In short, it is what it looks like when the loop starts.

② Condition judgment statement: used to represent the condition for repeated execution of the loop. In short, it is used to judge whether the loop can be executed all the time.

③ Loop body statement: used to represent the content of loop repeated execution. In short, it is the matter of loop repeated execution.

④ Conditional control statement: used to represent the contents of each change in the execution of the loop. In short, it controls whether the loop can be executed.

Syntax corresponding to loop structure:

① Initialization statement: here can be one or more statements, which can complete some initialization operations.

② Conditional judgment statement: an expression with boolean result value is used here. This expression can determine whether to execute the loop body, for example: a < 3;

③ Loop body statement: here can be any statement, which will be executed repeatedly

④ Conditional control statement: here, a statement is usually used to change the value of the variable, so as to control whether the loop continues to execute downward. i++,i -- such operations are common.

1.2) for loop statement format

Format:

for (initialization statement; conditional judgment statement; conditional control statement){

Loop body statement;

}

Execution process:

① Execute initialization statement

② Execute the conditional judgment statement to see whether the result is true or false

If false, the loop ends

If true, continue

③ Execute loop body statement

④ Execute conditional control statements

⑤ Go back to ② and continue

```/*
for loop
*/
public class forDemo{
public static void main(String[] args){
//Requirement: output "HelloWorld" 5 times on the console“
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("HelloWorld");
System.out.println("--------");

//Cycle improvement
for (int i = 1; i <= 5; i++){
System.out.println("HelloWorld");
}
}
}```

DOS command prompt window

```C:\Users\Apple>d:

D:\>javac foeDemo.java

D:\>java foeDemo
HelloWorld
HelloWorld
HelloWorld
HelloWorld
HelloWorld
--------
HelloWorld
HelloWorld
HelloWorld
HelloWorld
HelloWorld

D:\>
```

1.3) case: output data

Requirement: output 1-5 and 5-1 data on the console

analysis:

① Repeat the output action, using the loop structure

for (initialization statement, condition judgment data, condition control statement){

Loop body statement;

}

② From 1 to 5, setting initialization starts from 1

for (int i = 1; conditional judgment data, conditional control statement){

Loop body statement;

}

③ From 1 to 5, set the judgment condition to continue execution when it does not reach 5, and execute until the program exceeds 5

for (int i =1; I < = 5; conditional control statement){

Loop body statement;

}

④ From 1 to 5, increase 1 each time, and set the condition control + 1 each time

for (int i = 1; i <= 5; i++){

Loop body statement

}

⑤ Write the repeated events into the loop structure and print the corresponding data

for (int i = 1; i <= 5; i++){

System.our.ptintln(i);

}

```/*
Case:
Requirement: output 1-5 and 5-1 data on the console
*/
public class forDemoTest{
public static void main(String[] args){
//for loop statement output 1-5
for (int i = 1; i <= 5; i++){
System.out.println(i);
}
System.out.println("--------");

//for loop statement output 5-1
for (int i = 5; i >= 1; i--){
System.out.println(i);
}
}
}```

DOS command prompt window

```C:\Users\Apple>

D:\>javac forDemoTest.java

D:\>java forDemoTest
1
2
3
4
5
--------
5
4
3
2
1

D:\>```

1.4) case: summation

Requirements: sum the data between 1-5, and output the sum result on the console

analysis:

① The final result of summation must be saved. You need to define a variable to save the summation result. The initial value is 0

int sum = 0；

② The data from 1 to 5 is completed by using the circular structure

for (int i = 1; i <=5; i++){

sum += i;

}

System.out.println("sum+i");

```/*
Case: summation
Requirements: sum the data between 1-5, and output the sum result on the console
*/
public class forDemoTest1{
public static void main(String[] args){
//for loop
int sum = 0;
for (int i = 1; i <= 5; i++){
sum += i;
/*
sum += 1;  Equal to sum = sum + i;
The first time: sum = sum + i = 0 + 1 = 1;
The second time: sum = sum + i = 1 + 2 = 3;
The third time: sum = sum + i = 3 + 3 = 6;
The fourth time: sum = sum + i = 6 + 4 = 10;
The fifth time: sum = sum + i = 10 + 5 = 1;
*/
}
System.out.println("1-5 The data and between are:" + sum);
}
}```

DOS command prompt window

```C:\Users\Apple>

D:\>javac forDemoTest1.java

D:\>java forDemoTest1
1-5 The data between and are: 15

D:\>
```

1.5) case: ask for even books and

Requirements: find the even sum between 1-100, and output the sum result on the console

analysis:

① The summation of 1-100 data is almost the same as that of 1-5 data, except that the end conditions are different

int sum = 0；

for (int i = 1; i <= 100; i++){

sum += i;

}

② For the even summation of 1-100, add restrictions on the summation operation

int sum = 0 ;

for (int i = 1; i <= 100; i++){

if (execution constraints){

sum += i；

}

}

③ The limiting condition is that an even number is involved in the operation, so the condition should be to judge whether it is an even number

int sum = 0;

for (int i = 1; i <= 100; i++){

if (1 % 2 == 0){

sum += i;

}

}

④ When the loop is completed, the final data is printed out

System. Out. Println ("the even sum between 1 and 100 is:" + sum ");

```/*
Case: seeking even book sum
Requirements: find the even sum between 1-100, and output the sum result on the console
analysis:
①The summation of 1-100 data is almost the same as that of 1-5 data, except that the end conditions are different
②For the even summation of 1-100, add restrictions on the summation operation
③The limiting condition is that an even number is involved in the operation, so the condition should be to judge whether it is an even number
④When the loop is completed, the final data is printed out
*/
public class forDemoTest2{
public static void main(String[] args){
//Define variables
int sum = 0;
for (int i = 1;i <= 100; i++){
if (i % 2 == 0){
sum += i;
}
}
System.out.println("1-100 The even sum between is:" + sum);
}
}```

DOS command prompt window

```C:\Users\Apple>d:

D:\>javac forDemoTest2.java

D:\>java forDemoTest2
1-100 The even sum between: 2550

D:\>```

1.6) case: daffodils

Requirement: output all "daffodils" on the console

What is "daffodil number"?

① The number of daffodils is a three digit number

② The sum of each digit, ten digit and hundred digit cube of daffodil number is equal to the original number

eg: 123 = 1 + 8 + 27 = 36 ≠   one hundred and twenty-three     Not daffodils

371 = 27 + 343 +1 = 371 = 371   It's daffodils

analysis:

① How to find a three digit number

three hundred and seventy-one         1 is the result of the remainder operation of the original number on 10         371 % 10 = 1

② How to find a three digit hundred digit number

three hundred and seventy-one         3 is the result of dividing the original number by 100 (integer)         371 /   100 = 3

③ How to find the three digit ten digit number

three hundred and seventy-one         371 by dividing by 10, you can move 7 to each bit (integer)         371 / 10 = 37

37 the value 7 of the last bit can be obtained by residual operation on 10         37 % 10 = 7

④ Thinking: how to calculate the value of any number in the specified bit?

First use the integer operation to move the required number to one bit, and then use the remainder operation to get the last bit.

eg: 123456789, if you take 5, divide by 10000 to get 12345, and then divide by 10 to get 5

analysis:

① The output of all daffodils must be used to cycle, three digit range: 100 - 999

for (int i = 100;  i <= 999; i++){

}

② Not every three digits is the number of daffodils, so it needs to be determined that the number that meets the conditions can be output

if (execution constraints){

}

③ The judgment condition is to take out each value in the three digits, calculate the cube sum, and compare it with the original number

if (? +? +? = = original number){

Output primitive

}

④ Gets the value on each of the three digits before calculation

int a = bits of the original number;

int b = ten digits of the original number;

int c = the hundredth of the original number;

if (a * a * a  + b * b * b + c * c * c = = original number)

Comprehensive analysis:

① : create a loop first

② : take out the one, ten and hundred digits of the three digits

③ Calculate whether the conditions are met and output if the conditions are met

```/*
Case: number of daffodils
Requirement: output all "daffodils" on the console
What is "daffodil number"?
①The number of daffodils is a three digit number
②The sum of each digit, ten digit and hundred digit cube of daffodil number is equal to the original number
*/
public class forDemoTest3{
public static void main(String[] args){
/*	((same as for below)
for (int i = 100; 1 < 1000; i++){

}
*/
for (int i = 100; i <= 999; i++){
//Get the value on each of the three digits before recalculation
int a = i%10;
int b = i/10%10;
int c = i/10/10%10;

//The judgment condition is to take out each value in the three digits, calculate the cube sum, and compare it with the original number to see whether it is equal;
if (a*a*a + b*b*b + c*c*c == i){
//The number satisfying the output condition is the number of daffodils
System.out.println(i);
}
}
}
}```

DOS command prompt window

```C:\Users\Apple>d:

D:\>javac forDemoTest3.java

D:\>java forDemoTest3
153
370
371
407

D:\>```

1.7) case: Statistics

Demand: count the total number of "daffodils" and control the output number

analysis:

① Define the variable count, which is used to save the number of daffodils. The initial value is 0

int count = 0；

② In the process of determining the number of daffodils, if the conditions are met, it will not be output, but the value of count will be modified

If (a * a * a + b * B + C * c * C = = original number){

}

③ Print out the final result

System.out.println("total number of daffodils:" + count + ");

```/*
Case: count the number of daffodils
Demand: count the total number of "daffodils" and control the output number
*/
public class forDemoTest4{
public static void main(String[] args){
//Define the variable count, which is used to save the number of "daffodils". The initial value is 0
int count = 0;

//To output all daffodils, you must use a loop to traverse all three digits, starting from 100 and ending at 999
for (int i = 100; i < 1000; i++){

//Obtain the value on each pseudo in the three digits before calculation
int a = i%10;
int b = i/10%10;
int c = i/10/10%10;

//In the process of determining the number of daffodils, if the conditions are met, it is not output. Change the value of count to make count+1
if(a*a*a + b*b*b + c*c*c == i){
count++;
}
}
//Print out the final result
System.out.println("The number of daffodils is:" + count + "individual");
}
}```

DOS command prompt window

```C:\Users\Apple>d:

D:\>javac forDemoTest4.java

D:\>java forDemoTest4
Number of daffodils: 4

D:\>```

Tags: Java

Posted on Sat, 18 Sep 2021 14:47:32 -0400 by itisme