1. Select Structure if statement format and its use
Format of the A:IF statement:
if (comparison expression 1) {
Statement body 1;
}else if (comparison expression 2) {
Statement body 2;
}else if (comparison expression 3) {
Statement Body 3;
}
...
else {
Statement body n+1;
}
B: Execution Process:
The comparison expression 1 is evaluated first to see whether the return value is true or false.
If true, executes the statement body 1,IF statement end.
If False, the comparison expression 2 is calculated to see whether the return value is true or false.
If true, executes the statement body 2,IF statement end.
If False, the comparison expression 3 is calculated to see whether the return value is true or false.
If all is false, the statement body n+1 is executed.
C: Note: The last else can be omitted, but it is not recommended to omit, can be prompted for error values outside the range
eg
Import Java.util.Scanner;
Class Demo_if {public
static void Main (string[] args) {
Scanner sc = new Scanner (system.in); Keyboard input, while
(true) {
System.out.println ("Please enter your results"); Prompt to enter
int a = Sc.nextint (); keyboard input with int type receive
if (a>100|a<0) { //dead loop, easy to test
System.out.println ("You entered a wrong grade");
} else if (a>=90&a<=100) {
System.out.println ("A,");
} else if (a>=80&a<90) {
System.out.println ("B");
} else if (a>=70&a<80) {
System.out.println ("C et");
} else if (a>=60&a<70) {
System.out.println ("D,");
} else if (a<60) {
System.out.println ("E et");
} else {
System.out.println ("You have entered a wrong grade");}}
2. Select structure switch statement format and its use
A:switch format:
switch (expression) {
Case value 1:
Statement body 1;
Break
Case Value 2:
Statement body 2;
Break
...
Default
Statement body n+1;
Break
}
Format interpretation of B:switch statements
(Basic data type, as long as it can be promoted to int, reference data type enumerations (JDK1.5) and string (JDK1.7))
C: Execution Process
Evaluate the value of an expression first
And then the match after the case, if there is to execute the corresponding statement, otherwise execute the statement of default control
eg
Import java.util.*;
Class dome_if3{public
static void Main (string[] args) {
//system.out.println ("Hello world!");
Scanner SC =new Scanner (system.in); Keyboard entry while
(true) { //Dead Loop convenience test
System.out.println ("Please enter the number of weeks to convert");//keyboard input hint in
week = Sc.nextint ();
Switch (week) {case
1:
System.out.println ("Week 1");
break;
Case 2:
System.out.println ("Week 2");
break;
Case 3:
System.out.println ("Week 3");
break;
Case 4:
System.out.println ("Week 4");
break;
Case 5:
System.out.println ("Week 5");
break;
Case 6:
System.out.println ("Week 6");
break;
Case 7:
System.out.println ("Sunday");
break;
Default:
System.out.println ("The number you entered is incorrect, please re-enter");}}
3: Summarize the respective usage scenarios for switch statements and if statements
Switch recommends determining the fixed value when using
If it is recommended to determine the interval or range
* Can do with switch, can do with if, single vice versa
This article discusses the choice of structure if statements and switch statements is the difference between the small series to share all the content, hope to give you a reference, but also hope that we support the cloud-dwelling community.