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