Java white learning experience and summary of notes (day six)-the difference between break and continue

for loop

Structure:
for (loop initial condition; loop condition judgment; loop condition change)
{loop body}

The advantages of the for loop:
1. The content of the parenthesis after for is more readable
2. The parenthesis after for can be used. The content inside can roughly estimate how many times the entire loop will be executed.
3. The for loop and some special data Very close

Use a for loop to display all data from 1 to 100

class Demo1 {
	public static void main(String[] args) {
		for (int i = 1; i <= 100; i += 1) {
			System.out.println(i);
		}
	}
}

Use a for loop to show all the even numbers between 1 and 100

class Demo2 {
	public static void main(String[] args) {
		for (int i = 1; i <= 100; i++) {
			if (i % 2 == 0) {
				System.out.println(i);
			}
		}
		
		/*
		50次循环(优化)
		*/
		for (int i = 2; i <= 100; i += 2) {
			System.out.println(i);
		}
	}
}

Use a for loop to display all capital English letters

class Demo3 {
	public static void main(String[] args) {
		for (char ch = 'A'; ch <= 'Z'; ch += 1) {
			System.out.println(ch);
		}
	}
}

break

Literal meaning:
break out, break
The function in the code is to break out of the loop structure or switch case structure. The
break keyword can stop the loop during the loop, when the current loop result shows the expected content.

The while loop is used with the break keyword to
show 1 ~ 100
but if the data reaches 50 break, it will be executed! ! !

class Demo4 {
	public static void main(String[] args) {
		int i = 1;
		while (i <= 100) {
			System.out.println(i);
			if (50 == i) {
				System.out.println("循环跳出!!!");
				break;
			}
			i += 1;
		}
	}
}

Use the for loop to demonstrate the break keywords from
1 to 100
to 30. Use break to jump out of the loop

class Demo5 {
	public static void main(String[] args) {
		for (int i = 1; i <= 100; i += 1) {
			System.out.println(i);
			
			if (30 == i) {
				System.out.println("跳出循环");
				break;
			}
		}
	}
}

In the case of nested loops. Use of break keyword

class Demo6 {
	public static void main(String[] args) {
		for (int i = 0; i < 10; i++) {
		
			System.out.println("i : " + i); 
			
			for (int j = 0; j < 10; j++) {
				System.out.print("j : " + j + " ");
				if (5 == j) {
					break;
				}	
			}
			
			System.out.println();
		}
	}
}

3. continue

continue keyword
End the current loop and enter the next loop

while, do while和continue

class Demo7 {
	public static void main(String[] args) {
		int i = 0;
		
		/*
		continue关键字和while循环以及do while一起使用,还要操心
		循环变量修改问题在continue之前,还是之后
		*/
		while (i < 10) {
			System.out.println(i);
			i += 1;
			if (5 == i) {
				System.out.println("Continue执行");
				continue;
			}		
		}
		
		int j = 0;
		
		do {	
			j += 1;
			
			if (5 == j) {
				System.out.println("执行了");
				continue;
			}
			
			System.out.println("没执行");
		} while (j < 10);
	}
}

for loop

class Demo8 {
	public static void main(String[] args) {
		
		/*
		for循环使用continue关键字不会受到影响
		不会跳过循环条件变更
		*/
		for (int i = 1; i <= 10; i++) {
			if (5 == i) {
				System.out.println("continue执行了");
				continue;
			}
			
			System.out.println("for每循环");
		}
	}
}
Published 6 original articles · won 9 · 713 views

Guess you like

Origin blog.csdn.net/qq_43571809/article/details/105645155