if conditional statements are divided into three syntax formats, each of which has its own characteristics
1.if statements
An If statement is a sort of processing if a condition is met
Formula:
if (conditional statement) {
Execute the statement;
......
}
1 public class pd{ 2 Public static void main (string[] args) { 3 int time = 5; 4 if (Time<7" { 5 System.out.println ("The alarm is ringing, it's time to get up!") " 6 } 7 } 8 }
Result: The output alarm will be ringing, it's time to get up!
2.if...else statements
if (judging condition) {
EXECUTE Statement 1
......
}else{
EXECUTE Statement 2
......
}
1 Public classPd {2 3 Public Static voidMain (string[] args) {4 intA = 4,b = 7;//define two int integer variables5 intMax;6 if(a>b) {7Max =A;8}Else{9Max =b;Ten System.out.println (max); One } A } -}
Result: The output will be 7
3.if...else if...else Statements
if (judging condition 1) {
EXECUTE Statement 1
} else if (judging condition 2) {
EXECUTE Statement 2
}
...
else if (judging condition N) {
EXECUTE statement N
} else {
Execute Statement n+1
}
1 Public classpd{2 Public Static voidMain (string[] args) {3 intScore = 103;4 if(Score>=90 && score<=100){5System.out.println ("excellent");6}Else if(Score>=60 && score<90){7SYSTEM.OUT.PRINTLN ("Qualified");8}Else if(Score>=0 && score<60){9System.out.println ("Failed");Ten}Else{ OneSYSTEM.OUT.PRINTLN ("Error or exception"); A } - } -}
Result: Error or exception is output
Java Selection Structure if