Java 0 Basic Series 005--condition control

Source: Internet
Author: User

 Public classControl { Public Static voidMain (string[] args) {//the common conditional control statements in Java have if-esle,switch statements,        BooleanTR =true; BooleanFA =false; //The parentheses after the IF statement are logical, and the result is a Boolean type, and when the result in parentheses is true, the program runs the statement in curly braces, and no one runs the else inside. System.out.println ("*******************if-else*************************"); if(TR) {System.out.println (TR); }        Else{System.out.println (FA); }                //There are also multiple optional else if, as in the following statement, when A=1, executes only the first statement inside, and when a=2 executes only the 2nd Curly brace statement, and so on        intA = 1; if(a==1) {System.out.println ("I'm in the first choice."); }        Else if(a==2) {System.out.println ("I'm in the second choice."); }        Else if(a==3) {System.out.println ("I'm in a third choice."); }        Else if(a==4) {System.out.println ("I'm in the fourth choice."); }        Else{System.out.println ("I'm not anywhere."); }                //switch is also a SELECT statement, but it simplifies the if statement, which has the same effect as the above if statement. Switch brackets can only be of type int or type that could be automatically converted to int (short, byte, etc.)System.out.println ("*******************switch-case*************************"); Switch(a) { Case1: System.out.println ("I'm in the first choice.");  Break;//in a switch statement, if you do not write a break, the program executes the statements in the other case in turn until you meet the break or execute the statement inside the default.             Case2: System.out.println ("I'm in the second choice.");  Break;  Case3: System.out.println ("I'm in a third choice.");  Break;  Case4: System.out.println ("I'm in the fourth choice.");  Break; default://default equals ElseSystem.out.println ("I'm not Anywhere"); }                //in JDK7, the function of switch is enhanced so that the judgment inside the switch parentheses can make the string, as followsSystem.out.println ("*******************jdk_switch-case*************************"); String name= "Peng"; Switch(name) { Case"Dong Dahai": System.out.println ("I'm in the first choice.");  Break;//in a switch statement, if you do not write a break, the program executes the statements in the other case in turn until you meet the break or execute the statement inside the default.         Case"Xiao Hui": System.out.println ("I'm in the second choice.");  Break;  Case"Peng": System.out.println ("I'm in a third choice.");  Break;  Case"Xiao Dong": System.out.println ("I'm in the fourth choice.");  Break; default://default equals ElseSystem.out.println ("I'm not Anywhere"); }    }}

Operation Result:

Java 0 Basic Series 005--condition control

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.