Java parenting classic 3: if and switch conditional structure

Dad: have you finished your homework last time?

  1. Define your own variables and calculate the area of a triangle
  2. Evaluates the value of the specified expression

Son: according to what I said last time, I soon made it! You said before that you can use if conditional structure to judge. Can you tell me about it?

Dad: arrangement

boolean type

As we mentioned last time, boolean type is a basic data type in Java. It has only two values

  • True: true
  • False: false

For example: bananas are yellow. Is this true or false? Obviously, the answer is true. We use boolean type to indicate that it is true

Another example: is it true or false that the earth is bigger than the sun? Obviously, it must be false. If we use boolean type, it is false

if conditional structure

If means if. In fact, if conditional structure is what we often say in Chinese. If xxx is xxx

For example:

If it's sunny today, I'll dry my clothes;

If the score is below 60, I will fail

The basic if conditional structure in Java is represented as follows:

if (condition) {
    //Statement to execute
}

The above example can be expressed as follows:

if (It's sunny today) {
    I'll dry my clothes;
}
if (Less than 60 points in the exam) {
    I failed;
}

Logical operator

Multiple if conditional structure

if (condition A) {
    //Execute the logic of condition A
} else if (condition B) {
	//Execute the logic of condition B
} else if (condition C) {
	//Execute the logic of condition C
} else {
	//If conditions A, B and C are not satisfied, execute the logic in this else
}

In the multiple if conditional structure shown above, only one of the logic of condition A, the logic of condition B, the logic of condition C and the logic in else will be executed, for example:

int score = 80;
if (score >= 90) {
    //If the score is greater than or equal to 90, output "excellent"
    System.out.println("excellent");
} else if (score >= 80) {
    //If the score does not meet the condition of greater than or equal to 90 (i.e. less than 90), and greater than or equal to 80, the output is "good"
    System.out.println("good");
} else if (score >= 60) {
    //If the score does not meet the condition of greater than or equal to 80 (i.e. less than 80), and greater than or equal to 60, the output "pass"
    System.out.println("pass");
} else {
    //If the score does not meet the above three if conditions, that is, the score is less than 60, the output is "failed"
    System.out.println("fail,");
}
Note: the output result of the above program is
    good

Here is another comparison:

int score = 80;
if (score >= 90) {
    System.out.println("excellent");
}
if (score >= 80) {
    System.out.println("good");
}
if (score >= 60) {
    System.out.println("pass");
}
if (score < 60) {
    System.out.println("fail,");
}
Note: the output result of the above program is
	good
	pass

Multiple if condition judgments of the above programs do not have else, so each if condition will execute judgment. As long as an if condition is met, the statements under the if condition will be executed. So how to output the same execution result as if else? See the following procedure:

int score = 80;
if (score >= 90) {
    System.out.println("excellent");
}
if (score >= 80 && score < 90) {
    System.out.println("good");
}
//Here we use & & (logical and) to ensure that the score is greater than or equal to 60,
//Moreover, the output "pass" can only be executed when it is less than 80, and only "good" can be output when it is equal to 80
if (score >= 60 && score < 80) {
    System.out.println("pass");
}
if (score < 60) {
    System.out.println("fail,");
}
Note: the output result of the above program is
    good

Ternary expression (also called ternary operator)

Assuming variable A=10 and variable B=20, now take the maximum value of A and B (assuming the maximum value variable is max), and the following expression is A ternary expression:

//If A > B, max is taken as? The following value (i.e. A), otherwise max takes the following value (i.e. B)
int max = A > B ? A : B;

This ternary expression is equivalent to the following:

int max = 0;
if (A > B) {
    max = A;
} else {
    max = B;
}

switch conditional structure

switch (expression) {
    case Constant 1:
        //If the expression is equal to constant 1, execute statement 1
        Statement 1
        //break means to jump out of the switch statement and stop judging
        break;
    case Constant 2:
        //If the expression is equal to constant 2, execute statement 2
        Statement 2
        break;
    default:
        //If the expression does not find a matching constant, execute statement 3
        Statement 3
}

Examples are as follows:

char grade = 'B';
switch (grade) {
    case 'A':
        //If grade equals' A ', the output is "excellent"
        System.out.println("excellent");
        break;
    case 'B':
        //If grade equals' B ', the output is "good"
        System.out.println("good");
        break;
    default:
        //If grade is neither equal to 'A' nor 'B', output "unknown level"
        System.out.println("Unknown level");
}
Note: the output result of the above program is
    good

matters needing attention:

  • Constants in a case in a switch statement cannot have duplicate values
  • The expression in parentheses after switch must be integer (int), character type (char), String (String)

Comparison between switch conditional structure and multiple if conditional structure

task

1. Xiao Ming's mother gives him a red envelope. If the money in the red envelope is less than or equal to 10 yuan, it will output "buy candy"; If it is more than 10 yuan, it will output "buy a racing car", please write it in a ternary expression. Suppose the money in the red envelope is expressed by the variable money

2. The school canteen provides different dishes every day. The following conditions exist

  • If it is Monday, output "corn ribs porridge"
  • If it is Tuesday or Wednesday, output "taro fresh shrimp rice"
  • If it is Thursday, output "egg shredded meat noodles"
  • Other days, output "buffet"

Please write the corresponding program according to the above four conditions. Suppose the day of the week is represented by the variable day. The requirements are as follows:

1> Written using switch conditional structure

2> Write using if conditional structure

Keywords: Java Back-end

Added by twomt on Sun, 02 Jan 2022 03:05:27 +0200