JAVA basics/Lesson 16th: Control Process/java if and if else, else if, and SWITCH condition statements, elseswitch

Source: Internet
Author: User

JAVA basics/Lesson 16th: Control Process/java if and if else, else if, and SWITCH condition statements, elseswitch

1. ifif (expression 1) {expression 2;} If expression 1 is true, execute expression 2.
Public class HelloWorld {public static void main (String [] args) {boolean B = true; // print if yes (B) {System. out. println ("yes ");}}}
2. Multiple Expressions and one expression
Public class HelloWorld {public static void main (String [] args) {boolean B = false; // if there are multiple expressions, a large Arc must be used for inclusion. if (B) {System. out. println ("yes1"); System. out. println ("yes2"); System. out. println ("yes3");} // otherwise expression 2 3 executes if (B) System no matter whether B is true or not. out. println ("yes1"); System. out. println ("yes2"); System. out. println ("yes3"); // if there is only one expression, you do not need to write an arc. It looks a little simpler if (B) {System. out. println ("yes1");} if (B) System. out. println ("yes1 ");}}
3. if problems may occur during use

In row 3, if has a semicolon, And the semicolon is also a complete expression.
If B is true, the semicolon is executed, and then yes is printed.
If B is false, do not execute this semicolon and print yes
In this way, it seems that yes will be printed anyway.

public class HelloWorld {    public static void main(String[] args) {         boolean b = false;         if (b);            System.out.println("yes");     }}
4. if else

Else indicates not true

public class HelloWorld {    public static void main(String[] args) {         boolean b = false;         if (b)            System.out.println("yes");        else            System.out.println("no");     }}
5. else if

Else if is multi-condition judgment

Public class HelloWorld {public static void main (String [] args) {// if only if is used, int I = 2 will be judged four times; if (I = 1) system. out. println (1); if (I = 2) System. out. println (2); if (I = 3) System. out. println (3); if (I = 4) System. out. println (4); // if else if is used, once the judgment is true in 18 rows, the Judgment of 20 rows and 22 rows will not be executed, saves computing resources if (I = 1) System. out. println (1); else if (I = 2) System. out. println (2); else if (I = 3) System. out. println (3); else if (I = 4) System. out. println (4 );}}
6. switch

The swich statement is equivalent to another expression of if else. switch can use byte, short, int, char, String, enum.

Note: Each expression ends with a break;
Note: String is not supported before Java 1.7. Java supports switch to use String from 1.7. After compilation, the String is converted to the hash value, which is actually an integer.
Note: enum is an enumeration type, which is described in detail in the enumeration section.

Public class HelloWorld {public static void main (String [] args) {// if you use if else int day = 5; if (day = 1) System. out. println ("Monday"); else if (day = 2) System. out. println ("Tuesday"); else if (day = 3) System. out. println ("Wednesday"); else if (day = 4) System. out. println ("Thursday"); else if (day = 5) System. out. println ("Friday"); else if (day = 6) System. out. println ("Saturday"); else if (day = 7) System. out. println ("Sunday"); else System. out. println ("what is this? "); // If you use switch (day) {case 1: System. out. println ("Monday"); break; case 2: System. out. println ("Tuesday"); break; case 3: System. out. println ("Wednesday"); break; case 4: System. out. println ("Thursday"); break; case 5: System. out. println ("Friday"); break; case 6: System. out. println ("Saturday"); break; case 7: System. out. println ("Sunday"); break; default: System. out. println ("what is this? ");}}}

 

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.