Process control is a very important learning stage for any language. It provides the basic means of the overall operation process of the program. It can also be said that process control is an important part of making the computer move.
Let's talk about what we need to know about process control?
Control statement:
"Sequential structure" represents the logic of "execute a first and then execute b". For example, first find a girlfriend and then call her; First get engaged, then get married;
"Conditional judgment structure" represents the logic of "if..., then...". For example, if your girlfriend calls, answer the phone quickly; Stop if you see a red light;
"Loop structure" represents the logic of "if..., repeat...". For example, if you don't get through to your girlfriend, continue to call again; If you don't find someone you like, keep looking.
Before speaking Java control statements, you can watch the course to help you understand the Java control process more easily:
[recommended courses] Java300 set zero basic video tutorial for beginners_ Java 300 set zero basic tutorial_ Introduction to java video foundation consolidation tutorial_ Introduction to Java languageJavaSE basics tutorial - for beginners only_ Gaoqi Java300 set / zero foundation of Java / introduction to Java / proficient in Java programming / JavaSE/Java Foundation
JavaSE basic system practical course_ Java process control and operator operation practice_ Loop operation control / JavaSE course / data type / operator / scanner / JavaSE introductory practical tutorial
Introduction to Java practice_ Java zero Foundation / online education / introduction to Java to proficient / Java programming / introduction to Java zero Foundation
When we don't know the Java selection structure, the program we write always starts from the program entry and executes each statement in sequence until the end of the last statement. However, conditional judgment is often needed in life. It is necessary to decide whether to do something according to the judgment results, so we need to select the structure.
The selection structure is used to judge the given conditions, and then control the flow of the program according to the judgment results. The main conditional judgment structures are: if structure and switch structure. If structure can be divided into if single branch structure, if else double branch structure and if else multi branch structure.
First: select structure
1. if single branch structure
if selection structure is a programming flow control composed of conditional Boolean expression and statement series (code block).
Conditional expression 1 ~ conditional expression n: required parameters. It can be composed of multiple expressions, but the final returned result must be of boolean type (true, false)
Statement series: it can be one or more statements. When the value of conditional expression 1 is true, execute statement Series 1; When the value of conditional expression 2 is true, execute statement sequence 2; and so on.
Form I:
if (expression){
Code block 1;
}
if statement determines the Boolean expression once. if it is true, execute the statement block in {}, otherwise skip the statement block.
Example:
int age = 20; if (age > 18) { String name = "Tom"; System.out.println("My name is " + name + ","Already" + age + "Years old, I'm an adult! "); }
2. If else double branch structure
Form 2:
if (expression){
Code block 1;
} else {
Code block 2;
}
When the Boolean expression is true, execute statement block 1; otherwise, execute statement block 2. That is the else part.
Example:
int age = 16; if (age > 18) { String name = "Tom"; System.out.println("My name is " + name + ","Already" + age + "Years old, I'm an adult! "); } else { System.out.println("I'm a minor! "); }
3. If else multi branch structure
When Boolean expression 1 is true, execute statement block 1; Otherwise, judge Boolean expression 2, and execute statement block 2 when Boolean expression 2 is true; Otherwise, continue to judge Boolean expression 3 ······; If 1~n Boolean expressions are determined to be false, the statement block n+1, that is, else part, is executed.
Form 3:
if (expression) { Code block 1; } else if (expression) { Code block 2; } else { Code block 2; }
Example:
if (age >= 0 && age <= 10) { System.out.println(""Teenagers"); } else if (age <= 18) { System.out.println(""Teenagers"); } else if (age <= 30) { System.out.println(""Youth"); } else if (age <= 50) { System.out.println("Middle age "); } else { System.out.println(""Old age"); }
4. Nested if selection structure
Execution process: judge the inner conditions when the outer conditions are met
Note: in one selection structure, another selection structure can be nested (any combination is supported as long as the nesting format is correct)
if((outer expression){ if(Inner expression){ Inner code block 1 }else{ Inner code block 2 } } else{ Outer code block }
5. switch multiple selection structure
swtich} statements are composed of control expressions and multiple} case} label blocks.
The switch statement will be executed from the matching case tag to the break statement or the end of the switch statement according to the value of the expression. If the value of the expression does not match any case value, enter the default statement (if there is a default statement).
Syntax structure:
switch (expression) { case Value 1: Statement sequence 1; [break]; case Value 2: Statement sequence 2; [break]; ... ... ... ... ... [default: Default statement;] }
Note that if else if else multi branch structure or switch structure can be used when the Boolean expression is judged by equivalence. If the Boolean expression is judged by interval, only if else if else multi branch structure can be used.
Example:
public class TestSwitch1{ public static void main(String [] args){ //Give a score int score = (int)(Math.random()*81)+20; //According to the score, give the grade char grade; switch(score/10){ case10 : case9 :grade = 'A'; break; case8 :grade = 'B'; break; case7 :grade = 'C'; break; case6 :grade = 'D'; break; default :grade = 'E'; } //Output level System.out.println("fraction:"+score+",Grade:"+grade); } }
Second: circular structure
Repeat an operation many times, and using a loop can simplify the code
1. while loop structure
At the beginning of the loop, the value of "Boolean expression" is calculated once. If the condition is true, the loop body is executed. For each subsequent additional cycle, it will be recalculated before the start.
There should be a statement in the statement that makes the loop tend to end, otherwise an infinite loop – "dead" loop will appear.
Syntax structure:
while (Boolean expression){ Circulatory body; } |
Example:
public class TestLoop3 { public static void main(String[] args) { int i = 1; int sum = 0; // 1+2+3+...+100=? while (i <= 100) { sum += i;//Equivalent to sum = sum+i; i++; } System.out.println("sum= " + sum); } }
2. Do while loop structure
The do while loop structure will execute the loop body first, and then judge the value of the Boolean expression. If the condition is true, execute the loop body, and end the loop when the condition is false. The loop body of a do while loop is executed at least once.
Syntax structure:
do { Circulatory body; }While (Boolean expression); |
Example:
public class TestLoop4 { public static void main(String[] args) { int i = 0; int sum = 0; do { sum += i; i++; } while (i <= 100);//Here; Cannot be omitted System.out.println("sum= " + sum); i = 1; sum = 0; do{ sum += i; i+=2; }while(i<100); System.out.println("sum= " + sum); } }
3. for loop
For loop statement is a general structure that supports iteration. It is the most effective and flexible loop structure. The for loop needs to be initialized before the first iteration, that is, execute the initial expression; Then, judge the Boolean expression. If the judgment result is true, execute the loop body; otherwise, terminate the loop; Finally, at each iteration, some form of "stepping" is performed, that is, the iteration factor is executed.
- The initialization section sets the initial value of the loop variable
- The conditional judgment part is an arbitrary Boolean expression
- The iteration factor controls the increase or decrease of cyclic variables
After the execution condition of the for loop is determined, the body of the loop is executed first, and then the step is executed.
Syntax structure:
for (initial expression; Boolean expression; iteration factor){ Circulatory body; } |
Example:
public class TestLoop5 { public static void main(String[] args) { //calculation int sum; int i; for (i = 1, sum = 0; i <= 100; i++) { sum += i; } System.out.println("1+2+3....+100=" + sum); i = 1; sum =0 ; for (; ; ) { if (i > 100) { break; } sum += i; i++; } System.out.println("1+2+3....+100=" + sum); sum = 0; for (int j = 1; j <= 100; j++) { sum += j; } System.out.println("1+2+3....+100=" + sum); } }
Third: loop control statement
1. break statement
In the body of any loop statement, you can use break to control the flow of the loop. Break is used to forcibly exit the loop without executing the remaining statements in the loop.
Example:
public class TestBreak{ public static void main(String [] args){ int sum = 0; int i; for(i=1;i<=100;i++){ sum +=i; System.out.println("i="+i+",sum="+sum); if(sum>=3000){ //break; return; } } System.out.println("i="+i+",sum="+sum);//10177 not 78 } }
It was supposed to cycle 100 times. As a result, when i=77, sum > = 3000. At this time, break means that the subsequent 23 times are not executed, and the cycle is ended in advance.
break statements can appear in loops and switch statements
Return statement: ends the current method. It has nothing to do with the loop; Each method ends with a return
2,continue
The continue statement is used in the body of a loop statement to terminate a loop process, that is, skip the statements that have not been executed in the loop body, and then determine whether to execute the loop next time.
Example:
public class TestContinue { public static void main(String[] args) { for (int i = 1; i <= 100; i++) { /* if(i%5==0){ System.out.print(i+"\t"); } */ if (i % 5 != 0) { continue; } System.out.print(i + "\t"); } } }
It was originally a cycle of 100 times. Did you use continue or a cycle of 100 times; When i=1,2,3,4, because I% 5= 0, do not execute subsequent statements of the loop body; When i=5, because i%5==0, execute the subsequent statements of the loop body
Learning objectives
1. Master the difference and use occasion between if statement and switch statement
2. Master the usage of break in switch and the default statement
3. Master the writing format and execution sequence of the three cycles
4. Master the break and continue keywords and understand the function of return
5. Master the use of multiple loops and some common algorithms in process control
6. Understand loop and unreachable statements
7. Master the declaration and use of methods and the memory analysis when calling methods
8. Master the method and application occasions
The above is the process control of Java language, which is a very basic and important knowledge point. I hope you can like it~~~
Dry article: Dimple: Java learning circuit - giant super full version super full super full