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 ("This is an if statement");
}
}
}
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 ("This is an if statement");
}else{
System.out.print ("This is the Else statement");
}
}
}
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 ("This is the Else statement");
}
}
}
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 = Y = 10");
}
}
}
}
The results of the above code compilation run as follows:
X = + and Y = 10
Switch statement
The switch statement determines whether a variable is equal to a value in a series of values, and each value is called a branch.
Grammar
The switch syntax format is as follows:
switch (expression) {
Case Value:
Statement
Break Options available
Case Value:
Statement
Break Options available
You can have any number of case statements
Default://Optional
Statement
}
The 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 of the 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.
The switch statement terminates when a break statement is encountered. 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].charat (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 results of the above code compilation run as follows:
Good
Your rank is C.
Java looping structure –for, while and do...while Java number & Math class
List of Notes
Alan.huang
407***[email protected]
Using the preceding variables and the For loop, and if knowledge, the program's function is to enter a diamond consisting of an * 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;//declaring row number variables
Float e;//declares the variable that prints the * number
Float f;//Declare a variable to print a space
Float r;//Declaration Ascending sort
float s = c% 2;//modulo
if (s = = 0) {
SYSTEM.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 ();
}
}
}

Here is the code in reverse print
public void Prit2 (float m) {
Float i;//declaring row number variables
Float j;//declares the variable that prints the * number
Float k;//variable that declares the number of spaces to print
float n = m/2 + 1;//reverse order
Float o = m% 2;//m modulus
if (o = = 0) {
System.out.print ("");
} else bjrongjinhuiyin.com {
for (i = 1; I <= n; i++)//line number loop;
{
Print a space before printing the * number;
for (k = 0; k < i-1; k++) {
System.out.print ("");
}

The following is a loop that prints the number of * numbers;
for (j = (n-k) * 2-2; J >= 1; j--)//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 of 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);
}
}
Alan.huang
Alan.huang
407***[email protected]
5 months ago (08-07)
Ant Abu
163***[email protected]
The need to print the funding platform diamond in a square, the console print out a a*a area, find the diamond edge of the function, the point on the edge of the print "*", other points to print "". Instead of distinguishing between ascending and descending, see the 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 is also the number of rows
int i,j; I j is the cyclic structure parameter
public void Draw (int h) {
for (i = 1; I <= h; i++) {//Progressive printing
for (j = 1;j <= h;j++) {//per line print count is consistent with number of rows
The following statement is a diamond four-edged function, a coordinate point on the edge, printing *, otherwise printing a space
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 out line break
}
}
public static void Main (string[] args) {//static method
Draw B = new Draw (); Initialize method
int a = 35; Assigning and executing the Draw method
B.draw (a);
}
}

Java Branching 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.