Java Branching Structure-if...else/switch

Source: Internet
Author: User
Tags case statement java se

If statement
An If statement contains a Boolean expression and one or more statements.
Grammar
The syntax for the IF statement is as follows:
if (Boolean expression)
{
If the Boolean expression is true, the statement executed
}
If the value of the Boolean expression is true, the code block in the IF statement is executed, otherwise the code after the IF statement block is executed.
Test.java File Code:
public class Test {

public static void Main (String args[]) {
int x = 10;

  if( x < 20 ){     System.out.print("这是 if 语句");  }

}
}
The results of the above code compilation run as follows:
This is the IF statement
If...else statements
The If statement can be followed by the Else statement, and if the Boolean expression value of the If statement is false, the ELSE statement block is executed.
Grammar
The usage of If...else is as follows:
if (Boolean expression) {
True if the value of a Boolean expression
}else{
If the value of the Boolean expression is False
}
Instance
Test.java File Code:
public class Test {

public static void Main (String args[]) {
int x = 30;

  if( x < 20 ){     System.out.print("这是 if 语句");  }else{     System.out.print("这是 else 语句");  }

}
}
The results of the above code compilation run as follows:
This is the Else statement
If...else if...else Statements
The If statement can be followed by a elseif...else statement, which detects a variety of possible scenarios.
When using the If,else if,else statement, the following points need to be noted:
The IF statement has at most 1 else statements, and ELSE statements after all ElseIf statements.
An If statement can have several ElseIf statements, which must precede the Else statement.
Once one of the else if statements detects true, the other else if and else statements will skip execution.
Grammar
The IF...ELSE syntax format is as follows:
if (Boolean expression 1) {
If the value of the Boolean expression 1 is true, the code is executed
}else if (Boolean expression 2) {
If the value of the Boolean expression 2 is true, the code is executed
}else if (Boolean expression 3) {
If the value of the Boolean expression 3 is true, the code is executed
}else {
If none of the above Boolean expressions executes code for true
}
Instance
Test.java File Code:
public class Test {
public static void Main (String args[]) {
int x = 30;

  if( x == 10 ){     System.out.print("Value of X is 10");  }else if( x == 20 ){     System.out.print("Value of X is 20");  }else if( x == 30 ){     System.out.print("Value of X is 30");  }else{     System.out.print("这是 else 语句");  }

}
}
The results of the above code compilation run as follows:
Value of X is 30
Nested IF...ELSE statements
It is legal to use nested IF...ELSE statements. This means that you can use an if or ElseIf statement in another if or ElseIf statement.
Grammar
The nested IF...ELSE syntax format is as follows:
if (Boolean expression 1) {
If the value of the Boolean expression 1 is true, the code is executed
if (Boolean expression 2) {
If the value of the Boolean expression 2 is true, the code is executed
}
}
You can nest else if...else like an if statement.
Instance
Test.java File Code:
public class Test {

public static void Main (String args[]) {
int x = 30;
int y = 10;

  if( x == 30 ){     if( y == 10 ){         System.out.print("X = 30 and Y = 10");      }   }}

}
The result of compiling the code above is as follows:
X = A and Y = ten
Switch statement
Switch statement determines whether a variable is equal to a value in a series of values, each value is called a branch. The
Syntax
Switch syntax is formatted as follows:
switch (expression) {
Case value:
//Statement
break;//optional
Case value:
//Statement
Break Optional
//You can have any number of case statements
Default://optional
//Statement
}
Switch statement has the following rules: The variable type in the
switch statement can be: Byte, short, int, or Char Starting with Java SE 7, switch supports string types, and case labels must be string constants or literals. A
switch statement can have more than one case statement. Each case is followed by a value and a colon to compare. The data type of the value Kim in the
Case statement must be the same as the data type of the variable, and can only be constants or literal constants.
When the value of a variable is equal to the value of the case statement, the statement after the case statement starts executing until the break statement appears and jumps out of the switch statement.
When a break statement is encountered, the switch statement terminates. The program jumps to the statement following the switch statement execution. The case statement does not have to contain a break statement. If no break statement appears, the program continues to execute the next case statement until a break statement appears. A
switch statement can contain a default branch, which must be the last branch of a switch statement. Default is executed when there is no case statement with the value equal to the value of the variable. The default branch does not require a break statement.
Instance
Test.java file code:
public class Test {
public static void Main (String args[]) {
//char grade = Args[0].cha RAt (0);
Char grade = ' C ';

  switch (grade) {case ' A ': System.out.println ("excellent");     Break        Case ' B ': Case ' C ': System.out.println ("good");     Break     Case ' D ': System.out.println ("pass");        Case ' F ': System.out.println ("You need to work harder");     Break  Default:System.out.println ("Unknown level"); } System.out.println ("Your rank is" + grade);  

}
}
The result of compiling the above code is as follows:
Good
Your rank is C
Java looping structure –for, while and do...while Java number & Math class br/> note list
A Lan.huang
407***[email protected] BR uses the preceding variables and for loops, as well as the if knowledge, to develop a small program that is programmed to enter a diamond consisting of the number in the console. Take a look at the following code:
public class Demo2 {
//The following is the ascending code
public void Prit1 (float c) {
Float p = c/2;//ascending sort
Float d;//declaration Row count variable
Float e;//variable that declares the print number
Float f;//declares a variable that prints a space
float r;//declaration Ascending sort
Float s = c% 2;//modulo
if (s = = 0) {
Syst EM.OUT.PRINTLN ("The data you enter cannot form a diamond structure");
} else {
for (d = 1, d <= p; d++) {
for (f = p; f >= D; f--) {
System.out.print ("");
}
for (e = 1; e <= d 2-1; e++) {
if (e = = 1 | | e = = d
2-1) {
System.out.print ("");//If it is the first number And the last number, enter
} else {
System.out.print ("");//Otherwise Enter a space
}
}
System.out.println ();
}
}
}

