Java for loop statement usage, while loop statement usage

catalogue

for loop

while Loop

do...while Loop

do... The difference between while loop and for loop and while loop:

The difference between for loop and while loop:

First of all, the operation process of our loop statements is: Judgment - > loop - > control

for loop

Format: for (initialization statement; conditional judgment statement; conditional control statement)

{circular body sentence}

Execution process: 1 Execute initialization statement;

2. Execute conditional judgment statements; (if it is true, the execution will continue, and if it is false, the loop will end)

3. Execute the loop body statement;

4. Execute conditional control statements;

5. Return to the second step to continue the cycle;

It may not be easy to understand the literal meaning only. We learn through simple code:

public class Max {
	public static void main(String args[]) {
		for(int i=0;i<5;i++) {
			/*First, we assign 0 to i as the initial value
			  1,Execute i=0 first;
			  2,When judging whether i is less than 5, it is true because 0<5
			  3,When it is true, execute the loop body statement, that is, print the value of i on the console
			  4,After printing, i++ operation can be carried out. The value of I increases by 1 to become 1
			  5.Then proceed to the second step
			  After performing the above operation for 5 times, the value of i becomes 5,
			  When executing the condition judgment statement, the condition is not satisfied,
			  So it will exit the for loop and execute the following code
			  */
			System.out.println(i);
		}
		System.out.println("implement for Code outside the loop");
	}
}

The following is the output of the console:

For loop is a very common statement. For example, we need to print "program ape" 100 times. We can use output statement to output 100 times, that is, system out. Println ("program ape"); It's too troublesome to write this string of code a hundred times. After learning the for loop, we can use the following code to perform the same output:

public class A {
	public static void main(String args[]) {
		for(int i=0;i<100;i++) {
			
			System.out.println("Cheng xuape");
		}
		
	}
}

In this way, the program ape will be output 100 times on the console, which is much more convenient than writing 100 output statements.

while Loop

Format: while (conditional judgment statement)

{loop body statement; conditional control statement;}

Execution process: 1. Execute initialization statements;

2. Execute conditional judgment statements; If it is true, the execution will continue, and if it is false, the loop will end

3. Execute the loop body statement;

4. Execute conditional control statements;

5. Return to the second step to continue;

Let's look at the code:

public class Max {
	public static void main(String args[]) {
		int i=0;		//This is an initialization statement
		while(i<5) 		//Judge first, i=0, i<5, meet the conditions, so enter the while cycle
		{
			System.out.println(i);   //Print the value of i on the console
			i++;		//Then operate the value of i by +1
			//Then I becomes 1 and continues to judge i<5, 1<5, so you can continue to execute the contents of the loop body
			//In this way, after 5 cycles, the value of I becomes 5, which is not satisfied with i<5, so the contents of the loop body are no longer executed
		}
		System.out.println("This is a statement outside the loop body");
	}
}

The following is the console output:

In fact, while loop is similar to for loop, but while loop is more convenient to construct an endless loop. When we change the conditional judgment statement to true, we will fall into an endless loop. See the following code:

	public class Max {
		public static void main(String args[]) {
			int i=0;		//This is an initialization statement
			while(true) 		//There is no need to judge here. The judgment statement is always true
			{
				System.out.println(i);   //Print the value of i on the console
				i++;		//Then operate the value of i by +1
				//Just keep cycling
			}
			//System.out.println("this is a statement outside the loop body");
			/*Because it is an endless loop that will always execute the code of the while loop, the statements outside the loop above
			  It becomes unreachable code, that is, it will never be executed*/
		}
	}

The console will output an increasing number and fall into an endless loop.

do...while Loop

Format: do{loop body statement; conditional control statement;}

while (conditional judgment statement);

Execution process: 1. Execute initialization statements;

2. Execute the loop body statement;

3. Execute conditional control statements;

4. Execute conditional judgment statements;

5. Return to the second step and continue to cycle;

Let's look at the code:

	public class Max {
		public static void main(String args[]) {
		int i=0;
		do 
		{
		System.out.println(i);
		i++;
		//First perform the operation of printing i, and then perform the operation of +1 on i
		}
		while(i<5);
		//Then judge whether the value of i is less than 5. If it is less than 5, continue to execute. If it is greater than 5, exit the loop
		System.out.println("Execute out of circulation statements");
		}
	}

The following is the output of the console:

do... The difference between while loop and for loop and while loop:

for loop and while loop are to judge whether the condition is true, and then execute the loop body statement

do... The while loop executes the loop body once first, and determines whether the condition is true and whether to continue to execute the loop body

Let's look at the code

public class Max {
		public static void main(String args[]) {
		int i=0;
		do 
		{
		System.out.println(i);
		i++;
		//First perform the operation of printing i, and then perform the operation of +1 on i
		}
		while(i>5);
		//i judged here is greater than 5, so it will exit the loop,
		//But the above statement block has been executed, so the console will output the value of i
		System.out.println("Execute out of circulation statements");
		System.out.println(i);
		//Because the i++ operation has been performed once in the loop body, the output value of I is 1
		}
	}

Console output is:

In this way, when we use do While the loop body, even if our conditional judgment statement is wrong at the beginning, the statements in the loop body will be executed once first.

The difference between for loop and while loop:

In the for loop, the self increasing variable i we control belongs to the for loop, so it will not be accessed after the for loop ends.

That is, the I for (int i=0; i<5; i++) only belongs to the for loop. We can only use this I in the for loop. If we use it outside the for loop, an error will be reported. For example: use system out. println(i); An error will be reported. Moreover, we have defined a local variable named I here. Even outside the for loop, we can no longer define a local variable named I, which will also have duplicate names.  

This will not happen in the while loop, because we define i outside the loop, so we can use this i even if it is not in the loop body.

Well, that's all the content of this article. Thank you for watching.

Tags: Java

Posted by CharlesH on Fri, 15 Jul 2022 05:30:33 +0930