Loop structure (while, do-while, for)

1. while keyword

1. Code:

	boolean sign = true;
	while (sign) {
    
    // 括号中写判定条件,结果类型为boolean
		// 循环体
	}

2. Knowledge points

  • When the condition in the parentheses is judged to be true, execute the loop body; when the judged result is false, end the loop. The decision is executed before each loop.
  • It should be noted that the difference between while in Java and C language is that the result in parentheses requires a value of boolean type, through (1) ">" "<" "==" "!=" ">="" <=" to judge the size or whether it is equal; (2) Call the variable, or call the result of the method as boolean to get the boolean value.
  • Supplement: The judgment condition in C language is that the return value in parentheses is a non-zero number (the judgment result in C language is only 0 and non-zero, and non-zero is considered to be 1, which corresponds to true; 0 corresponds to false. There is no boolean in C89. A basic data type, if necessary, you can define it yourself
	#define TRUE 1
	#define FALSE 0

It is just for us to understand the code logic, and the logic inside the code will not change.


Two, do-while code block

1. Code:

	boolean sign = true;
	do {
    
    
		// 循环体
	} while (sign);
  • The do-while code block is used to ensure that the loop body is executed at least once . The operating principle is similar to while, except that the first execution of the loop body does not require a judgment condition.

Three, for keyword

1. Code

	int arr[] = new int[10];
	for (int i = 0; i < arr.length; i++) {
    
    
		// 循环体
	}

2. Knowledge points

(1) The parameters after the for keyword (things in parentheses) are generally divided into three parts, separated by English semicolons.

	for(第一部分;第二部分;第三部分){
    
    
		// 循环体
	}

a. All three parts can be empty, namely:

	for (;;) {
    
    
		// 循环体
	}

At this time, the effect is equivalent to:

	while (true) {
    
    
		// 循环体
	}

It can only be jumped out by the break inside the loop body, otherwise it will enter an infinite loop.


b. The first part will be executed before the judgment and loop body, and only once

	// 可以为空
	for (;第二部分;第三部分) {
    
    
		// 循环体
	}
	
	// 给参数设定初值
	for (i = 0;第二部分;第三部分) {
    
    
		// 循环体
	}

	// 定义参数并赋值
	for (int i = 0;第二部分;第三部分) {
    
    
		// 循环体
	}

c. The second part: the judgment condition, which is judged before each execution of the loop body. If the judgment result is true, the loop body will be executed; if the result is false, the loop will end.

	// 下面表示i分别等于0~9,执行了10遍循环体,一般用于枚举或数组的遍历
	int arr[] = new int[10];
	for (int i = 0; i < arr.length;i++) {
    
    
		// 循环体
	}
	
	// 下面表示一个标志变量sign,当满足一定条件时,修改sign的值以结束循环
	for (boolean sign = true; sign == true;) {
    
    
		// 循环体
		if (……) {
    
    
			sign = false;
		}
	}

d. The third part: the additional loop body, the operation of the third part will be executed every time the loop body is executed

	// i从初值0开始,每次循环i都会自加1
	int arr[] = new int[10];
	for (int i = 0; i < arr.length;i++) {
    
    
		// 循环体
	}

(2) Advanced usage of for

	for (声明语句 : 表达式) {
    
    
		// 循环体
	}

example:

	int arr[] = new arr[10];
	for (int i : arr) {
    
    
		System.out.println(i);
	}

a. Declaration statement: A new variable of the same type as the array must be defined, and cannot be assigned

b. Expression: here should be an array whose data type is consistent with the declaration statement. If it is a defined array, just write the array name directly (do not need to write in the format of arr[]); here can also be a method , the return value is an array.

① Error-prone point one
	int i;
	int arr[] = new arr[10];
	for (i : arr) {
    
    
		System.out.println(i);
	}

The above writing method is not allowed (an error will be reported), and the variable at i must be defined internally . It is even more impossible not to define!

for

② Error-prone point 2: The declared variable type is inconsistent with the array type
  • The essence of this writing method is that in each cycle of traversal, the corresponding elements in the array are assigned to the variables declared on the left, so automatic type conversion will occur when the data types are inconsistent.
  • Add a knowledge point: automatic type conversion
    In short, the levels are as follows
低  ------------------------------------>  高

byte,short,char—> int —> long—> float —> double 

The level of the variable on the left is lower than that of the array variable on the right, that is, when the left is lower and the right is higher, the program will report an error .

for1


The level of the variable on the left is higher than that of the array variable on the right, that is, when the left is high and the right is low , the compilation is allowed and no error will be reported
for2
, but it is still not recommended! Not recommended!
It is always right to be standardized. The deeper you study, the more benefits you will discover.

Guess you like

Origin blog.csdn.net/War_wick/article/details/126105246