------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------
This article mainly introduces the process control in the Java Basic grammar, and the process control is the basis for us to write flexible code. There are four main types of process Control: judging structure, selecting structure, circulating structure and other control structures.
I. Structure of judgment
The symbol of the judging structure is the IF statement, and the IF statement has three main forms of expression:
1. if (conditional expression)
{
EXECUTE statement
}
Attention:
1, if there is only one statement in the IF statement, then you can not write curly braces. But beginners must write parentheses to avoid mistakes.
2, if the IF statement does not write curly braces, if can only control the nearest single statement.
2. if (conditional expression)
{
Execute the statement;
}
Else
{
Execute the statement;
}
The ternary operator is shorthand for the if-else format, but the ternary operator is an expression that produces concrete results, so it can be abbreviated as a ternary operator when the IF-ELSE structure has specific results.
3. if (conditional expression)
{
Execute the statement;
}
else if (conditional expression)
{
Execute the statement;
}
Else
{
Execute the statement;
}
We can write a program to deepen our understanding of the IF-ELSE structure: print the season to which the month belongs, based on the specified month.
1 classOperatordemo2 {3 Public Static voidMain (string[] args)4 {5Ifdemo (9);6 }7 Static voidIfdemo (intmonth)8 {9 //There are four seasons in a year, spring 3-5, Summer 6-8, Autumn 9-11, winter 12-2Ten if(month<1| | Month>12) OneSystem.out.println ("No" +month+ "month, this month!) "); A Else if(month>=3&&month<=5) - { -System.out.println (+month+ "Month is spring! "); the } - Else if(month>=6&&month<=8) - { -System.out.println (+month+ "Month is summer! "); + } - Else if(month>=9&&month<=11) + { ASystem.out.println (+month+ "Month is autumn! "); at } - Else - { -System.out.println (+month+ "Month is winter! "); - } - } in}
The result of the output is:
II. Structure of judgment
The symbol for judging the structure is switch
Format:
switch (expression)
{
Case takes value 1:
Execute the statement;
Break
Case takes value 1:
Execute the statement;
Break
Default:
Execute the statement;
Break
}
Features of the switch statement:
1. The switch statement chooses only four types: Byte,short,int,char.
2. Case and default have no order. Executes the first case, without matching case execution default.
3. Two cases of ending the switch statement: ① encounters Break,② execution to the end of the switch statement.
4, if the matching case or default does not have a corresponding break, then the program will continue to execute down, run the statement can be executed until the break or the end of the switch is encountered.
5. After entering the switch statement, the execution order is to execute the case first, then from top to bottom, then the default is executed. Even if default is placed on a case, the order of execution is not changed.
Also use switch to demonstrate the month of the season to get the topic:
1 Static voidSwitchdemo (intmonth)2 {3 Switch(month)4 {5 Case3:6 Case4:7 Case5:8System.out.println (+month+ "Month is spring! ");9 Break;Ten Case6: One Case7: A Case8: -System.out.println (+month+ "Month is summer! "); - Break; the Case9: - Case10: - Case11: -System.out.println (+month+ "Month is autumn! "); + Break; - Case12: + Case1: A Case2: atSystem.out.println (+month+ "Month is winter! "); - Break; - default : -System.out.println ("No" +month+ "month, this month!) "); - Break; - } in}
Defines a static function to be implemented, in order to output in the main function is convenient, the result of the output is:
For several fixed values, it is recommended to use a switch statement because the switch statement loads the specific answer into memory, which is relatively efficient
Third, the cycle structure
The key words of cyclic structure are: while, do-while, for;
1. The structure of the while statement:
while (conditional expression)
{
Execute the statement;
}
Be careful not to write while (true), such a statement, followed by a semicolon is the loop body, which means no statement execution, the loop is a dead loop, followed by {EXECUTE statement;} becomes a local code block
2. The format of the Do-while statement:
Do
{
Execute the statement;
}while (conditional expression)
Dowhile Statement Features: The loop body executes at least once, regardless of whether the condition is satisfied. While if the condition is not met, the loop body will not execute at once.
3, the format of the for statement;
for (initialization expression; loop condition expression; post-loop action expression)
{
Execute the statement;
}
For the three expressions that run in the order, the initialization expression is read only once, the loop condition is judged, the loop body is executed, and then the operation expression after the loop is executed, and then the loop condition continues to be judged, and a process is repeated until the condition is not satisfied.
The next step is to introduce a more complex for loop, which is a for loop nesting.
In the simplest case, the loop operation is also performed. In life is very common, such as 9*9 multiplication formula, two multipliers are increased from 1 to 9, it is necessary to operate a double cycle. The following is a demonstration of the code of the multiplication formula:
1 Static voidDoublefordemo (intNumber )2 {3 for(inti=1;i<=number;i++)4 {5 //J<i as a cyclic judging condition can control the number of times each cycle is to be manipulated6 for(intj=1;j<=i;j++)7 {8System.out.print (j+ "*" +i+ "=" +j*i+ "\ T");9 }Ten //Print carriage return line after each round of cycle One System.out.println (); A } -}
The "\ T" in the code is an escape character, that is, a tab. There are some other escape characters: \ n: Carriage return, \b: backspace, \ r: Carriage return.
The output is:
Iv. Other control statements
Break (jump out), continue (continue).
The break statement is applied to jump out of the current loop;
The continue statement is the end of this loop to continue the next loop.
Note: These two statements leave the application scope of the loop, and there is no point in existence.
This two statement exists alone, there can be no statements below, because the execution is not done, compile error!
We can get acquainted with break and continue through a little practice.
1 //ask for the first three odd numbers within 10. 2 Static voidbreakandcontinue ()3 {4 intCount=0;5 for(inti=0;i<10;i++)6 { 7 if(i%2==0)8 Continue;//if it is an even number, end this cycle and continue with the next loop. 9 if(count++>=3)//if it is odd, it is the first number of times, if it exceeds the third odd, the loop is terminated.Ten Break; One System.out.println (i); A } -}
The results are as follows:
The summary of the process control is here to end, to write good code, process control must be firmly mastered.
Keep on trying to refuel! For tomorrow's better self.
Dark Horse programmer--java Basic Grammar (ii)---process Control