【 Related links 】
Last one : Xiaobaixue Java The first 9 course : Operator ( Two )
Next : Xiaobaixue Java The first 11 course :switch Multi branch statement
general programme : Returns a list of
【1】 understand if Conditional statements
if It's a word in English , I believe that as long as you can lose English, you will know its meaning , you 're right , It is " If " It means .
From the literal meaning of understanding , The word is usually associated with an uncertain event . such as : If tomorrow is anything , I'm just what I am . That is to say, what I have to do is decided according to the result of tomorrow , Such a statement is a conditional statement . such as : If it doesn't rain tomorrow , I'll go out and play , Or I'll stay at home . So this statement is programmed if To translate is :
if( It won't rain tomorrow ) {
I'll go out and play
} else {
I'll just stay at home
}
In this code else In English it means “ Other ” Meaning , That is, if the weather is anything else ( It's raining ) situation , I'll just stay at home .
【2】if ... else ...
The next dice , Everyone should have played , Divided into 1,2,3,4,5,6 spot , If only size , that 1、2、3 For small ,4、5、6 For big .
Then it can also be expressed as :
if( points <=3 ) {
Small
} else {
Big
}
Or for :
if( points > 3 ) {
Big
} else {
Small
}
I believe many friends have seen this if Using the method of , you 're right , When if If the condition in the parentheses after is true , Then execute the following statement , Otherwise, it will be executed else The words in it .
Everyone should begin to understand the theory , Now let's run the code , The new class Test09, And add the following code :
package com.java;
/**
* if Conditional statements
*/
public class Test10 {
public static void main(String[] args) {
int x = 2;
if (x <= 3) {
System.out.println(" Small ");
} else {
System.out.println(" Big ");
}
}
}
Run the program : You can see that when the program x be equal to 2 When , The output is zero " Small ".
We will x The initial value is changed to 6, Then try running the program again :
You can see , This time it's printed else Inside the braces “ Big ”.
Some friends have thought of the binocular operator in the last lesson after reading here .
Yes , stay if ... else ... In this structure , He and the ternary operator can be converted to each other .
For example, trinomial operations :
int x = 10;
int y = (x < 10) ? 0 : 1;
It can be transformed into equivalent if ... else ...
int x = 10;
int y;
if (x < 10) {
y = 0;
} else {
y = 1;
}
Let's verify through the program that :
The result of the binocular operation :
if Calculation results :
It can be seen that the two results are consistent .
As for when to use the ternary operator , When to use if What about conditional statements ? This will do both , Look at your hobbies .
Generally speaking , The code of ternary operator is less , Code reduction , But compared with if Compare sentences , The code is less readable , When the logic of the code is not high , You can use it according to your hobby .
But in teamwork , If you encounter huge and complex operational logic , Suggest using if , The code written in this way can be easily understood by others , To achieve the goal of joint cooperation .
【3】if ... else if ... else...
In the game of size , Our dice are big and small . But if you play big and small ,1 and 2 For small ,3 and 4 For in the ,5 and 6 For big , Looks like if ... else ... This structure does not support , Because he has only two situations . But we can go through another structure if ... else if ... else... To show it , As follows :
if( Points for 1 or 2) {
Small
} else if( Points for 3 or 4) {
in
} else {
Big
}
We change the code and execute it : You can see , When x by 3 When , Only the middle bracket condition holds , So output the following statement .
Let's give you another example , Here is New age segment :
Suppose we use variables age It means age , So we can express it as :
This is a combination of the first 8 The comparison operator knowledge of the lesson , If you have mastered , Here should be the second to understand .
if(age <6) {
Young children
} else if(age >=7 && age <= 12) {
a juvenile
} else if(age >=13 && age <= 17) {
teenagers
} else if(age >=18 && age <= 45) {
youth
} else if(age >=46 && age <= 69) {
middle-aged
} else {
Big
}
【 Related links 】
Last one : Xiaobaixue Java The first 9 course : Operator ( Two )
Next : Xiaobaixue Java The first 11 course :switch Multi branch statement
general programme : Returns a list of