The branch structure of the Java language

Source: Internet
Author: User

1 Process Control Statement 1.1 What is a Process Control statement
    • Can control the execution process of the program
1.2 Classification of Flow control statements
    • Sequential structure
    • Select structure
    • Loop structure

2 Sequential structure
    • From the top down, proceed in turn.

3 Branching Structure 3.1 classification of branch structures
    • If statement
    • Switch statement
The format of the 3.2 if statement
    • Format 1
class   ifstruct1{    publicstaticvoid  main (string[] args) {          If(boolean expression            ) {// statement body         }}    }
    • Format 2
class   ifstruct2{    publicstaticvoid  main (string[] args) {          If(boolean expression)            {// Statement body 1        }else{            //  Statement body 2}}    }
    • Format 3
class   ifstruct3{    publicstaticvoid  main (string[] args) {          If(Boolean expression 1) {            // Statement body 1        }Elseif(Bourg killed 2) {             // Statement body 2        } Else {            // Statement Body 3        }}    }
3.3 Execution Process
    • The value of the comparison expression is evaluated first to see if its return value is true or false.
    • If true, the statement body is executed.
    • If False, the statement body is not executed.
3.4 Example
    • Format 1 Example
 Package Com.xuweiwei;  Public class Ifdemo {    publicstaticvoid  main (string[] args) {        int x = 0;         if (x ==0) {            System.out.println ("Hello");        }        System.out.println ("World");}    }
    • Format 2 Example
 Package Com.xuweiwei;  Public class Ifdemo {    publicstaticvoid  main (string[] args) {        int x = 2;         if (x% 2 = = 0) {            System.out.println ("is even");        } Else {            System.out.println ("is odd");     }}}
    • Format 3 Example
 PackageCom.xuweiwei; Public classIfdemo { Public Static voidMain (string[] args) {Charc = ' Male '; if(c = = ' man ')) {System.out.println ("You're a boy."); }Else if(c = = ' Female ') {System.out.println ("You're a girl."); }Else{System.out.println ("Your sex is temporarily unrecognizable."); }    }}

    • Example: Finding the maximum value in three values
 PackageCom.xuweiwei; Public classIftest { Public Static voidMain (string[] args) {intA = 10; intb = 20; intc = 30; if(A >b) {            if(A >c) {System.out.println ("A=" +a+ "Max"); }Else{System.out.println ("C=" +c+ "Max"); }        }Else{            if(B >c) {System.out.println ("B=" +b+ "Max"); }Else{System.out.println ("C=" +c+ "Max"); }        }    }}
3.5 Switch Statement
    • Format
Switch (expression) {// expression can be a byte short int char enumeration, JDK7 new string             Case value 1:                statement body 1;              Break ;              Case value 2:                statement body 2;              Break ;            ...             default :                statement body N;              Break ;}

    • Example:
 PackageCom.xuweiwei; Public classIftest { Public Static voidMain (string[] args) {String str= "a"; Switch(str) { CaseA: System.out.println (A);  Break;  Case"B": System.out.println ("B");  Break; default: System.out.println (Hello);  Break; }    }}

    • Example: Given an integer, please output the corresponding day of the week?
 PackageCom.xuweiwei;ImportJava.util.Scanner; Public classIftest { Public Static voidMain (string[] args) {Scanner input=NewScanner (system.in); System.out.println ("Please enter 1-7:"); intnum =Input.nextint (); if(num < 0 | | num > 7) {System.out.println ("Sorry, the input number is illegal"); } Else {            Switch(num) { Case1: System.out.println ("Monday");  Break;  Case2: System.out.println ("Tuesday");  Break;  Case3: System.out.println ("Wednesday");  Break;  Case4: System.out.println ("Thursday");  Break;  Case5: System.out.println ("Friday");  Break;  Case6: System.out.println ("Saturday");  Break; default: System.out.println ("Sunday");  Break; }        }    }}

    • Considerations for Switch
      • ①case can only be constants, cannot be variables, and values after multiple case cannot be the same.
      • ②default can be omitted, but not recommended.
      • ③break can also be omitted, but may cause penetrating phenomena.
      • ④default statements are not necessarily at the end, but are recommended at the end.
      • The end condition of the ⑤switch statement: 1, when the break statement is ended 2, execution to switch's closing curly brace ends.

4 the difference between if and switch
    • If suggested to determine the interval or range
    • Switch is recommended to use when determining fixed values

The branch structure of the Java language

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.