Java Branch Structure

Source: Internet
Author: User

1. Branching structure

Any complex logic can be achieved through the "order", "branching", "looping" three basic program structures. Branching structure: The program executes different statements according to different conditions in the run.

Three types of structures:

    • If structure: Executes the statement when the condition is met, does not execute (1 paths)
    • If...else structure: Satisfies the condition execution statement, does not satisfy then executes the additional statement (two path)
    • IF...ELSE If structure: multiple conditions to meet different conditions to execute different statements (multiple paths)
1.1 If structure

Grammar:

if (conditional expression) {

statement block;

}

1) Case 1, determine if an integer is an even number

public class Test {public    static void Main (string[] args) {        int a = 4;        if (a%2==0) {        System.out.println (A + "is an even number");        }        System.out.println ("Over");}    }
1.2 If...else structure

Grammar:

if (conditional expression) {

Statement Block 1;

}else{

Statement Block 2;

}

1) Case 1, determine if an integer is an even number and output the result

public class Test {public    static void Main (string[] args) {        int a = 4;        if (a%2==0) {        System.out.println (A + "is an even number");        } else{        System.out.println (A + "not even");        }        System.out.println ("Over");}    }
3.IF...ELSE if structure (nested)

Grammar:

if (conditional expression 1) {

Statement Block 1;

}else if (conditional expression 2) {

Statement Block 2;

}else if (conditional expression 3) {

Statement Block 3;

}

1) Case 1, determine whether a number is positive, negative, or 0 and outputs the result

public class Test {public    static void Main (string[] args) {        int a = 4;        if (a>0) {        System.out.println (A + "is positive");        } else if (a<0) {        System.out.println (A + "is negative");        } else if (a==0) {        System.out.println (A + "is 0");        }        System.out.println ("Over");}    }
2.switch...case

A special branch structure that can only be used to determine integers, which may be executed from different program entrances based on the different values of an integer expression, and must be used in conjunction with break, exit the switch block after executing the corresponding statement, and do not execute the following statement.

If you do not add a break, the statement block of the other conditions below the condition will be output sequentially, after the condition is met, until a break or stop is encountered, as in case 2.

The default option can not be written, or must be placed in the latter (the final recommendation), the default match after all conditions do not match the case will execute the default statement, default can not write break, the following case 3.

Grammar:

Switch (judging integer) {

Case 1:

Statement Block 1;

Break

Case 2:

Statement Block 2;

Break

Case 3:

Statement Block 3;

Break

Default

Statement Block 4;

Break

}

1) Case 1

public class Test {public    static void Main (string[] args) {        int a = 4;        Switch (a) {case        1:        System.out.println ("a");        break;        Case 2:        System.out.println ("B");        break;        Case 3:        System.out.println ("C");        break;        Default:        System.out.println ("D");        Break;}}}    /* Run Result: d*/

2) Case 2, without break

public class Test {public    static void Main (string[] args) {        int a = 2;        Switch (a) {case        1:        System.out.println ("a");        break;        Case 2:        System.out.println ("B");        Case 3:        System.out.println ("C");                Default:        System.out.println ("D");}}}    /* Run Result: bcd*/

3) Case 3,default not written in the end, default needs to be added break, the following conditions

public class Test {public    static void Main (string[] args) {        int a = 4;        Switch (a) {        default:        System.out.println ("D");        Case 1:        System.out.println ("A");        break;        Case 2:        System.out.println ("B");        break;        Case 3:        System.out.println ("C");        Break;}}}    /* Run Result: da*/

  

Java Branch Structure

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.