Home  >  Article  >  Java  >  [Java Introduction Notes] Java Language Basics (4): Process Control

[Java Introduction Notes] Java Language Basics (4): Process Control

黄舟
黄舟Original
2016-12-22 11:28:381155browse

Process control refers to the way to control the direction of the program while it is running. It is mainly divided into the following categories:

Sequential structure

Sequential structure, as the name suggests, means that the program is executed sequentially from top to bottom. There is no judgment or jump in the middle.

Branch structure

Java provides two branch structures: if and switch. The if statement uses Boolean expressions or Boolean values ​​to judge the branch control, while the switch statement uses integers, String types, and enumeration types. .

if statement

if statement uses Boolean expressions or Boolean values ​​to judge and control branches. There are mainly three structures:

if statement

if (condition) {

//Statement

}

Execution trend

[Java Introduction Notes] Java Language Basics (4): Process Control

Example:

int score = 65;if(score >= 60){
System.out.PRintln("You passed");
}

if…else statement

if (condition) {
                                                                                                                                                       Example:

int score = 65 ;if(score >= 60){
System.out.println("You passed, congratulations");

} else {

System.out.println("Failed, cleaning toilets for three months");

}

[Java Introduction Notes] Java Language Basics (4): Process Control

if…else if statement

if (condition) {

//statement } else if (condition) {
//statement } else {
//statement
}

Program flow chart



Example:

Evaluation of students’ final exam scores

Score>=90: Excellent

Score>=80: Good

[Java Introduction Notes] Java Language Basics (4): Process Control Score>=60: Moderate

Score

int score = 70; //Examination score if ( score >= 90 ) {

System.out.println("Excellent");

} else if (score >= 80 ) {

System.out. println("Good");

} else if (score >= 60 ) {

 System.out.println("Medium");

} else {

 System.out.println("Poor");

}


contains another if statement in the if statement

If (condition 1) { if (condition 2) { if (condition 3) {

}else{
}
}………
}

Normally, the statement blocks of these types of judgment statements are wrapped in {} brackets, but if the statement has only one sentence, you don’t need to use curly braces, for example

boolean b = true;if( b)
System.out.println("You can not use curly braces"); else
System.out.println("It is recommended to put curly braces");

If there is only one statement in the judgment statement, it can be omitted. Curly braces are used, but it is recommended that you still use curly braces.

switch statement

switch (expression) //Calculate the value of the expression{  case constant 1: //If equal to constant 1 Statement; break;  case constant 2: //If equal to constant 2 Statement; break; Default: //If no matching value is found Statement

Break; }


Different from the if statement, the expression of the switch statement can pass values ​​of byte, short, int, long, and String types.

String s = "Passed";switch(s){

 case "Passed":

 System.out.println("Performing well");

 break;
case "Failed":

 System.out. println("Clean the toilet");

  break;

 default:

  System.out.println("Didn't come for the exam? Call your parents");

  break;

}



int i = 1;switch(i){
 case 1:
  System.out.println("Won the first place");
  break;
 case 2:
  System.out.println("Won the second place" );
  break; case 3:
 System.out.println("Get third place");
 break;
 default:
 System.out.println("You are not the top three");
  break;
}

Pay attention to the break statement when using it. If you miss it, hey!

Comparison of switch and multiple if

Same points:

 Both can implement multi-branch structures

Differences:

 Switch: can only handle conditional judgments of equal values, and the conditions are equal to integer variables or character variables Value judgment

Multiple if: The processing also includes other if structures in the else part, which is especially suitable for situations when a certain variable is in a certain range

Loop structure

Loop statements are also called iteration statements. Loop statements can satisfy Under the condition of conditions, repeatedly execute a certain code. The cycle statement includes:

while cycle

while (cycle condition) {// meet the conditions and continue to execute the cycle; Operation}

Features: Judge first, then execute

[Java Introduction Notes] Java Language Basics (4): Process Control

Example sentence: Print out 30 Hello

int i = 1;while(i System.out.println ("Hello");
i++;
}

do-while loop

do {

Loop operation //Perform the loop operation first} while (loop condition); //If the conditions are met, the loop continues to execute; otherwise, Loop exit

Features: Execute first, then judge

[Java Introduction Notes] Java Language Basics (4): Process Control

//Whether he has passed or not, clean the toilet first, and then see whether he has passed or not. int i = 65;do{

System.out.println("Unlucky, need to clean the toilet");
} while(i

Infinite loop problem: all loop control statements, if none of them exit condition, it will enter an infinite loop state. In the above example, if i is less than 60, it will always be "cleaning the toilet" and cannot continue to execute.

for loop

for (parameter initialization; conditional judgment; update loop variable) {

loop operation;
}

Features: initialize parameters first, judge the condition, if it is true, execute the loop body, and then update the loop variable, and then return to the conditional judgment. If it is not established, exit the loop directly.

[Java Introduction Notes] Java Language Basics (4): Process Control

for (int i = 0 ; i  System.out.println("I'm the best");
}

Control loop structure

break statement

in a certain Sometimes, we need to forcefully terminate the loop when a certain condition occurs, and we can use break to complete this function. For example:

[Java Introduction Notes] Java Language Basics (4): Process Control

for(int i = 1; i }
System.out.println("Complete a circle") ;
}

In the above example, 10 laps should have been completed, but when reaching the 8th lap, I couldn’t hold on anymore and broke, and the rest will not be executed.

[Java Introduction Notes] Java Language Basics (4): Process Control


continue statement

The functions of continue and break are somewhat similar. The difference is that continue only skips this loop, and subsequent loops will still be executed. For example:

[Java Introduction Notes] Java Language Basics (4): Process Control


for(int i = 1; i }
System.out.println("Complete one Lap");
}

He was supposed to run 10 laps, but he actually only ran 9 laps.

[Java Introduction Notes] Java Language Basics (4): Process Control


The above is the content of [Java Getting Started Notes] Java Language Basics (4): Process Control. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn