The jump of the loop of the loop - continue statement

Loops are for efficiency and code readability .

But a well-functioning loop body will give up the loop under certain conditions .

The jump of the loop of the loop - continue statement

The concepts presented in this article are:

label in continue    A label consists of a legal identifier and ":". Such as: outer:                                                                                                           
identifier The names of packages, classes, methods, parameters, and variables in java can be composed of English letters, numbers, underscores (_) or dollar signs ($) in any order, but identifiers cannot start with numbers, and identifiers cannot be keywords .
grammar:

continue label;

where label is an optional parameter         


  

Jump without label:

If the unlabeled continue statement is executed, the current loop will be ended, the remaining unexecuted statements in the loop body will be skipped, and the next loop will be judged according to the loop conditions, or the loop will end. If it is a for loop, expression 3 is also executed first to change the value of the loop control variable . Example: Realize the output of all numbers within 100 that are divisible by 7, and use the continue statement to filter other numbers                                                                                                                                       

public class Test {
	  public static void main(String[] args) {
	        for(int i=0;i<100;i++) {
	        	if(i%7!=0) {
	        		continue;
	        	}
	        	System.out.println(i+"、");
	        }
	    }
}

Jump with labels:

If a labeled continue statement is executed, instead of skipping one cycle of the current loop statement, it skips one cycle of the loop statement specified by the label. Because labels are used to specify loop statements, labels are defined before the loop statement. Labeled loop statements are often used in multiple loops.      

Example title: Using a for loop and a labeled continue statement to output a nine-nine multiplication table

public class Test {
	public static void main(String[] args) {
		outer: for (int i = 1; i < 10; i++) {
			for (int j = 1; j < 10; j++) {
				if (j > i) {
					System.out.println();// 换行
					continue outer;// skip the loop specified by the label and give up the unnecessary multiplier
				}
				String str = j + "*" + i + "=" + j * i;
				System.out.print(str + "\t");// Format output with tabs
			}
		}
	}
}



expand:

Hear the thunder in a silent place

In the unlabeled continue statement, there is a requirement that if it is a for loop, expression 3 must be executed first to change the value of the control loop variable. This sentence may sound relatively inconspicuous. Here is an example to illustrate:

Example: Use Iterater and for loop to output even-numbered elements in a list

So I took it for granted and wrote the following code:

public class IteratorDemo {

	public static void main(String[] args) {
		// TODO auto-generated method stub
		List<Integer> list = new ArrayList<Integer>();// create a list
		for (int i = 0; i < 10; i++) {// add 10 elements to the list
			list.add(i);
		}
		System.out.println("The even-numbered element in the list: ");
		boolean flag=true;
		for (Iterator<Integer> it = list.iterator(); it.hasNext();) {
			flag=!flag;
			if(flag) {
				continue;
			}
			System.out.print(it.next() + " ");
		}
	}
}

The result is that all elements in the list are output, which means that the continue statement does not work, because in this way, there is no expression three in the loop condition of the for loop, so the continue statement is not executed.

The question requires the use of for loops and Iterators to achieve this function, so there is no way to use Boolean variables to complete the separation of odd and even numbers, so the correct code to achieve parity separation in this case is:

public class IteratorDemo {

	public static void main(String[] args) {
		// TODO auto-generated method stub
		List<Integer> list = new ArrayList<Integer>();// create a list
		for (int i = 0; i < 10; i++) {// add 10 elements to the list
			list.add(i);
		}
		System.out.println("The even-numbered element in the list: ");
		for (Iterator<Integer> it = list.iterator(); it.hasNext(); it.next()) {
			System.out.print(it.next() + " ");
		}
	}

}



Guess you like

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