Java SE Process Control statements IF and switch statements

Source: Internet
Author: User

the use of the Process Control statement if is:
The first form of:
if (boolean expression)
{
//code to be executed
}
The second form of:
if (boolean expression)
{
//code to be executed
}
Else
{
//code to be executed
}
The third form of:
if (boolean expression)
{
//code to be executed
}
else if (boolean expression)
{
//code to be executed
}
else if (boolean expression)
{
//code to be executed
}
Else
{
//code to be executed
}

public class Iftest
{
public static void Main (string[] args)
{
int a = 3;
int b = 2;

if (a > B)
{
System.out.println ("a > B");
}
else if (a < b)
{
System.out.println ("a < B");
}
Else
{
System.out.println ("a = = B");
}
}
}


switch statement, using the form:
switch (variable)//variable Type here there are only 4 types available for the current study: Byte, short, int, char.
{
Case constant 1:
//code to be executed
Break ;
Case constant 2:
//code to be executed
Break ;
Case constant 3:
//code to be executed
Break ;
Default:
//code to be executed
}

 Public classswitchtest{ Public Static voidMain (string[] args) {intA = 3; Switch(a) { Case3: System.out.println ("A = = 3");  Break;  Case1: System.out.println ("A = = 1");  Break;  Case2: System.out.println ("A = = 2");  Break; default: System.out.println ("Default"); }        if(3 = =a) {System.out.println ("A = = 3"); }        Else if(1 = =a) {System.out.println ("A = = 1"); }        Else if(2 = =a) {System.out.println ("A = = 2"); }        Else{System.out.println ("Default"); }    }}
Switch


Although the break in a case statement is optional, in most cases, if there is no break, the logic of the program will be wrong, so it is often necessary to add a break.

Java SE Process Control statements IF and switch statements

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.