Control statement: combine the statements into small logic modules that can complete certain functions. Divided into 1 Sequential statement 2 Select statement 3 Loop statement. In fact, any software and program, from an exercise to an operating system, is essentially composed of "selection statements and circular statements". In cpp, the order structure, selection structure and loop structure we learn are not the same as those in Java, but only the same.
Conditional statement
Conditional judgment structures include 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.
if structure
Although the if structure can be divided into if single branch structure, if else double branch structure and if else multi branch structure, the essence of judgment is the same. Whether to execute or not is judged by the Boolean expression in parentheses after if.
Let's use a random function math Random() to simply apply the if structure:
package com.CWJH; public class textif11 { public static void main(String[] args){ int age=(int)(100*Math.random() ); System.out.print("Age is" + age + ", belong to"); if (age < 15) { System.out.println("Children, like to play!"); } else if (age < 25) { System.out.println("Young people, learn!"); } else if (age < 45) { System.out.println("Middle age, work!"); } else if (age < 65) { System.out.println("Middle aged and elderly people should supplement calcium!"); } else if (age < 85) { System.out.println("Old age, exercise more!"); } else { System.out.println("Old birthday, ancient rare!"); } } }
Operation results:
switch multi branch structure
There are still many differences between switch multi branch structure and if structure;
Is the end of the switch. If the value of the expression does not match any case value, enter the default statement. Note: if
If there is no break before default, the contents of the default statement will still be executed (pit questions from cpp);
package com.CWJH; public class textswitch { public static void main(String[] args) { int month = (int)(Math.random()*12)+1; if(month==1||month==2||month==3||month==4 ||month==5||month==6){ System.out.println(month+"Month belongs to the first half of the year"); }else{ System.out.println(month+"Month, second half"); } switch (month){ case 1: case 2: case 3: case 4: case 5: case 6: System.out.println(month+"Month belongs to the first half of the year"); break; default: System.out.println(month+"Month, second half"); break; } } }
Operation results:
Circular statement
Java loop statements are the same as those in cpp. They are all while, do--while and for loops. However, the description in the class is somewhat different from that in cppc. Java: loop structures are divided into two categories: when type and until type.
- When type: when the Boolean expression condition is true, execute a statement repeatedly, and stop the loop only when the Boolean expression value is false, such as while and for loops.
- Until type: execute a statement first and then judge the Boolean expression. If it is true, execute a statement again. Repeat this process until the Boolean expression condition is false, such as do while loop.
(cpp has done a lot of practice on the loop structure, so we won't perform it here)
break statement and continue statement
1.break is used to forcibly exit the whole cycle 2 Continue is used to end this cycle and continue the next cycle
Finally, a little knowledge collected during watching the video
- println implements line feed and print does not wrap
- Math.round ÷ rounding (the adjacent integer interval of the parameter is rounded to the nearest integer. If it is in the middle, it is rounded to the positive infinite direction, which is consistent with the round() function in cpp)