1. Sequential structure
Java The basic structure is the sequential structure, and the code is executed sequentially.
2. Selection Structure
(1) if selection structure
1. if single-choice structure
if A single-choice structure is also called if A one-condition branching statement that controls the flow of program execution based on one condition.
Grammar:
if(Boolean expression){ //Statement that will be executed if the expression is TRUE }
2. if double-selection structure
When two judgments are needed, the double-choice structure is used, so there is if...else Sentence.
Grammar:
if (Boolean expression){ //Statement whose expression value is TRUE will execute }else{ //Statement whose expression value is FALSE will execute }
3. if multi-selection structure
There are many kinds of real problems, and if there are multiple levels of judgment, For example, if you enter scores for different segments and you want to output the grades for corresponding segments, you need to use a multiple-choice structure to process them.
Grammar:
if (Boolean expression 1){ //Statement whose value of expression 1 is TRUE will execute }else if (Boolean Expression 2){ //The value of expression 2 is the statement that TRUE will execute }else if (Boolean expression 3){ //The value of expression 3 is the statement that TRUE will execute }else{ //None of the above expressions are statements that TRUE will execute }
4. if nested structure
if...else Statement nesting is also legal. That means you can have another if perhaps if...else Use in statement if perhaps if...else Sentence.
Grammar:
if (Boolean expression 1){ //Statement whose value of expression 1 is TRUE will execute if (Boolean Expression 2){ //The value of expression 2 is the statement that TRUE will execute } }
(2) Switch multi-choice structure
Another way to implement a multiple-choice structure is to switch...case Sentence. switch...case Statement determines whether a variable is equal to a value in a series of values, and each value becomes a branch.
Grammar:
switch(expression){ case value: //Sentence break; //Optional case value: //Sentence break; //Optional //There can be any number of case statements default: //Optional //Sentence }
Variable types in a switch statement can be:
- byte, short, int, or char
- String type support starting with java7
- String constants or literals must also be present at value
Note: Some explanations about break statements are described below
3. Circulation Structure
(1) while cycle
while Loops are the most basic loop structure
Grammar;
while(Boolean expression){ //Circulatory body }
Loop condition always TRUE will cause "dead loop". In most cases, we want to stop the loop. We need a condition to end the loop. In a few cases, we need to keep the loop going, such as server request response monitoring.
(2) do...while cycle
about while Statement, if the condition is not met, you cannot enter a loop. But sometimes we need to execute at least once, even if the condition is not met.
Grammar:
do { //Circulatory body }while(Boolean expression)
The difference between while and do...while:
- While judges before execution, do...while judges after execution
- do...while always guarantees that the circulatory body will be executed at least once
(3) for cycle
Java Another kind of looping statement is provided: for Loop statements to make some looping structures simpler.
Grammar:
for (Initialization; Boolean expression; Amendment variable){ //Circulatory body }
The rules for executing the for statement are as follows:
- Complete the initialization. You can initialize one or more loop control variables, which can be empty statements;
- Judges the Boolean expression, if the value is TRUE, then proceeds to step 3, otherwise ends the for loop;
- Execute the loop once, then correct the variable to change the cycle condition, proceed to step 2;
- End the for loop.
For example:
public class ForDemo04 { public static void main(String[] args) { //Output 0~5 with for loop for (int i=0; i<5; i++){ System.out.println(i); } } }
4. Other
(1) break and continue statements
- break statement
Break is used to force the loop to exit without executing the remaining statements in the loop. For example, as mentioned above, a break statement is used in the switch statement.
Let's take another example, where a student's grade is known and the corresponding grade is determined:
Let's write the break statement first
public class BreakDemo { public static void main(String[] args) { char grade = 'C'; //Here, for convenience, write your results to death switch (grade){ case 'A': System.out.println("excellent"); break; case 'B': System.out.println("good"); break; case 'C': System.out.println("qualified"); break; case 'D': System.out.println("Unqualified"); break; } } }
The result is correct
Then we remove all the break statements
public class BreakDemo { public static void main(String[] args) { char grade = 'C'; //Here, for convenience, write your results to death switch (grade){ case 'A': System.out.println("excellent"); //break; case 'B': System.out.println("good"); //break; case 'C': System.out.println("qualified"); //break; case 'D': System.out.println("Unqualified"); //break; } } }
The result is this
That is, if you do not write a break statement, grade is a variable that matches the following case statement. When it matches the third case statement and prints "qualified", it does not end the program, but proceeds down to the contents of the next case statement, which also illustrates the role of break.
- continue Statement
The continue statement is used in loop body statements to terminate a loop process, that is, to skip statements that have not yet been executed in the loop body, and then decide whether to execute the loop next time.
Let's take another example, which outputs multiples of no more than 10 and no more than 100:
public class ContinueDemo { public static void main(String[] arg) { int i = 0; while (i < 100){ i++; if (i%10==0){ //A balance of 10, equal to 0, is a multiple of 10 or 10 System.out.println(); //Line Break continue; } System.out.print(i+"\t"); //Output Digital } } }
You can see that when you encounter a multiple of 10 or 10, the program terminates the current loop and moves on to the next one because of the continue statement.
(2) Arrays and for loops
jdk5 An enhanced version was introduced for Loops, mainly used to traverse arrays or collections.
Grammar:
for (Declare Statement:Expression){ //Circulatory body }
- Declare statement: Declare a new local variable whose type must match the type of the array element. Its field of action is limited to a loop statement block whose value is equal to that of the array element at this time.
- Expression: An expression is the name of an array to access or a method that returns an array.
For example:
public class ForDemo05 { public static void main(String[] args) { //Define an array int[] numbers = {1, 2, 3, 4, 5}; //Traversing the elements of an array for (int x:numbers){ System.out.print(x); } } }
That is, each loop takes a value from numbers and assigns it to the variable x.
The above is only personal learning experience, there are deficiencies, please correct ~Thank you