The article directories

  • While loop control
    • 1. Basic grammar
    • 2. The while loop performs process analysis
    • 3. Notes and details
    • 4. Class exercises

While loop control

1. Basic grammar

2. The while loop performs process analysis

While01.java

  1. Draw a flow chart

  2. Use the while loop to complete the previous problem
	// Output 10 sentences hello, xi moving
	
	int i = 1; // Loop variable initialization
	while( i <= 10 ) {// Loop condition
		System.out.println("Hello, I look beautiful." + i);// Execute the statement
		i++;// Loop variable iterates
	}
	
	System.out.println("Exit while, continue..." + i); / / 11
Copy the code

3. Notes and details

  1. A loop condition is an expression that returns a Boolean value
  2. A while loop is a judgment before execution statement

4. Class exercises

WhileExercise.java

  1. Print all numbers between 1 and 100 divisible by 3 [using while]
  • Simplify complexity: break down complex requirements into simple requirements and complete them step by step. First print the numbers between 1 and 100
	int i = 1;
	while (i <= 100){
	    System.out.println("i="+i);
	    i++;
	}
Copy the code

  • To live after death: Consider fixed values first, then convert to flexible values. And the number divisible by 3 is expressed as a variable,1-100.Between the range of100It’s also represented by variables.
	int i = 1;
	int end = 100;
	int count = 3;
	while (i <= end){
	    if (i % count == 0){
	        System.out.println("i="+i);
	    }
	    i++;
	}
Copy the code

  1. Print all even numbers between 40 and 200 [use while]
	int j = 40;
	while (j <= 200) {if (j % 2= =0){
	        System.out.println("j=" + j);
	    }
	    j++;
	}
Copy the code