1. If statements are used to judge different conditions and execute different codes depending on the outcome of the judgment.
2, if the condition can be judged by the relational operation, the logical operation, if the statement according to the logical value of TRUE, false to determine the execution of different code.
3, if statement in the development of the use of extremely extensive.
if (conditional expression) {
Executes this code block when the result of the conditional expression is true
}
"Example" lose into the test Try into Performance , if you reach - points above, the display Example: into Performance Hopewell Lattice
Public Static void Main (string[] args) {
Scanner scanner=New Scanner (System. in);
System. out. println ("Input score:");
int score=scanner.nextint ();
if (score>=60) {
System. out. println (score+ "pass");
}
}
if (conditional expression) {
Executes this code block when the result of the conditional expression is true
}
else{
Execution of this code block when the result of a conditional expression is false
}
"Example" lose in two numbers, find the maximum number .
Public Static void Main (string[] args) {
Scanner scanner=New Scanner (System. in);
System. out. println ("Show first Number:");
int a=scanner.nextint ();
System. out. println ("Show second number:");
int b=scanner.nextint ();
if (A>B) {
System. out. println (A + "is the maximum number");
}Else{
System. out. println (b + "is the maximum number:");
}
}
if (conditional expression 1) {
Conditional expression 1 When the result is true, execute this code block
}Else if(...) {
......
}Else if(conditional expression N) {
When the conditional expression n evaluates to True, the code block is executed
}Else{
When the conditional expression n results in false, the code block is executed
}
"Sample" keyboard input 5 score, according to the following criteria to give results
Grade.
5 points: Excellent
4 points: Good
3 points: Pass
0~2 points: Failed
Public Static void Main (string[] args) {
Scanner scanner=New Scanner (System. in);
System. out. println ("Input 5 score:");
int score=scanner.nextint ();
if (score==5) {
System. out. println ("excellent");
}Else if(score==4) {
System. out. println ("good");
}Else if(score==3) {
System. out. println ("Pass");
}Else if(score>=0&&score<=2) {
System. out. println ("failing");
}Else{
System. out. println ("not 5 fractions");
}
}
"Sample" Keyboard input Java, Android and SQL three scores, find out the highest score
Public Static void Main (string[] args) {
Scanner scanner=New Scanner (System. in);
System. out. println ("Input Java Score:");
int java=scanner.nextint ();
System. out. println ("Enter Android Score");
int android=scanner.nextint ();
System. out. println ("Input SQL Score");
int sql=scanner.nextint ();
if (Java>=android&&java>=sql) {
System. out. println ("Highest score:" +java);
}Else if(android>=sql) {
System. out. println ("Highest score:" +android);
}Else{
System. out. println ("Highest score:" +sql);
}
}
"Example" finds the highest score of three subjects (solution 2-selection method)
Public Static void Main (string[] args) {
Scanner scanner=New Scanner (System. in);
System. out. println ("Java Android sql:");
int java=scanner.nextint ();
int android=scanner.nextint ();
int sql=scanner.nextint ();
int Maxscore=java;
if (maxscore<android) {
maxscore=android;
}
if (Maxscore<sql) {
Maxscore=sql;
}
System. out. println ("Highest score:" +maxscore);
}
1.30 if statement