Java - Flow Control

If you don’t accumulate a small step, you can’t go a thousand miles; if you don’t accumulate a small stream, you can’t make a river; the
article is continuously updated, you can search for [Xiaoqi JAVA interview] on WeChat to read it as soon as possible, reply to [Information] to get benefits, and reply to [Project] to get the project source code , reply [Resume Template] to get the resume template, reply [Learning Roadmap] to get the learning roadmap.

insert image description here


foreword

We must follow certain principles when doing anything. For example, when we go to the Internet bar, we must have an ID card and have money. These two conditions are indispensable. The same is true of programming, which needs to have flow control statements to implement control such as our real life.

One, match the sentence

A matching statement is a statement enclosed in {} curly brackets, which is a matching statement, and a matching statement can also be nested within a matching statement, which is a special nesting doll.

If a variable is defined in the conforming statement, it can only be used in this conforming statement. If it is used outside the conforming statement, then sorry, an error will be reported.

2. Conditional Statements

Conditional statements can execute different statements based on different conditions. Conditional statements include if conditional statements and switch multi-branch statements.

1, if conditional statement

If means if, we can use if to judge whether a condition is true. For example, if we judge whether you have an ID card, if you have an ID card, we will let you enter the Internet cafe, and if you do not have an ID card, we will let you go out and turn right. go home for dinner.

1. Simple if conditional statement

The syntax is as follows:

if(布尔表达式){
    
    
	语句序列
}

Boolean expression: a necessary parameter, indicating that the final returned result is a boolean value.
Statement sequence: optional parameter.

2. if...else statement

Earlier we saw that the if condition is followed by a sequence of statements, so that the sequence of statements is executed to prove that the condition is met. If the condition is not met, which statement is executed, and the condition after the else is executed if the condition is not met.

if(表达式){
    
    
	满足条件走这里
}
else{
    
    
	不满足条件走这里
}

3. if...else if multi-branch statement

Earlier we only had one expression to judge, that is, black and white, but what if we had multiple conditions to judge, if we judged the student's name and let them go back to the corresponding home and find the corresponding mother.

if(姓名是“张三”){
    
    
	回张三家;
}else if(姓名是“李四”){
    
    
	回李四家;
}else if(姓名是“王五”){
    
    
	回王五家;
}else{
    
    
	谁也不是送福利院;
}

4, switch multi-branch statement

The above if...else if statement can have multiple judgment conditions, but it is a bit too bloated to write that way. Imagine that if you judge all the students in the class, you need to judge all the expressions. Here we use switch to simplify.

The syntax is as follows:

swith(表达式){
    
    
	case 常量值 1
	语句块 1
	[break;]
	...
	case 常量值 n;
	语句块 2
	[break;]
	default:
	语句块n+1;
	[break;]
}

The value of the expression in the swith statement must be an integer, character or string type, and the constant values ​​1~n must also be an integer, character or string type. The swith statement first calculates the value of the expression. If the value of the expression is the same as the constant value after a case, several statements after the case statement are executed until the break statement is encountered. If there is no break in a case statement, then continue to execute in the next case statement, until the break statement is encountered, the following logical judgment will not be followed. If there is no constant value after a case equal to the expression, then the default statement is used.

Here's an example:

String name = "张三";
swith(name){
    
    
	case "张三";
	回张三的家;
	break;
	case "李四";
	回李四的家;
	break;
	default:
	谁也不是就去福利院;
}

Third, the loop statement

The loop statement is to repeatedly perform a certain operation under the condition that certain conditions are met. For example, Chi Ge, I went to eat Bawang meal today, but the boss did not let me go, and told me to stay and make money by brushing the dishes. Then I brush the dishes every day. , in the evening, go to the accountant to judge whether I have enough money for washing the dishes to pay for the meal. If it is not enough, I will continue to wash the dishes the next day, and I will be able to leave when I have enough.

1, while loop statement

The while statement is also called a conditional judgment statement, and its looping method is to use a condition to control whether to continue to execute the statement repeatedly.
The syntax is as follows.

while(条件表达式)
{
    
    
 执行语句
}

