(1. Process Control
There are three basic techniques that can change the program's control flow:
A. Calling method: Causes the control flow to leave the current method and transfer to the called method.
B. Select: A. If/else statement
B. Switch statement
C. Three mesh operation
C. Loops: A. While loop
B. For loop
C. Do/while Cycle
Note: All control structures contain some kind of Boolean judgment
(2. Boolean Logic (Boolean: True, False)
There are four types of logic when combining Boolean expressions:
A. and (&&): Each expression is true.
B. or (| | ): As long as one of the expressions is true.
C. Xor or
D. Non-(! ): Inverse of a Boolean expression.
(3. Boolean operator
A. Short circuit and (&&)
B. and (&)
C. Short Circuit or (| | )
D. or (|)
E. XOR (^)
F. Non-(! )
(4. SELECT statement
A. Single Branch statement: if () {}
B. Two-branch statement: if () {}else{}
C. Multi-Branch statement: if () {}else if () {}else{}
D. Switch statement: Generally used as the equivalent condition judgment
switch (variable) {
Case value: statement;
Case value: statement;
Default
}
Note: The 1.switch statement can only be byte (byte type), short (shorter integer), int (integer), char (character type).
2. Each case constant expression must be of a different type
3.case and default position not fixed
(5. Looping statements
A. While: first judgment, back loop
B. Do/while: Cycle First, then judge, cycle at least once
C. For: First judge, after loop, it is a control structure with a specified number of cycles
For example 1: There is a fractional sequence: 2/1,3/2,5/3 ..... to find out the sum of the first 20 items of this series.
Public class one{
Public static void Main (string[] args) {
int den=1;//Denominator
int num=2;//molecule
double sum=0;
int temp=0;
for (int i=0;i<20;i++) {
Temp=num;
Num=den+num;
den=temp;
sum=sum+ (double) Num/den;
}
SYSTEM.OUT.PRINTFLN ("Sum of 20 items:" +sum);
//System.out.printf ("Sum of 20 items:%.2f", sums);
}
}
For example 2. Randomly generate a random number between 0~99 and then start guessing the number game
import Java.util.Scanner;
Public class three{
Public static void Main (string[] args) {
Scanner input = new Scanner (system.in);
//Generate random numbers
int random = (int) (Math.random () *100);
System.out.println (random);
int c=0;
int temp=0;
While (c==0) {
System.out.print ("Please enter the number you guessed:");
int num = Input.nextint ();
temp++;
if (random==num) {
System.out.println ("Congratulations, you guessed it!") ");
c=1;
}
else if (random>num) {
System.out.println ("A little bit, guess again");
}
else if (random<num) {
System.out.println ("Big point, Guess again");
}
}
System.out.println ("Total number of guesses:" +temp);
}
}
(6. Jump statement
A. Break statement: Can be used for any loop control structure, so that the loop terminates immediately
B. Continue statement: A. Continue in the For loop will cause the control flow to immediately jump to the UPDATE statement
B. Continue in while and do/while, the control flow jumps immediately to the Boolean expression
Program flow control in the Java language