Day08——Circular structure

Day08——Circular structure

The program statement of the sequence structure can only be executed once. If you want to perform the same operation multiple times, you need to use a loop structure.

There are three main loop structures in Java:

  • while loop
  • do...while loop
  • for loop

An enhanced for loop, mainly used for arrays, was introduced in Java5 .

while loop

While is the most basic loop. While statement format:

while( 布尔表达式 ){
    
    
    //循环内容
}

When the Boolean expression is true, the contents of the while loop will be executed until the Boolean expression is judged to be false and stop.

In a few cases, the loop needs to be executed all the time, such as the server's request response monitoring.

If the loop condition is always true, it will cause an infinite loop [infinite loop]. In our normal business programming, we should try to avoid the infinite loop. The infinite loop will affect the performance of the program or cause the program to freeze and crash!

Example 1: Find 1+2+3+… +100=?

public static void main(String[] args){
    
    
    int i = 0;
    int sum = 0
        while(i<100){
    
    
            i++;
            sum = sum + i;
        }
    System.out.println("Sum = " + sum );
}//Sum = 5050

do...while loop

Do...while statement format:

do{
    
    
    //循环内容
}while(布尔表达式);

When the Boolean expression is false, the while statement will not execute the loop content, but the do...while statement will execute the loop content once, that is, execute it once, and then judge the Boolean expression.

Example 2:

//例1代码段4~7改为如下
do{
    
    
	i++;
	sum = sum + i;
}while(i<100);   //5050

for loop

The for loop statement is a general structure that supports iteration, and is the most effective and flexible loop structure.

For loop format:

for(初始化; 布尔表达式 ; 迭代){
    
    
    //循环内容
}

【扩展】缩写:100.for = for (int i = 0; i < 100; i++) {}

Example 3:

for (int i = 0; i <= 100; i++) {
    
    
    sum = sum + i;
}

As can be seen from the upper code, the for loop puts the initialization of i and the iterative statement of i in while and do...while together, so the loop structure becomes simpler.

Example 4: Output a number divisible by 5 between 1-1000, and output 3 per line.

int j = 0;
for (int i = 0; i <= 1000; i++) {
    
    
	if(i%5==0){
    
    
		j++;//判断输出了几个数
		if(j%3==0){
    
    
			System.out.println(i);//println打印出i并且换行
		}else{
    
    
			System.out.print(i+"\t");//print不换行,\t为转义字符tab,用于分隔两相邻数据
		}
	}
}

Example 5: Print the nine-nine multiplication table

for (int i = 1; i <= 9; i++) {
    
    
	for (int j = 1; j <= i; j++) {
    
    
		System.out.print(i+"*"+j+"="+i*j+"\t");
	}
	System.out.println();
}
/*
1*1=1	
2*1=2	2*2=4	
3*1=3	3*2=6	3*3=9	
4*1=4	4*2=8	4*3=12	4*4=16	
5*1=5	5*2=10	5*3=15	5*4=20	5*5=25	
6*1=6	6*2=12	6*3=18	6*4=24	6*5=30	6*6=36	
7*1=7	7*2=14	7*3=21	7*4=28	7*5=35	7*6=42	7*7=49	
8*1=8	8*2=16	8*3=24	8*4=32	8*5=40	8*6=48	8*7=56	8*8=64	
9*1=9	9*2=18	9*3=27	9*4=36	9*5=45	9*6=54	9*7=63	9*8=72	9*9=81	
*/
  • Enhanced for loop

    Java5 introduced an enhanced for loop mainly used for arrays or collections .

    int[] numbers={
          
          10,20,30,40,50};//创建一个数组numbers,并赋值
    	for(int x:numbers){
          
                  //遍历数组中的元素,赋值给x
    		System.out.println(x);
    }//10	20	 30	   40  	50
    

    See the Array chapter for details.

break & continue

1. Break keyword

Break is mainly used in loop statements or switch statements to jump out of the entire statement block .

In the double loop nested statement, jump out of the inner loop.

2, continue keyword

continue applies to any loop control structure. The function is to make the program immediately jump to the next iteration of the loop, that is, end this loop, and perform the next cycle of judgment Boolean expressions.

The difference between break & continue:

  • Break is used to forcibly exit the loop without executing the remaining statements in the loop.
  • The continue statement is used in the loop statement body to terminate a loop process, that is, skip the statement that has not been executed in the loop body, and then determine whether to execute the loop next time.

[Extension] Print triangle

public static void main(String[] args) {
    
    
	for (int i = 1; i <= 5; i++) {
    
      //i控制行数,i=1即第一行
		for (int j = (9-(2*i-1))/2 ; j >= 0 ; j--) {
    
    
			System.out.print(" ");  //j控制空格数,即此行需要多少个空格
		}
		for (int k=2*i-1; k > 0 ; k--) {
    
    
			System.out.print("*");  //k控制*数,即此行需要多少个*
		}
		System.out.println();       //换行
	}
}/*
     *
    ***
   *****
  *******
 *********
*/

Guess you like

Origin blog.csdn.net/weixin_46330563/article/details/114908622