Java grammar foundation (2)

flow control statement

  expression statement

  Adding ; after the expression constitutes an expression statement, referred to as "statement" When we write java code, we are writing expression statements more often. int i; declaration statement i = 10; assignment statement

  flow control statement

  The flow control statement mainly realizes the control of the execution flow of the code, such as selecting, looping, turning, and returning. According to the impact on the code execution process, we generally divide the flow control statement into three structures: Sequence structure: The program defaults to execute code sequentially from top to bottom; it is like driving on a straight road. Branching structure (choice structure): Makes the code choose to execute according to the logical situation, as if encountering an intersection. Including: if statement, if...else statement, switch statement Loop structure: make the code execute in a loop, as if lost, drive around the roundabout until you remember. Including: while loop statement, do...while loop statement, for loop statement

  if statement, if...else statement

  The if statement and the if...else statement belong to the flow control statement of the branch structure. The expressions in the if statement are required to be boolean values. if statement: The if statement is used to judge whether the condition is satisfied (true/false), and decide whether to execute the statement according to the judgment result. The code structure of the if statement: if (conditional expression){ execute the statement; } Code execution flow: 1 Determine whether the expression is true (true/false) 2 If the expression is true, execute the "execution statement"; otherwise, skip the if statement, The code below the if statement is executed in a sequential structure. For example, the main items for judging prime numbers are: If the execution code is only one line, the curly brackets can be omitted. If the curly braces are omitted, the if statement only acts on the statement following it. Even if it is followed by an empty statement. {} represents a local code block, which represents a collection of multiple statements. if...else statement if...else statement is used to judge whether the condition is satisfied (true/false), and decide whether to execute statement 1 or statement 2 according to the judgment result. Choose one to execute. if...else statement code structure: if(conditional expression){ execute statement 1; }else{ execute statement 2; } code execution flow: 1 Determine whether the expression is true; if it is true, execute "execute statement 1", Then end the if...else statement, according to the sequence structure, execute the following code 2 If the expression does not hold, execute "execute statement 2", then end the if...else statement, according to the sequence structure, execute the following code if.. The logic of .else statement is very similar to that of ?: ternary expression, so how to choose in actual development: Features of ternary expression: The structure is simple; but in java, the entire expression must return a result. Therefore, in a ternary operation expression, if the execution statement is a statement such as println that does not return a result, the compiler will report an error. if...else if...else statement: The if...else if ...else statement determines which condition is met and executes that statement. It is a multi-choice one implementation method. The code structure of if...else if...else statement: if(expression 1){ execute statement 1; }else if(expression 2){ execute statement 2; }else if(expression 3){ execute statement 3; } ... else{ else execute statement; } Code execution flow: 1 Determine whether expression 1 is true; if it is true, execute "execute statement 1", then end the if...else if...else statement, and execute the following according to the sequence structure code. 2 If expression 1 is not established; judge whether expression 2 is established; if it is true, execute "execute statement 2", then end the if...else if...else statement, and execute the following code according to the sequence structure. And so on; 3 if none of the expressions hold. Execute the "else execution statement", then end the if...else if...else statement, and execute the following code according to the sequence structure.

  switch statement

  The switch statement belongs to the flow control statement of the branch structure. The main purpose is to achieve multiple selection one operation. The code structure of the switch statement: switch(expression){ case value 1: execute statement 1; break; case value 2: execute statement 2; break; case value 3: execute statement 3; break; ... default: default execute statement ; } switch statement requirements: The value of the switch expression can be byte, char, short, int, String. String is started after jdk7.0. Value 1, value 2, value 3, must be a constant switch without a case statement, preferably with the break keyword, otherwise it will run through the execution. The code execution flow of the switch statement: 1 Calculate the value of the expression first 2 According to the value of the expression, match the value 1; if it matches, execute "execute statement 1", if there is a break after it, jump out of the switch statement; Execute statement 2", "execute statement 3", "default execution statement"; 3 If value 1 does not match, match value 2; if it matches, execute "execute statement 2", if there is a break after it, jump out of the switch statement; otherwise Run through "execute statement 3", "default execution statement"; 4 If the value 2 does not match, match the value 3; if it matches, execute "execute statement 3", if there is a break after it, jump out of the switch statement; otherwise, run through the execution of " default execution statement"; 5 If none of the case values ​​match, then execute the default statement. According to the position of the default statement, it is judged whether to run through the execution. The logic of the switch statement can also be implemented using the if else if else statement.

  while loop

  The while statement belongs to the flow control statement of the loop structure. Mainly controls the repeated execution of the code. The code writing format of the while statement: while(conditional expression){ The statement executed by the loop; } The conditional expression returns a value of type boolean. Code execution flow: 1. Determine whether the expression is true. If it is true, execute the "loop execution statement"; otherwise, end the while loop. 2 Repeat this: If the expression directly writes the variable ture, the code will always execute the "loop execution statement" in a loop. It belongs to the "dead loop". In the console, if you want to end the infinite loop, you need to end the program running, ctrl+c can only execute a limited number of loops as needed for the while loop statement, we usually use variable control. int i = 0; while(i<10){ execute statement; i++; } If there is only one loop statement, the curly braces can be omitted. If the curly braces are omitted, the while statement only works on the following sentence, even if it is an empty statement.

  do...while loop

  The code writing format of the do...while statement: do{ The statement executed by the loop; }while(conditional expression); The conditional expression returns a value of type boolean. Code execution flow: 1 First execute the "loop execution statement" 2 Determine whether the expression is true, if true, execute the "loop execution statement"; otherwise, end the while loop 3 Repeat the difference between it and the while statement: No matter whether the condition is true, the loop executes The statement is always executed once. More often, we will use the while loop statement. It is more in line with our logical judgment habits.

  for loop

  The for loop statement belongs to the loop flow control statement. Code writing structure: for(initialization statement; judgment statement; statement executed after each loop) { loop statement; } initialization statement: the statement that is executed first after entering the for loop. During the entire for loop, it is executed only once. Judgment statement: Judging whether the condition is established, if so, continue to execute the for loop; otherwise, end the for loop and execute the "statement executed after each loop" after each loop is executed. Code execution flow: 1 Enter the for loop and execute "initialization" Statement" 2 Judge "whether the judgment statement is true", if not, end the for loop directly 3 If the "judgment condition" is true, execute the "loop statement", and execute the "statement executed after each loop" 4 Continue step 2 until it jumps out of the for cycle. Note: 1 for( ; ; ) is an infinite loop 2 The initialization statement can be written above the for loop 3 The statement executed after each loop can be written after the loop statement.

  transfer character

  In java, some characters have special meanings, such as ",', \ etc. In order to use these special characters in programming, we need to use escape characters

  break,continue keyword

  break, end the current loop; or prevent the switch statement from running through continue, ending the current loop

 

