Some error-prone little details

(1) The difference between using i++ and ++i in a loop

while loop using i++

 1 public static void main(String[] args) {
 2         
 3         int count = 0;
 4         int max = 5;
 5 
 6         while(count++<max){
 7             System.out.println(count);
 8         }
 9 
10         System.out.println(count);
11     }

Before the code runs to line6, count=0, while (count++< max) can be understood as two consecutive statements while (count<max) and count=count+1. So it will first judge 0<5?, and then count becomes 1 before entering the loop block. Understand in turn, the last few states are count=4, count<5?, count=count+1=5, enter the loop block and output 5; at this time count=5, count<5?, count=count+1=6, Do not enter the loop block. The code for the loop is executed 5 times.

It should be noted here that the last value of count in the loop code block (line6-9) is max, the value after the loop is ended is max+1, and the loop is executed max times.

so output

1
2
3
4
5
6

 

while loop using ++i

 1 public static void main(String[] args) {
 2         
 3         int count = 0;
 4         int max = 5;
 5 
 6         while(++count<max){
 7             System.out.println(count);
 8         }
 9 
10         System.out.println(count);
11     }

Before the code runs to line6, count=0, while (++count< max) can be understood as two consecutively executed statements count=count+1 and while (count<max). So when entering the loop for the first time, count=count+1=1, 1<5?, then enter the loop block to output 1, the last few states are count=4, then enter line6, count=count+1=5, 5 <5? , without entering the loop block. The code for the loop is only executed 4 times.

It should be noted here that the last value of count in the loop code block (line6-9) is max-1, the value after the loop is ended is max, and the loop is executed max-1 times.

so output

1
2
3
4
5

 

i++ in for loop

public static void main(String[] args) {
        
        int count = 0;
        int max = 5;

        for(;count<max;count++){
            System.out.println(count);
        }
        System.out.println(count);
    }

output

0
1
2
3
4
5

++ i in for loop

public static void main(String[] args) {
        
        int count = 0;
        int max = 5;

        for(;count<max;++count){
            System.out.println(count);
        }
        System.out.println(count);
    }

output

0
1
2
3
4
5

The results of using count++ and ++count in the for loop are the same, and they both loop max times , because the for loop executes the judgment statement count<max and enters the loop block first, and then executes count++ (or ++count) after executing the loop block. Here There is no difference. Before entering the loop block, count will be incremented by 1.

Note: the output of the for loop count starts from 0, while the while loop starts from 1.

(2) Updating...

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325160635&siteId=291194637