First Lecture Grammatical Structure
1. Judging structure
1) If Else
2) ternary operator:
Benefit: You can simplify if else code;
disadvantage : Because it is an operator, the completion of the operation must have a result;
format: variable = (conditional expression)? Expression 1: Expression 2
Practice:
2. Select structure--switch case break default
1) Writing format:
Switch (expression)//the value type in the expression can only be: byte short int char
{
Case takes value 1:
Execute the statement;
break;
Case takes value 1:
Execute the statement;
break;
。。。。。。
Default
Execute the statement;
Break
}
Features: regardless of where the default is located, it will be executed at the end.
Practice:
2) If and switch comparison
-
- If the exact value of the judgment is not many, but it conforms to the four types of byte, short, int, char, although both statements can be used, but it is recommended to use switch, because its efficiency is slightly higher;
- If the judging range is large, it is recommended to use if;
- For interval judgment, the use of if,if is wider when the result is a Boolean type.
3. Cyclic structure
1) while
Format:
while (conditional expression)
{
Execute the statement;
}
2) Do While
Format:
Do
{
Execute the statement;
}while (conditional expression)
Features: The loop body is executed at least once, regardless of whether the condition is satisfied.
3) for Loop
The loop condition expression must be true or FALSE, the initialization expression and the loop action expression can
Random. When there are many expressions, you can separate them with commas: for (int x=1,y=3; x<4; x++,y++).
4) Comparison of circular statements
while and Do While difference:
- While the condition is first judged, the loop body is executed only if the condition satisfies;
- Do While the loop body is executed first, and then the condition is satisfied, the condition satisfies and then proceeds to execute the loop body;
- Does while the loop body executes at least once, regardless of whether the condition is satisfied;
for and while difference
If this variable is used only to control the number of loops as the loop increment exists, it is best to use for. Because, when the For loop ends, the variables automatically disappear and the memory can be optimized.
5) Looping nested statements
The rows and columns are separated and can be controlled by variables when the row or column count changes as the loop progresses.
Exercise: 9X9 multiplication Table
Note: in a row, the spaces between the different multiplication formulas can be "\ t" to align the upper and lower operators.
4. Process Control statement: Break (jump out) continue (continue)
Break: Can only be used in the selection structure and loop structure--jump out of the current loop and end the loop.
Continue: Applies to the loop structure--ends the loop and continues the next cycle.
Note: the statements below are not executed when both exist separately.
Exercise: printing as a shape
Second lecture Functions Overview & Application
1. Function definition
1) Overview: A function is a separate applet defined in a class with a specific function, and the function becomes a method.
2) function Format:
Modifier return value type function name (parameter type form parameter 1, parameter type parameter 2)
{
Execute the statement;
return value;
}
2. Function characteristics
1) Function code can be encapsulated;
2) Facilitate the reuse of this function;
3) function is executed only if it is called;
4) The appearance of the function improves the reusability of the Code;
5) If the function does not have a specific return value, the return value type is denoted by the key value void, then the return statement in the function can be omitted if the last line is not written. Note: The constructor does not return a value, there is no need to add a return value type, void is not added.
Note: functions can only call functions, and other functions cannot be defined inside a function. The result of the function is returned to the caller.
3. Function application
function, the run result of the called function is returned to the caller.
4. Function overloading
1) Overload concept
In the same class, more than one function with the same name is allowed, as long as the number of arguments or the type of argument is different.
2) heavy-duty features
Regardless of the return value type, see only the parameter list (number of parameters and parameter type).
3) Reload Benefits
Convenient and reading, optimized the program design.
4) Example
The following three functions are overloaded:
5) When to use overloading
When the function defined is the same, but the unknown content of the participating operation is not the same. At this point, you can define a function name that represents its function and is easy to read. You can differentiate multiple functions with the same name by using different parameter lists.
Summary of Knowledge points
1, there are two kinds of judgment structure: if else and ternary operation, when the result of the operation has a return value, both can be used; when there is no return value, only if else is used.
2. There are two types of selection structure: if else and switch.
1) If the value of the judgment is not many, and is a byte, short, int, char of several types, it is recommended to use switch, its efficiency is high;
2) If the interval is judged, or if the result of the expression is Boolean, it is recommended to use the IF statement;
3, the loop structure has three kinds: for, while and do while
4. Conditions for overloaded functions
Function names are the same, parameter lists are different. The list of parameters includes two contents: number of parameters and parameter type.
Dark Horse Programmer-java Basics-Statements & Functions