Dabai became the tenth day of the Java software siege lion (loop structure, for control statement)

For control statement in Java

1. Loop structure

In the program, there are always some code that needs to be executed repeatedly. Assuming that there is no loop structure, the code that needs to be executed repeatedly naturally needs to be written repeatedly. The code cannot be reused. So most programming languages ​​support loop structure. In the future, put the code fragments that need to be executed repeatedly into the "loop body", and then combine the "counter" to jointly control the code that needs to be executed repeatedly.

Basically all programming languages ​​support three types of loops:

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

2. For loop

1 Grammatical structure :

 for (初始化表达式 ; 布尔表达式 ;更新表达式) {
    
    
 
循环体:由java语言构成,是需要重复执行的代码片段

 }

2 The execution process/principle of for loop?

2.1 Initialization expressions, Boolean expressions, and update expressions are not required! [But two semicolons are required]

2.2 The initialization expression is executed first, and only once in the entire for loop.

2.3 Boolean expressions must be true/false and cannot be other values.

2.4 The execution process of for:

1. The initialization expression is executed first, and the expression is executed only once.

2. Determine whether the result of the Boolean expression is true or false:

  • The Boolean expression is true:
    1. Execute the loop body
    2. Execute the update expression
    3. Determine whether the result of the Boolean expression is true or false
    Boolean expression true
    1. Execute the loop body
    2. Perform the update expression
    3. Determine the Boolean expression Is the result true or false
    ...
  • Boolean expression is false and the
    loop ends

Nesting of loop statements and conditional judgment statements [nesting of for and if]

Find all odd numbers from 1 to 100

public class ForTest01
{
    
    
	public static void main(String[] args){
    
    
		第一种方法
		for(int i = 1;i<=100;i+=2){
    
    
			System.out.println("奇数-->" + i);
		}
		
		第二种方法
		for(int i=1;i<=100;i++){
    
    
			if(i%2 != 0){
    
    
				System.out.println(i)
			} 
		}	
	}
}

Find the sum of all odd numbers from 1 to 100

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

3 for loops can also nest for loops

The variable name in the inner loop and the variable name in the outer loop cannot be the same

Use a for loop to output the nine-nine multiplication table

public class ForTest03
{
    
    
	public static void main(String[] args){
    
    
		for(int i=1;i<=9;i++){
    
    
			for(int j=1;j<=i;j++){
    
    
				int  a=i*j;
				System.out.print(i +"*"+j+"="+a );
			}
			System.out.println(); //换行
			//System.out.print("\n");
		}
	}
}

Write a for loop to find all prime numbers from 1 to 100

Prime number: also known as prime number, can be divisible by 1 and itself, and the number that cannot be divisible by other numbers is called prime number.

public class ForTest04 {
    
    

    public static void main(String[] args) {
    
    
        for(int i=2;i<=100;i++){
    
    
            boolean flag=true; //标记i是否能被其他数整除
            for(int j=2;j<i;j++){
    
     //此刻i只能被1和本身整除,所以应从2~i-1
                if(i%j==0){
    
     
                    flag=false;  //如果i能被其他数整除,就跳出循环
                    break;
                }
            }
            if (flag)
            System.out.print(i+" ");
        }
    }
}

Guess you like

Origin blog.csdn.net/qq2632246528/article/details/112607654