------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,. NET training </a>, look forward to communicating with you! -------
Program Flow
First, the program flow control
Also called statements, mainly include: judgment structure, choice structure and circulation structure.
1. Judging structure--if
Three forms of the IF statement:
a) if (conditional expression)
{
Execute the statement;
}
b) if (conditional expression)
{
Execute the statement;
}
Else
{
Execute the statement;
}
c) if (conditional expression)
{
Execute the statement;
}
else if (conditional expression)
{
EXECUTE statement
}
......
Else
{
Execute the statement;
}
If statement features:
A, each of these formats is a single statement.
b, the second format differs from the ternary operator: The ternary operator must have a value to appear after the operation. The advantage is that you can simplify if else code.
C, no matter what the conditional expression is written, the final result is either true or false.
2. Select Structure--switch
Switch statement format:
switch (expression)
{
Case takes value 1:
Execute the statement;
Break
Case takes value 2:
Execute the statement;
Break
......
Default
Execute the statement;
Break
}
Switch statement features:
There are only four types of A,switch statement choices: Byte,short,int, Char.
There is no order between b,case and default. Executes the first case, without matching case execution default.
C, the end of the switch statement in two cases: 1, encountered a break end; 2, execution to the end of the switch end.
D, if the matching case or default does not have a corresponding break, then the program will continue execution down, running the statement that can execute until the break is encountered or the end of the switch ends.
Note: JDK1.5 can receive enumeration types later, JDK1.7 can receive strings later.
The IF and switch statements are similar. What is the specific scenario, and which statement to apply? If the exact value of the judgment is not many, and conforms to the four types of byte short int char. Although both statements are available, a switch statement is recommended because it is slightly more efficient. Other cases: Judging the interval, the use of if,if is more extensive when judging the result as a Boolean type.
3. Cyclic structure--while,do while,for
While statement format:
while (conditional expression)
{
Execute the statement;
}
Do While statement format:
Do
{
Execute the statement;
}while (conditional expression);
The difference between the while and do while:
While: The condition is judged first, and the loop body is executed only if the condition satisfies.
Do While: The loop body is executed first, then the condition is satisfied, and then the loop body is resumed.
A simple sentence: Do while: the loop body executes at least once, regardless of whether the condition is satisfied.
For statement format:
for (initialization expression; cyclic conditional expression; post-loop action expression)
{
Execute the statement;
}
Description
A,for inside the order of the operation of a single expression, the initialization expression is read only once, to determine the loop condition, for the real execution of the loop body, and then execute the loop after the operation of the expression, and then continue to judge the loop condition, repeat the process until the condition is not satisfied.
B,while and for are interchangeable, except that variables defined for the purpose of a loop are freed in memory at the end of the For loop. While the variables used by the while loop can continue to be used after the loop ends.
C, the simplest infinite loop format: while (true) {}, for (;;) {}, the non-write conditional expression is ture by default for the for. The reason why an infinite loop exists is not knowing how many times the loop is, but depending on some conditions to control the loop.
When do I use the loop structure?
The loop structure is used when you want to execute a number of statements many times.
Circular Note: Be sure to identify which statements need to participate in the loop and which do not.
4. Other Process Control Statements--break,continue
Break (Jump) statement: Application scope: Select structure and loop structure. (End loop)
Continue (continuation) Statement: Applies to the loop structure. (End this cycle, go to the next cycle)
Second, function
Definition: A piece of independent applet defined in a class that has a specific function. Also called methods.
Format:
Modifier returns a value type function name (parameter type form parameter 1, parameter type form parameter 2, ... )
{
Execute the statement;
return value;
}
which
Return value type: The data type of the result after the function is run.
Parameter type: is the data type of the formal parameter.
Formal parameter: is a variable that stores the actual arguments passed to the function when the function is called.
Actual parameter: The specific value passed to the formal parameter.
return: Used to end the function.
Return value: The value is returned to the caller.
Characteristics:
- Defining a function can encapsulate the function code to facilitate reuse of the function.
- Functions are executed only if they are called.
- The appearance of functions improves the reusability of code.
- For cases where the function does not have a specific return value, the return value type is represented by the keyword void, then the return statement in the function can be omitted if the last line is not written.
Attention:
A) functions can only be called in functions, and functions cannot be defined inside a function.
b) When defining a function, the result of the function should be returned to the caller and referred to the caller for processing.
How do I define a function?
- Define what the final result of the feature is to be defined.
- Determine whether unknown content is required to participate in the operation during the definition of the feature.
An important feature of a function--overloading (override)
Concept: In the same class, more than one function with the same name is allowed, as long as the number of argument lists or the parameter types are different.
Features: Regardless of the return value type, see only the parameter list.
Benefits: Easy to read and optimized for programming.
Example:
Returns the number of two integers and
int add (int x,int y) {return x+y;}
Returns the number of three integers and
int add (int x,int y,int z) {returnx+y+z;}
Returns two decimals and
Double Add (double x,double y) {return x+y;}
When do I use the overload?
When the functions defined are the same, but the unknown contents of the participating operations are different. Then, a function name is defined to represent the function, to facilitate reading, and to distinguish multiple functions with the same name by different parameter lists.
Small exercise:
1 classFunctionTest2{//objective to print the 99 multiplication table3 //Analysis: A dual for loop is used, and no return value4 Public Static voidPrint99 (inttem)5 {6 intx, y;7 for(x=1;x<=tem;x++)//limit the number of rows in the 99 multiplication table8 {9 for(y=1;y<=x;y++)//because only print to 7*7,8*8 and so on, so y<=xTen { OneSOP (x+ "*" +y+ "=" +x*y+ ' \ t '); A } -SOP ("\ n"); - } the } - Public Static voidMain (string[] args) - { - intX=9; +Print99 (x);//x represents how many layers you want to print, and if you want to print only to 8*8,x to 8 - } + Public Static voidsop (Object obj) A { at System.out.print (obj); - } -}
The results of the program run are as follows:
Dark Horse programmer--java Basic Grammar---process control and functions