First, the process control structure
Scanner class
In the basic grammar, only simple operation results can be printed, and the interaction between programs and people cannot be realized.
Among the new features of Java 5, a tool class Scanner is provided to facilitate the acquisition of user input.
basic grammar
Scanner s = new Scanner(System.in);
Create a scanner that accepts user input
Judgment statement expression
if (Scanner.hasNextLine());
First use the hasNext() and hasNextLine() methods to determine whether the character is input
Similar judgment methods include:
hasNextByte() //Determine whether the byte type is input
hasNextInt() //Determine whether the integer type is input
hasNextShort() //Determine whether the Short type is input
hasNextLong() ////Determine whether the Long type is input
hasNextFloat() //Determine whether the Float type is input
hasNextDouble() //Determine whether the Double type is input
hasNextBoolean() //Determine whether the Boolean type is input
The difference between next() and nextLine()
next can only get characters without spaces, nextLine can get a line of characters
method get expression
Get the input characters through the next() and nextLine() methods of the Scanner class
String str = next()/nextLine()//Get characters and put them in str
Similar methods are also available:
nextByte() //Get byte type
nextShort() //Get Short type
nextInt() //Get Int type
nextLong() //Get Long type
nextFloat() //Get Float type
nextDouble() //Get Double type
nextBoolean() //Get Boolean type
close IO stream
scanner.close;
If it is not closed, it will take up resources and form a habit
if conditional statement
The if selection structure is generally used to make certain judgments and meet certain conditions to execute the statement.
if single-choice structure
If the if boolean expression is satisfied, the statement in the if bracket is executed, otherwise, skip to the next step directly
case
//if single-choice structure boolean select = true; if (select == false){ System.out.println("boolean value false");//Execute if the condition is met, jump out if it is not met }
if double select structure
If the value of the Boolean expression is true, execute the statement within the if brackets, otherwise execute the statement within the else.
case
//if double select structure int age = 17; if (age >= 18){ System.out.println("age of adulthood");//Execute the statement under if if the condition is satisfied, and execute the statement under the else if not satisfied }else{ System.out.println("Underage age!"); }
if multiple choice structure
On the basis of the if double selection structure, several else if keywords can be added in the middle. When the Boolean expression is satisfied, only one of the branches is executed and the program is executed downward.
Case:
//if multiple choice structure int score = 44; if (score == 60){ System.out.println("Just passing grade");//Which branch of the judgment is satisfied, execute the statement of which branch, if not satisfied, jump out of the entire judgment }else if(score >= 90){ System.out.println("Great score"); }else if(score >= 80){ System.out.println("good grades"); }else if (score >= 60){ System.out.println("Pass"); }else{ System.out.println("Failed grades");//When the above branch judgments are not satisfied, the else statement is executed at the end }
switch switch logic statement
The Boolean expression of the if selection structure is generally used to determine a range, while the Boolean expression of the switch selection structure is used to determine a specific value.
case
Scanner in = new Scanner(System.in); System.out.println("enter int type constant"); int a = in.nextInt(); switch(a){ case 1: System.out.println("result is equal to 1");//Constant expression satisfying case break; case 5: System.out.println("the result is equal to 5"); break; default: System.out.println("Inputs are not 1 and 5!");//Executed only if all case s do not match, not affected by the order
while loop structure
single while loop structure
The sequence structure and the selection structure can only execute the program once while can realize the repeated execution of a certain section of the statement, which is widely used in practical programming.
-
The entire while structure can be called a loop body
-
If there is no condition set in the while, the statement in the parentheses will always be looped, which is called an infinite loop
-
A condition is added to the while The program will stop when the while loop does not satisfy the condition
grammar:
while(boolean expression){ //loop statement };
Case using a single while loop
int num = 0; while (num < 10) { System.out.print(num + " ");//The num variable loops up until the Boolean expression is not satisfied and jumps out num++; } System.out.println();
do while loop structure
The do while structure executes the loop statement once and then judges whether the loop is executed or not. Do is written before while while is written after.
grammar:
do{ //loop statement }while(conditional expression);
The case of using do while loop
int a = 1; do { a++; System.out.println("code output" + a);//If the condition is not met, it will also be executed once }while(a > 10);
for loop structure
The for structure contains three options: variable declaration, judgment condition and variable value change, separated by semicolons. The code is concise and concise when it is consistent with the execution effect of while
When a large number of loops are required, it is recommended to use the for loop for loop, and declare that the condition update value can be empty, and there is no fixed specification.
working process:
After definition, judge the condition, output first, then auto-increment, and finally update
//declare; condition; iterate; for (int a = 1;a > 1;a++;){ //loop statement }
Use a for loop to output numbers from 1 to 10 sequentially
for (int i = 0;i < 10;i++){ System.out.print(i+" "); } System.out.println();
Enhanced for loop structure
A new feature referenced by Java 5
Enhanced for loops are mainly used for arrays and collections
grammar
for(//Variable declaration: expression){//statement block};
Mainly used for looping operations on arrays and collections