A week fast self-study java Programming Advanced Language dry goods note cycle structure grammar

Cyclic structure grammar

One of the process control statements in which the loop is difficult (turning).

1. Master the while cycle structure

//When: it's very similar to if selection structure when
 //Multiple judgment cycle conditions
 While (conditional expression){
   //Circulation operation / circulation body 
}

//if will only perform judgment once
 If (conditional expression){
    //Execution content
}

A cycle must include cycle condition and cycle operation (cycle body). When meeting a demand, if we consider using cycle to solve it, we must first identify these two parts.

/**
 * Requirements: 50 copies of simulated printing papers
 * Cycle condition: 50 copies after printing
 * Cycle operation: print test paper
 */
public class Demo2 {

	public static void main(String[] args) {

		// Initialize cycle variable
		int i = 1;
		
		// Apply while loop syntax
		// Cycle condition
		while(i <= 50) {
			// Circulatory body
			System.out.println("Printing page"+i+"A test paper!");
			// Update cycle variable [cycle exit] must have this process of updating variables, otherwise the cycle is called a dead cycle
			i ++;
		}
	}

}

The composition of the while loop (four elements):

  1. Initialization of loop variable: it can't be judged without it (writing conditional expression)
  2. Condition of loop: when what condition is satisfied by the loop
  3. Loop operation: the repeated operation that should be performed in the loop
  4. Update of cycle variable: if the variable is not updated, then the variable will not change its value, which will cause the cycle condition to be satisfied all the time and a dead cycle will occur.
/**
 * Requirement: it is required to complete the learning task check to see if it is qualified. If it is qualified, it is required to stop unqualified and continue to complete the learning task.
 * 	Cycle condition: continue if unqualified
 * 	Loop operation: completing learning tasks
 */
public class Demo3 {

	public static void main(String[] args) {
		
		// Use Scanner for qualified input
		Scanner input = new Scanner(System.in);
		System.out.print("Is the learning task of Shi Haoran qualified?(y/n): ");
		// 1. Initialization of loop variables
		String isPass = input.next();
		
		// 2. Cycle conditions
		// Remember first: strings cannot be compared with = = for the same content (problems related to reference data types will be explained later)
		// The content of string comparison is the same: equals()
		while("n".equals(isPass)) {
			// 3. Cyclic operation
			System.out.println("Read the textbook in the morning and program in the afternoon!");
			// 4. Circulation outlet
			System.out.print("Is the learning task of Shi Haoran qualified?(y/n): ");
			isPass = input.next();
		}
		
		System.out.println("Mission accomplished! Level up!");
	}

}

2. Master do while cycle structure (commonly used in the early stage)

// Initializing the loop's variables
do{
    // Cyclic operation
    // Circulation outlet
}while(Cycle condition);
/**
 * Requirement: it is required to complete the learning task check to see if it is qualified. If it is qualified, it is required to stop unqualified and continue to complete the learning task.
 * 	Cycle condition: continue if unqualified
 * 	Loop operation: completing learning tasks
 */
public class Demo1 {

	public static void main(String[] args) {

		Scanner input = new Scanner(System.in);
		
		// Initialize loop variable
		String isPass;
		do {
			// Cyclic operation
			System.out.println("Read the textbook in the morning and program in the afternoon!");
			// Circulation outlet
			System.out.print("Is the learning task of Shi Haoran qualified?(y/n): ");
			// The scope of a variable (the effective scope) the scope of a variable is related to the {} it directly belongs to
			isPass = input.next();
		}while("n".equals(isPass)); // Cycle condition
		System.out.println("Mission accomplished! Level up!");
	}
	
}

What's the difference between while and do while?

Different syntax:

while(Cycle condition){
    // Cyclic operation
}

do{
    // Cyclic operation
}while(Cycle condition);

Different operation time:

while is to judge before executing.

Do while is to execute before judging.

When the first condition fails, while will not execute! Do while will execute at least once whether it is established or not!

3. Master for cycle structure (very common in the later stage)

For loops can only be used for scenes with a fixed number of loops.

for(1;2;3){
    4
}
1: Initializing the loop's variables
2: Cycle condition
3: Update loop variable
4: Circulatory body

1  2  4  3  2  4  3  2  4....

Posted on Tue, 03 Dec 2019 07:50:39 -0500 by kobayashi_one