 //Below is the code in reverse print public void prit2 (float m) {Float i;//declaration row number Variable float j;//Declaration print * Number variable float k;//Declare the variable that prints the number of spaces    float n = m/2 + 1;//reverse sort float o = m% 2;//m modulo if (o = = 0) {System.out.print (""); } else bjrongjinhuiyin.com {for (i = 1; I <= n; i++)//number of lines loops; {//print * before printing a space; for ( k = 0; K < i-1;            k++) {System.out.print ("");                }//The cycle of the number of * numbers is printed below, for (j = (n-k) * 2-2; J >= 1; j--)//The cycle of the number of printed * numbers; {                if (j = = (n-k) * 2-2 | | j = 1) {System.out.print ("*");                } else {System.out.print ("");        }}//After printing the * number for line printing; System.out.println ();    }}}public static void Main (string[] args) {Demo2 a = new Demo2 (); Float B = 11;//Determines whether a diamond can be formed, depending on the number of rows.    If the cardinality row can enter the corresponding diamond, if it is an even row then output "The data you entered cannot form a diamond structure";    A.prit1 (b); A.prit2 (b);}  

}br/>alan.huang
Alan.huang
407***[ Email protected]
ant Abu
163***[email protected]
A to find the function where the diamond edge is located. Dots on the edge print "", Other dots print "". Instead of distinguishing between ascending and descending, see Code:
public class draw{
int A, B,//a is the number of diamond rows to be generated
int h;//h is a parameter in a method, and the number of rows
int i,j;//i J is a looping structure parameter
public void Draw (int h) {
for (i = 1; I <= h; i++) {//progressive print
for (j = 1;j <= h;j++) {//Line print count is consistent with number of rows
//The following statement is a diamond four Edge functions, coordinate points on edges, print , otherwise print spaces
if (j = = (H + 3)/2-i | | j = (H-1)/2 + i | | j = i-(h-1)/2 | | j = = (3
h + 1)/2-i) {
System.out.print ("*");
} else{
System.out.print ("");
}
}
System.out.println ();//Line I print line wrap
}
}
public static void Main (string[] args) {//static method
Draw B = new D Raw (); Initialize Method
int a = 35;//Assign and execute Draw method
B.draw (a);
}
}

Java Branch Structure-if...else/switch

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.