Java programming task-driven tutorial study notes three

Table of contents

1. Select statement

1. if conditional statement

2. Switch conditional statement

Second, the loop statement

1. while loop statement

2. do---while loop statement

3. for loop statement

Three, jump statement

1. Break statement

2. continue statement

3. return statement

4. Compound statement and block function

Five, ternary operator?

Case 1: Judging odd and even numbers

 Case 2: Judging whether 2008 is a leap year

 Case 3 Judging the month based on the number

Case 4 guessing game

 Case 5: Output ninety-nine multiplication table


1. Select statement

1. if conditional statement

(1) Simple if conditional statement

if(条件语句){
  代码块
}

Judgment condition: Boolean value, when the value is true, the statement in { } will be executed.

if-else statement

The if-else statement means that if a certain condition is met, a certain processing is performed, otherwise another processing is performed.

Format:

if(判断条件){
    执行语句
}else{
    执行语句
}

Multi-branch if-----else if------else statement

if(判断条件1){
   执行语句1
}else if(判断条件2){
   执行语句2
}
-------
else if(判断条件n){
   执行语句n
}else{
     执行语句n+1
}

2. Switch conditional statement

The Switch statement is a multi-branch switch statement, and the output statement is executed according to the value of the expression.

break is used to end the Switch statement.

Format:

switch(表达式){
    case 目标值1:
          执行语句1
          break;
     case  目标值2:
          执行语句2
          braek;
----------------
     case  目标值n:
          执行语句n
           break;
      default:      //用于非法数值的结束(可以省略,不推荐)
       执行语句n+1
       break;
}

The value of case must be a constant, the value of case is not allowed to be repeated, and multiple cases can be merged together.

The function of break is to terminate the execution of the Switch statement. If there is no break, when the program executes the matching case clause, the following case clause will be executed.

Second, the loop statement

1. while loop statement

Format:

while(循环条件){
      执行语句
       ------
}

The while statement is a looping conditional expression to control the looping statement. The loop condition expression is used to judge whether to execute the loop, and the value is true or false. When the loop starts, the loop condition expression will be executed first. If the value is true, the loop body is executed until the loop condition is false, and the loop ends.

2. do---while loop statement

Format:

do{
     执行语句
     -------
}while(循环条件)

The do----while loop first executes the loop body once, and then judges whether to continue execution according to the condition.

3. for loop statement

Used when the number of cycles is known

Format:

for(初始化表达式(1);循环条件(2);操作表达式(3)){
    执行语句(4)
     -----
}

Step 1: Execute 1

Step 2: Execute 2, the judgment condition is true, execute the third step, if the judgment result is false, execute the fifth step

Step 3: Execute 4

Step 4: Execute 3, and then repeat Step 2.

Step 5: Exit the loop

Three, jump statement

The jump statement is used to realize the jump of the program flow during the loop execution

1. Break statement

The break statement can terminate the loop or other control structures. As long as the break statement is executed, the execution of the loop body will be terminated. The break is not only applicable in the loop statement, but also in the Switch multi-branch statement.

When the break statement appears in the inner layer of a nested loop, it can only jump out of the inner loop. If you want to jump out of the outer loop, you need to mark the outer loop.

2. continue statement

The continue statement, if the continue statement is executed in the execution of a certain loop body, then this loop ends, and the statement after the continue statement in this loop is not executed, and the next loop is performed.

3. return statement

The return statement returns from a method and passes control to the statement that called it. The return statement is usually placed at the end of the method to exit the current method and return a value.

(1) The grammatical format of the return statement

return[expression];

(2) Two functions of the return statement;

1. End the execution of the method and return to the caller.

2. Bring back the return value of the method

4. Compound statement and block function

A compound statement is a number of simple sentences enclosed in a pair of curly braces

Five, ternary operator?

Format:

判断条件?表达式1:表达式2

The ternary operation is usually used to assign a value to a variable. When the judgment condition is true, the operation result is the value of expression 1, otherwise the result is the value of expression 2.

Case 1: Judging odd and even numbers

 

 Case 2: Judging whether 2008 is a leap year

 Case 3 Judging the month based on the number

package example;

public class example3 {
	public static void main(String[] args) {
		int month=6;
		switch(month) {
		case 1:
			System.out.println("January");
			break;
		case 2:
			System.out.println("February");
			break;
		case 3:
			System.out.println("March");
			break;
		case 4:
			System.out.println("April");
			break;
		case 5:
			System.out.println("May");
			break;
		case 6:
			System.out.println("June");
			break;
		case 7:
			System.out.println("July");
			break;
		case 8:
			System.out.println("August");
			break;
		case 9:
			System.out.println("September");
			break;
		case 10:
			System.out.println("October");
			break;
		case 11:
			System.out.println("November");
			break;
		case 12:
			System.out.println("December");
			break;
		default:System.out.println("非法数值");
		}
	}

}

Case 4 guessing game

 

 Case 5: Output ninety-nine multiplication table

Guess you like

Origin blog.csdn.net/TAO1031/article/details/121689432