Java_se base--13. Select Structure Statement

Source: Internet
Author: User

If SELECT structure Syntax:
if (condition) {        code block}

public class test{public        static void Main (string[] args) {                int a = 5;                if (a < 6) {                        System.out.print ("A less than 6");        } The output is a greater than B    }}
Description: The IF selection structure condition must be a Boolean expression that runs the code block once the value in the condition is true, otherwise skips

IF-ELSE Select structure Syntax:
if (condition) {        code block 1}else{        code block 2}

public class test{public        static void Main (string[] args) {                int a = 5;                if (a < 3) {                        System.out.print ("A less than B");                } else{                        System.out.print ("A greater than B");}    } The output is a greater than B}

Description: If-else Select structure once the value in the condition is true when code block 1 is run, otherwise code block 2 is run



Multiple if selection structure syntax:
if (condition 1) {        code block 1}else if (condition 2) {        code block 2}else{        code block 3}

public class test{public        static void Main (string[] args) {                int a = 5;                if (a > 2) {                        System.out.print ("a");                } else if (a > 3) {                        System.out.print ("B");                } else if (a > 4) {                        System.out.print ("C");                }        } The output is a}

Note: The multiple if selection structure solves the need to infer the condition is a continuous interval when there is a great advantage, else if the block can have multiple, depending on the needs of the program. Once condition 1 is true, code block 1 is run. Otherwise, run the else if block, inferring condition 2. True when code block 2 is run, otherwise code block 3 is run, and so on. If the condition satisfies an else if block then the rest will no longer run and jump out of the IF block

Nested IF selection structure syntax:
if (condition 1) {        if (condition 2) {                code block 1        }else{                code block 2        }}else{        code block 3}

public class test{public        static void Main (string[] args) {                int a = 3;                if (a > 2) {                        if (A = 3) {                                System.out.print ("a!=3");//code block 1                        }else{                                System.out.print ("a=3");//code Block 2                        }                } else{                        System.out.print ("a>2");//code block 3                }        }//output result is a=3}

Description: The nested If selection structure is in fact embedded in the if selection structure, if the condition 1 is false when the code block 3 is run, otherwise run the internal if selection structure. That is, to run code block 1, you must satisfy condition 1 and condition 2
Switch selection structure syntax:
public class Test{switch (expression) {case        constant 1:                code block 1;                break;        Case constant 2:                code block 2;                break;        Default:                code block 3;                break;    }

public class test{public        static void Main (string[] args) {                int a = 3;                Switch (a) {case                        1:                          System.out.println ("Monday");                          break;                        Case 2:                          System.out.println ("Tuesday");                          break;                        Case 3:                          System.out.println ("Wednesday");                          break;                        Default:                          System.out.println ("Sunday");                          break;                } Output is Wednesday        }}
Description: Switch selection structure
An expression can be an integer variable or a character-type variable
The case must be an integer or character constant expression, usually a fixed character, number. Case blocks can have multiplethe default block runs when no other case block is satisfiedBreak means to jump out of the current structure. Exit Switch statement blockswitch selection structure is easy to solve the equivalence inference problem compared with nested if selection structure
Do not know can add Q contact me Oh 654249738


If selection structureGrammar:

Java_se base--13. Select Structure Statement

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.