1. The sequence structure has been mentioned in the previous lecture. I will not talk about it here.
2. Branch Structure (or select structure ):
There are two types of statements: if Condition Statement and switch branch statement;
For the if Condition Statement, there are also three basic forms:
First:
If (Boolean expression)
{
// Execute the code block;
}
Second:
If (Boolean expression)
{
// Execute the code block;
}
Else
{
// Execute the code block;
}
Third:
If (Boolean expression)
{
// Execute the code block;
}
Else if (Boolean expression)
{
// Execute the code block;
}
For example:
Int age = 30;
If (age> 20)
{
System. out. println ("You are over 20 years old! ");
System. out. println ("people of this age must learn to take responsibility! ");
}
It should also be noted that many people prefer not to write the if or else after {}. In fact, this is a bad habit.
Logical judgment errors:
Let's take a look at the instance and explain it in detail:
Int age = 45;
If (age> 20)
{
System. out. println ("you're a young man! ");
}
Else if (age> 40)
{
System. out. println ("you're a middle man! ");
}
Else if (age> 60)
{
System. out. println ("you're an old man! ");
}
// When doing the branch structure like this, there is actually a problem. Where is the problem? The range of age> 20 obviously contains two selection expressions in the elseif clause. Therefore, when the program is executed to the if clause, it will not be executed any more, the latter two selection conditions are meaningless.
This is equivalent to saying that if you are a person, it means that you are an individual. if you are a man or a woman in the else, it makes no sense, because the range of people includes men or women.
Therefore, for the execution of this piece of code, I personally think that you only need to pay attention not to put a large range before organizing the branch structure. Generally, this is no problem.
For the above error code, you only need to change 20 and 60. Of course, you need to change the output content, right! Let's look at the example after correction:
Int age = 45;
If (age> 60)
{
System. out. println ("you're an old man! ");
}
Else if (age> 40)
{
System. out. println ("you're a middle man! ");
}
Else if (age> 20)
{
System. out. println ("you're a young man! ");
}
Of course, for age> 40, you can also describe the Boolean expression in the if statement as follows:
Age> 40 &&! (Age> 60); this expression is also feasible.
Similarly, the following age> 20 can be similar:
Age> 20 &&! (Age> 40 );
The if else statement is described here first. Next we will look at another branch structure: switch structure:
The syntax format is as follows:
Switch (expression)
{
Case condition1:
{
Statement (s)
Break;
}
... // Keep up with the similar case above;
For example:
Char score = 'C ';
Switch (score)
{
Case 'A ':
System. out. println ("excellent ");
Break;
Case 'B ':
System. out. println ("good ");
Break;
Case 'C ':
System. out. println ("medium ");
Break;
Case 'D ':
System. out. println ("pass ");
Break;
Case 'E ':
System. out. println ("fail ");
Break;
Default:
System. out. println ("product input error! ");
}
Note that the control expressions in the switch can only be byte, short, char, or int, not a string, therefore, when defining this control expression or variable, we must note that it can only be defined as one of these types.
Conclusion: when using the switch statement, there are two points to note: one is the type of the control expression mentioned above, and the other is break, you can choose whether to use break based on your actual needs.
Author: "Li Yuan Cao"