# Experience day3-8

Posted by jikishlove on Fri, 25 Feb 2022 07:33:49 +0100

## day3 basic if statement

### 3.1 if conditional branch statement,

The expression in if should be Boolean. There are three different options. (suppose there will be data processing in if)

First, only if statements are used, which means that I only filter the data I want;

Second: if Else statement, no matter entering the if or else branch, will always return a data to me;

Third: if... else if... else statement, which is multi conditional branch judgment. It will also return a data to me, but it may need to judge several layers of branches.

## Calculation of day4 leap year

### 4.1 thoughts: what is leap year?

(1) Non century years (years that cannot be divided by 100) are leap years that can be divided by 4 and cannot be divided by 100

(2) A leap year is a century year that can be divided by 400

This paper gives two methods to calculate leap years. Combined with the if statement of day3, the first method completes all logical judgments in one if, but the second method is more ingenious. Using an else if can skillfully simplify a large series of logical judgments in the first method.

```/**
* method1
* @param paraYear particular year
* @return boolean
*/
public static boolean isLeapYear(int paraYear) {
if ((paraYear % 4 == 0) && (paraYear % 100 == 0) || (paraYear % 400 == 0) ) {
return true;
}else {
return false;
}
}

/**
* method2
* @param paraYear
* @return boolean
*/
public static boolean isLeapYearV2(int paraYear) {
if (paraYear % 4 != 0) {
return false;
}else if (paraYear % 400 != 0) {
return false;
}else {
return true;
}
}```

## day5: basic switch statement

### 5.1 switch is also a conditional branch statement

The value of the expression in the switch matches the value after the case. If the match is correct, the code needs to be executed after execution, and the execution ends in case of a break. If there is no case match, default will be executed finally, and the default branch does not need a break statement

### 5.2 thinking 1

Do you have to break after every case? The answer is No. Without break, you will jump to the corresponding case and execute all the following statements.

### 5.3 thinking 2

What is the difference between switch and if conditional statements? The most obvious difference is the execution structure. The expression result in if can only be boolean type, while switch is just the opposite. The result of his expression can be int, char, etc. In my actual use, I use more if statements, but when it comes to judging more if branches, I will test the rate of using switch, so that the efficiency will be higher.

## day6: basic for statement

### 6.1 execution order of expressions in for statement

for(a;b;c) where a, b and C are expressions. Execution order: execute the a expression first, generally the initialization statement, and then the b expression. Generally judge the expression. If it is true, execute the loop body, and then execute the C expression after execution. If it does not meet the b expression, jump out of the loop.

## day7: matrix element addition

### 7.1 topic interpretation

The matrix is stored in a two-dimensional array, the sum of two-dimensional arrays is calculated, and the corresponding rows and columns of two-dimensional arrays are added to form a new two-dimensional array. for loop traversal is required (row first); The assignment of the matrix also needs to cycle through the initial value. When there is a cycle, it is necessary to avoid dead cycle and ensure that the cycle is finite.

### 7.2 two dimensional array

```int[][] tempMatrix = new int;
tempMatrix.length; //Represents the length of the line
tempMatrix.length； //Represents the length of the column```

### 7.3 thinking

In the process of this code, whether the initial value of the matrix is extracted as a method can be called many times, but the disadvantage is that the initial value of the matrix is not flexible enough.

``` /**
* Matrix initialization
* @param row Row length
* @param column Column length
* @return
*/
public static int[][] initialMatrix (int row, int column) {
int[][] tempMatrix = new int[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
tempMatrix[i][j] = i * 10 + j;
}
}
return tempMatrix;
}

public static void matrixElementSumTest1() {
int[][] tempMatrix = new int;
for (int i = 0; i < tempMatrix.length; i++) {
for (int j = 0; j < tempMatrix.length; j++) {
tempMatrix[i][j] = i * 10 + j;
}
}

System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "\r\n");
}

public static void matrixElementSumTest2() {
int[][] tempMatrix = initialMatrix(3, 4);

System.out.println("The matrix is: \r\n" + Arrays.deepToString(tempMatrix));
System.out.println("The matrix element sum is: " + matrixElementSum(tempMatrix) + "\r\n");
}```

## day8: matrix multiplication

### 8.1 topic interpretation

Matrix multiplication (only the columns of the first matrix are equal to the rows of the second matrix): a matrix (m*n) and b matrix (n*p) can be multiplied, and the multiplied matrix is m*p. Therefore, matrix multiplication can only be carried out under certain conditions, and if judgment is needed.

### 8.2 main code ideas of matrix multiplication

``` //The value corresponding to row i and column j of resultMatrix is equal to the sum of row i of paraFirstMatrix multiplied by column j of paraSecondMatrix
int[][] resultMatrix = new int[m][p];
for (int i = 0; i < m; i++) {
for (int j = 0; j < p; j++) {
for (int k = 0; k < n; k++) {
resultMatrix[i][j] += paraFirstMatrix[i][k] * paraSecondMatrix[k][j];
}
}
}```

## summary

1. Conditional branch statement: if Else statement; switch case statement; Combined with leap year calculation, comprehensive use of if, else if, else

2. Loop statement: for loop, common single-layer, double and three-layer loop examples

3. Comprehensively use conditional branch statements and circular statements to complete matrix multiplication

Conditional statements and circular statements are often used in development. When using conditional statements, you should pay attention to judge whether the logic of the conditions is correct. When using circular statements, when multiple loops appear, you need to consider whether there are other better methods to replace multiple loops, because the more the number of loops, the lower the efficiency. At the same time, avoid dead circulation.

Topics: Java