Java's flow control statements

1, flow control statements

  Java's main flow control statements comprise three categories: sequential structure, branched structure and cyclic structure.

2, the structure of the order

  Sequence Structure statement is in the order of the code is performed from top to bottom.

public static void main(String[] args){ 
    System.out.println(1); 
    System.out.println(2); 
    System.out.println(3); 
}

Output results:

1

2

3

3, branch structure

  Branch structure including two types: if - else if - else, switch

3.1, if the statement

  First, determine the relationship between the expression to see the result is true or false, if the statement is true on the implementation of the body, if the statement is false does not execute body.

Here Insert Picture Description

3.2, if - else statements

  First, determine the relationship between the expression to see the result is true or false, if the statement is true on the implementation of 1, if a false statement is executed 2.
Here Insert Picture Description

3.3、if – else if – else语句

  First, determine the relationship between the expression 1, to see the result is true or false. If the statement is true on the implementation of 1, if it is false to continue to determine the relationship between the expression 2, to see the result is true or false. If the statement is true on the implementation of the body 2, if it is false to continue to determine the relationship between the expression, to see the result is true or false ... If there is no relationship between the expression is true, then the statement is executed body n + 1.
Here Insert Picture Description

3.4, ternary operator

  :( format of the ternary operator expression): expression returns the contents of the content of expression returns to True as when False?. if statement can be achieved with the ternary operator interchangeable:

public static void main(String[] args) { 
	int a = 10; 
	int b = 20; 
	//定义变量,保存a和b的较大值 
	int max; 
	if(a > b) { 
		max = a; 
	} else { 
		max = b; 
	}
	//可以上述功能改写为三元运算符形式 
	max = a > b ? a:b; 
}

3.5, switch statements

switch(表达式) { 
	case 常量值1: 语句体1; 
	break; 
	case 常量值2: 语句体2; 
	break; 
	... 
	default: 语句体n+1; 
	break; 
}

  Execution process first calculates the value of expression, followed in sequence and comparison case, once the value corresponding to the corresponding statement will be executed, during the execution, it encounters a break will end. Finally, if the case were all expressions and values do not match, it will execute the default statement body part, and the program ends off.
  switch statement, the expression may be a data type byte, short, int, char, enum ( enumeration), may be received after JDK7 string.
  In a switch statement, if the case does not write back break, will appear penetration phenomenon, which is not a value judgment in the case, run directly back, until it encounters a break, or the end of the whole switch.

4, loop structure

  Loop structure includes three types: for loops, while loops, do ... while ... loop

4.1, for circulation

format:

for(初始化表达式①; 布尔表达式②; 步进表达式④){ 
	循环体③ 
}

Execution order 1234 -> 234 -> 234 up until 2 is false.

4.2, while circulation

format:

初始化表达式① 
while(布尔表达式②){ 
	循环体③ 
	布尔表达式④ 
}

Execution order 1234 -> 234 -> 234 up until 2 is false.

4.3, do ... while loop

format:

初始化表达式① 
do{ 
	循环体③ 
	布尔表达式④ 
}while(布尔表达式②);

The execution order of 134 -> 234 -> 234 up until 2 is false.

4.4、break和continue

break: jump out of the entire cycle
continue: out of the current cycle

5, a little arithmetic problem

5.1, the output of the prime numbers less than 200

/**
 * @author RuiMing Lin
 * @date 2020-03-18 13:35
 */
public class Demo1 {
    public static void main(String[] args) {
        int count = 0;      // 定义一个计数器
        for (int i = 2; i <= 200; i++) {     // 对2到200之间的数进行遍历
            boolean flag = true;        // 设置素数的指示器
            for (int j = 2; j < i - 1; j++) {
                if (i % j == 0) {
                    flag = false;      // 如果不是素数则flag为false
                    break;
                }
            }
            if (flag) {        // 如果是素数则输出
                System.out.println(i);
                count++;
            }
        }
        System.out.println("共有" + count + "个素数");
    }
}

5.2, narcissistic number

/**
 * @author RuiMing Lin
 * @date 2020-03-18 13:41
 */
public class Demo2 {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 100; i <= 1000; i++) {
            int ge = i % 10;
            int shi = i / 10 % 10;
            int bai = i / 100 % 10;
            if (ge * ge * ge + shi * shi * shi + bai * bai * bai == i) {
                System.out.println("第" + ++count + "个水仙花数:" + i);
            }
        }
    }
}

Please indicate the wrong place! Thought that it was in trouble if you can give a praise! We welcome comments section or private letter exchange!

Published 33 original articles · won praise 72 · views 10000 +

Guess you like

Origin blog.csdn.net/Orange_minger/article/details/104942533