Java Process Control includes sequential control, conditional control, and loop control
- Sequential control is the execution of each statement from beginning to end
- Conditional control, which executes a statement based on a conditional selection, for example, if the condition is true, then an action A is executed, or if the condition is true, action A is executed, and the action B is executed.
- Cyclic control, also known as loop control, performs the operation of the loop body according to the cyclic initial conditions and termination requirements
Sequential structures can only be executed sequentially, not judged and selected, so branching structures are required
Branching structure:
- If statement
- If....else ....
- If.....else If....else If ...
- Switch statement
Note: If...else ... The execution statement block of the condition {} If there is only one statement, then the pair {} can be omitted;
The switch statement executes the corresponding statement of the case once it satisfies the case condition. If there is no break or has reached the end, it will continue to execute its case statement! The variable type after the case can be byte,short,char,int, enumerated, string, and must be a constant or an expression with a constant value
The difference between do....while and while is that the Do...while loop executes at least once
When the number of cycles is uncertain, the number of cycles can be randomly used while
In general, a statement that has a program termination inside an infinite loop is implemented using break. If not, that's the cycle of death.
Loop structure:
- While loop
- Do...while Cycle
- For loop
- foreach Loop
code example:
PackageProcesscontrol;ImportJava.util.Scanner; Public classProcesscontrol { Public Static voidMain (string[] args) {//Ifstudy (); //Switchstudey (); //Forstudy (); //whilestudy ();Foreachstudy (); } Public Static voidIfstudy () {Scanner input=NewScanner (system.in); System.out.println ("Please enter the final result of your nickname:"); //here The default input is an integer, do not make a detailed judgment intScore =Input.nextint (); if(Score > | | Score < 0) {System.out.println ("Reward a car bmw! "); } Else { if(Score = = 100) {System.out.println ("You entered the wrong number!" "); } Else if(Score > && score <= 99) {System.out.println ("Reward a Taiwan iphone5s! "); } Else if(Score >= && score <= 80) {System.out.println ("Reward a reference book!" "); } Else{System.out.println ("Not a pass, but also want to reward!" "); } } } Public Static voidSwitchstudey () {Scanner input=NewScanner (system.in); System.out.println ("Please enter the final result of your nickname:"); //here The default input is an integer, do not make a detailed judgment intScore =Input.nextint (); Switch(SCORE/60) { Case1: System.out.println (Failed); Break; Case0: System.out.println ("Failed"); Break; } /** Switch (SCORE/10) {case 10:case 9:case 8:case 7:case 6: * SYSTEM.OUT.PRINTLN ("pass"); break; c ASE 5:case 4:case 3:case 2:case * 1:case 0:system.out.println ("failed"); Break } */ } Public Static voidForstudy () {/** Write program Foobizbaz.java, loop from 1 to 150 and print a value in each line, * also print "foo" on multiples of each 3 line, print "biz" on multiples of 5, print output "Baz" on multiples lines of 7 */ for(inti = 1; I <= 150; i++) {System.out.print (i); if(i% 3 = = 0) {System.out.print ("Foo"); } if(i% 5 = = 0) {System.out.print ("Biz"); } if(i% 7 = = 0) {System.out.print ("Baz"); } System.out.println (); } } Public Static voidWhilestudy () {//an even number within 100 of output /*int i = 1; while (I <=) {if (i% 2 = = 0) {System.out.println (i); } i++; } */ intI=1; Do{ if(i%2==0) {System.out.println (i); } I++; } while(i<=100); } Public Static voidForeachstudy () {int[] arr={2,3,4,5,6}; for(intI:arr) {System.out.println (i); } }}
Java Process Control