function

  function understanding

  Function: Code that encapsulates functions separately in order to improve the reusability of the code. For example, we sum 2+3, 4+5, 6+7. If we do not use functions, we need to constantly create variables and use code to sum. With functions, it is easy to implement.

  Function code writing format

  Function declaration modifier return value type function name (parameter type formal parameter 1, parameter type formal parameter 2, ...) { code to be executed; return return value; } If you want the function to be executable, it must be called. The calling function name of the function (actual parameter 1, actual parameter 2, ...); Modifier: used to modify the access rights, staticity, etc. of the function. Return value type: The type of data that the function eventually returns. Function name: The name of the function, specified by the user, it must conform to the requirements of the java identifier. Formal parameter list: a local variable belonging to the function, used to accept the parameters passed by the caller. Actual parameters: the specific data passed when calling. Return value: The specific operation result of the function.

  Steps to define a function

  1 First determine what kind of function the function performs 2 Determine the type of value you want the function to return in the end 3 Determine which parameters the function requires and the types of the parameters 4 Name the function so that it is familiar

  Function example 1 Write a function to find the sum of two integers of type int. Write a function to find the largest value of two int variables

  void return keyword

  If the function does not need a return value, when declaring the function, the return value type can use void. When the return keyword is executed, the entire code is executed, regardless of whether there is any other code behind the return. The function of the return keyword: 1 Return the result to the caller. 2 End the execution of the function.

  Features of the function

  1. The function code can be encapsulated by the function. 2. The code can be reused. 3. The function needs to be called to execute. 4. If there is a return value, use return to return directly. You can also leave out the return statement.

  function overloading

  Function overloading: If in a class, two or more functions have the same function name, but different parameter numbers or parameter types, we call it function overloading. The meaning of overloading: In order to make the function call more flexible and convenient, optimize the design of the program.

  Overloaded example: Write a function to find the sum of two variables of the same type, which can be int type, float type

 

array

  what is an array

  Array: A collection of data of the same data type. Using arrays can help us store and manage multiple data of the same type in a convenient and orderly manner. Features: Use the index to set or query the value of a variable.

  How to declare an array

  Dynamic declaration method: type[] array variable name = new type[number of elements]; int[] arr = new int[5]; static declaration method: type[] array variable name = new type[]{element list} ; int[] arr = new int[]{1,2,3,4,5}; Simple form: type[] array variable name = {element list}; int[] arr = {1,2,3,4 ,5};

  memory map of arrays

  The existence form of the array in the memory, the drawing indicates that once the array is created in the memory, the length cannot be changed.

  Array element access

  Array indexing (footer): Arrays are ordered and elements of the array can be accessed through the array's sony. The index is the position number of the element in the array, starting from 0 to the number of elements - 1. Array code format for accessing elements by index Array variable name [index] Set/modify the value of the corresponding element Array variable name [index] = constant; arr[1] = 10; Get the value of the element: Array variable name [index] println( arr[1]);

  Traversing the elements of an array

  Accessing each element in the array in turn, we call it "element traversal" In the code, we can use the loop and the subscript to change the elements in the array. public static void main(String[] args) { int[] x = { 1, 2, 3 }; System.out.println(x[3]); //java.lang.ArrayIndexOutOfBoundsException }

  Array FAQ

  1 The index of the array is out of bounds The index of the array is from 0 to the number of array elements minus 1; do not let the index exceed this range 2 Null pointer exceptions In java, except for the eight basic data types that are value types, all others are reference types. Arrays are reference types, and when the array object is null, the elements cannot be accessed.

  Sorting an array, finding

  Sorting of arrays: 1 Bubble sort 2 Searching for selection sorted arrays: Binary search

  selection sort

  Bubble Sort

  Use of Arrays tool class

  Arrays is a class that exists in the java.util package. It has many class methods that can help us deal with array objects. Traversal: toString() Returns the elements of the array in the form of strings Sort: sort() Sorts the array in ascending order Search: binarySearch() Finds the specified element in the specified array, returns the index of the element, if not found, returns (-insert Point -1) Note: When using the search function, the array must be sorted first.

  Two-dimensional array

  Two-dimensional array: The essence of two-dimensional array is the declaration method of one-dimensional array Two-dimensional array Dynamic declaration: type[][] array variable name = new type[number of two-dimensional elements][number of one-dimensional elements]; static declaration : Type[][] Array variable name = {{element list} , {element list} , ...}; Memory map of two-dimensional array The traversal of two-dimensional arrays uses loop nesting, with indexes, and traversal. Summation of two-dimensional arrays traversal summation

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324641708&siteId=291194637