Outline
4.1 Compound statement: Do not focus
4.2 Article statements
Overview:
Conditional statements can execute different statements based on different conditions. The conditional statement includes the IF statement and the switch multi-branch statement.
4.2.1 If condition statement
The IF condition statement is an important programming statement,
1. Simple if statement
Format: if (logical expression) {statement sequence};
< example >int a=1;
if (a==1) System.out.prnt (the value of "a" is "+a");//Only one sentence can be output without curly braces, but preferably with
2.if....else statements
else is exactly the opposite of the "logical expression" in if. else only follow one of the above if, for other if not cold.
Format: if (logical expression) {Several statements}else{several statements}
3.if () {}else if () {} Multi-branch statement
A multi-branch statement is used to handle a variety of situations for an event: If a certain condition is met, some processing is performed, otherwise the other processing is performed if another condition is met.
Format: if (logical expression 1) {several statements 1}
else if (logical expression 2) {several statements 2}
. . .
else if (logical expression N) {several statements n}
The IF statement executes only a command statement with a condition of true, and no other statements are executed.
Practice:
(a) Practise judging the parity of any number.
System.out.print ("Please enter any number of x=");
Scanner x=new Scanner (system.in);
int N=x.nextint ();
if (n%2==0)
{
SYSTEM.OUT.PRINTLN ("You have entered an even number");
}
Else
{
SYSTEM.OUT.PRINTLN ("You have entered an odd number");
}
(ii) any two-digit comparison size
Compare Size method One (?:)
int n1=10;
int n2=11;
String jieguo= (n1==n2)? " Two numbers equal ":" Two Results unequal: "+ ((n1>n2)?" N1>n2 ":" N2>n1 ");
System.out.println (Jieguo);
Method Two If
System.out.print ("Please enter n1=");
Scanner n1=new Scanner (system.in);
Double n1=n1.nextdouble ();
System.out.print ("Please enter n2=");
Scanner n2=new Scanner (system.in);
Double n2=n2.nextdouble ();
if (N1==N2)
{
System.out.println ("The two numbers you entered are equal!");
}
Else
{
System.out.println ("The two numbers you entered are not equal");
if (N1>N2)
{
System.out.println ("n1>n2");
}
Else
{
System.out.println ("n1<n2");
}
}
4.2.2switch Multi-Branch statement
Format:
switch (expression)
{
CASE constant 1:
Language block one;
Break
......
Case constant 2:
Language block two;
Break
Default
Statement block N+1;
Break
}
The value of an expression in a switch statement must be an integer (not long) and a character type, and in a switch, constants must differ from each other.
In switch, the value of a constant expression after a case statement can be an integer, but it must never be a real number, such as the following code is not valid:
Case 1.1; Error!!!!!!!
The value of a constant expression can be a character, but it must not be a string, for example, the following characters are also illegal:
Case "OK"; Error!!!!!!
< cases >
Comparison of Swich equivalents
System.out.print ("Please enter today is the day of the Week (0-1):");
Scanner d=new Scanner (system.in);
int Week=d.nextint ();
Switch (week)
{
Case 0:
System.out.println ("Sunday");
break;//Interrupt
Case 1:
System.out.println ("Monday");
Break
Case 2:
System.out.println ("Tuesday");
Break
Case 3:
System.out.println ("Wednesday");
Break
Case 4:
System.out.println ("Thursday");
Break
Case 5:
System.out.println ("Friday");
Break
Case 6:
System.out.println ("Saturday");
Break
Default
SYSTEM.OUT.PRINTLN ("Please re-enter, Range 0-6");
Break
}
4 Java Statement 4.2-piece statement