When the conditional expression is true, the statement in {} is executed. When the statement in {} is executed, the conditional expression is re-evaluated, and the loop can be exited until the expression is false.

while(涮盘子的钱 < 霸王餐的钱)
{
    
    
 继续刷盘子
 今天又挣了一块钱
}

Since the assignment operator "=" will first obtain the result of processing the right-hand expression, if an expression contains more than two "=" operators, it will be processed from the rightmost "=".

2. do...while loop statement

The do...while loop statement is similar to the while loop statement. The difference between them is that the while statement first judges whether the condition is true and then executes the loop body, while the do...while loop statement executes a loop first, and then judges whether the condition is true. That is, the program in the {} in the do...while loop statement must be executed at least once.
The syntax is as follows:

do
{
    
    
执行语句
}
while(条件表达式);

For example, in order to attract customers, some shops that sell snacks will let customers have a free taste first, and then buy it after eating. The first bite here is free, and the later ones will decide whether to buy or not

do
{
    
    
吃一个口
}
while(是否买了一口零食);

3. For loop statement

The for loop is one of the most useful loop statements in java programming. A for loop can be used to execute a statement repeatedly until a certain condition is met.

1. For statement

The syntax is as follows:

for(表达式1;表达式2;表达式3)
{
    
    
	语句序列
}

Expression 1: Initialization expression, responsible for completing the initialization of variables.
Expression 2: The loop condition expression, the value is a boolean expression, specifying the loop condition.
Expression 3: The operation expression after the loop is responsible for trimming variables and changing the loop condition.

When executing the for loop, first execute expression 1, give an initial value to the variable, and then judge expression 2. If the expression 2 is true, execute the statement sequence. After executing the statement sequence, execute expression 3 to change the variable. , after the change, the judgment of expression 2 is carried out. If it is still true, the statement sequence will continue to be executed. When expression 2 is not true, the loop will not be executed.
E.g:

for(int i = 0; i=10; i++){
    
    
	我要输出十遍我这句话
}

The above cycle is repeated ten times, and i is incremented by one each time. When i is equal to 10, the cycle is not repeated.

2. Foreach statement

The foreach statement is a special simplified version of the for statement and cannot completely replace the for statement, but any foreach statement can be rewritten as a for statement version. foreach is not a keyword, and it is customary to call this special for statement format the foreach statement. The foreach statement provides programmers with great convenience in traversing data and so on.
The syntax is as follows:

for(元素变量x : 遍历对象obj){
    
    
	输出x变量的值
}

For example, we have a list of students, we want to traverse their names and output, then we can use the following code:

for(学生对象  :  学生集合list){
    
    
	输出学生对象的姓名
}

4. Cycle control

Loop control includes two aspects, one is to control the way the loop variable changes, and the other is to control the jump of the loop. Two keywords, break and continue, are needed to control the jump of the loop. The jump effects of these two jump statements are different. Break is to interrupt the loop, and continue is to execute the next loop.

1. break statement

Use the break statement to break out of the loop body, e.g.

while(1=1){
    
    
	第一段话
	break;
	第二段话
}

The above will only output the first paragraph once, and then jump out of the while loop directly when it encounters break.

2. continue statement

The use of the continue statement is to stop the execution of the current loop after the continue, but it does not jump out of the loop, but executes the loop judgment again and enters the loop again. E.g

while(1=1){
    
    
	第一段话
	continue;
	第二段话
}

The above statement will always output the first paragraph, because continue will only stop the execution of the following statement, and will not jump out of the loop body.

4. Summary

The relevant content here has not been sorted out, and the article will continue to be updated later. It is recommended to collect it.

The commands involved in the article must be knocked several times like me. Only in the process of knocking can you find out whether you really master the commands.

You can search [Xiaoqi JAVA Interview] on WeChat to read it for the first time, reply to [Information] for benefits, reply to [Project] to obtain the project source code, reply to [Resume Template] to obtain the resume template, and reply to [Learning Roadmap] to obtain the learning roadmap.

Guess you like

Origin blog.csdn.net/weixin_44096133/article/details/125772175