"Java Basics" Branch structure (1)

Source: Internet
Author: User

Java Branch Structure

If, if else, if ElseIf if

1 /**2 file path: G:\JavaByHands\if-else3 file name: Ifelset.java4 Write Time: 2016/6/55 Author: Zheng-hui6 Write a description: If If-else if-ifelse Else code example7 */8  Public classIfelset {9      Public Static voidMain (string[] args) {TenSystem.out.println ("--Program start--"); One         //set Incoming conditions A         inti = 3; -         //Single If -         if(I < 4) { theSystem.out.println ("3<4"); -         } -         if(I > 4) { -System.out.println ("3>4"); +         } -          +SYSTEM.OUT.PRINTLN ("---split line---"); A          at         //If-else -         if(I < 4) { -System.out.println ("3<4"); -}Else { -System.out.println ("4<3"); -         } in          -SYSTEM.OUT.PRINTLN ("---split line---"); to          +         //if if else else -         if(I < 3) { theSystem.out.println ("3<3"); *}Else if(I > 3) { $System.out.println ("3>3");Panax Notoginseng}Else { -System.out.println ("3=3"); the         } +          ASystem.out.println ("--The end of the program--"); the     } +}

Results:

Note: The condition of else in if else if default is reversed for the previous if condition

code example:

/**file path: G:\JavaByHands\if-else file name: Ifelsetest.java writing time: 2016/6/5 Author: Zheng Write a description: The Else condition in ifelse defaults to the condition of the previous if*/ Public classIfelsetest { Public Static voidMain (string[] args) {System.out.println ("--The program begins--"); //Set Conditions        inti = 99; //First Kind        if(I > 60) {System.out.println (Failed); } Else if(I > 80) {System.out.println (Ordinary); } Else if(I > 95) {System.out.println (Excellent); } Else if(i = = 100) {System.out.println (Out); } System.out.println ("---split Line---"); //int i = 99; //The second Kind        if(i = = 100) {System.out.println (Out); } Else if(I > 95) {System.out.println (Excellent); } Else if(I > 80) {System.out.println (Ordinary); } Else if(I > 60) {System.out.println (Failed); } System.out.println ("--The end of the program--"); }}

Results: Two times the results are not the same

The reason is that the condition of else defaults to the previous if condition:

        inti = 99; //First Kind        if(I > 60) {//i >SYSTEM.OUT.PRINTLN ("Pass"); } Else if(I > 80) {//i > &&! (i >)System.out.println ("normal"); } Else if(I > 95) {//i > &&! (i > &&!) (i >))System.out.println ("excellent"); } Else if(i = = 100) {//i = = &&! (i > &&!) (i > &&!) (i > )))System.out.println ("Perfect"); } System.out.println ("---split Line---"); //int i = 99; //The second Kind        if(i = = 100) {//i = =System.out.println ("Perfect"); } Else if(I > 95) {//i > &&! (i = =)System.out.println ("excellent"); } Else if(I > 80) {//i > &&! (i > &&!) (i = =))System.out.println ("normal"); } Else if(I > 60) {//i > &&! (i > &&!) (i > &&!) (i = = )))SYSTEM.OUT.PRINTLN ("Pass"); }

Obviously, if you follow the first operation, the following conditions will not exist at all ... It will never run ...

This is in practical use also need to pay attention to, logic problem ~ ~

"Java Basics" Branch structure (1)

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.