If statement
An If statement consists of a Boolean expression and one or more statements.
Grammar
The syntax for the IF statement is as follows:
if (Boolean expression)
{
The statement to be executed if the Boolean expression is True
}
If the Boolean expression evaluates to True, the code block in the IF statement is executed. Otherwise, the code behind the IF statement block is executed.
public class Test {public
static void Main (String args[]) {
int x = ten;
if (x <) {
System.out.print ("This is an if statement");}}
The results of the above code compilation run are as follows:
If...else statement
If statements can be followed by the Else statement, the ELSE statement block is executed if the Boolean expression value of the IF statement is false.
Grammar
The use of If...else is as follows:
if (Boolean expression) {
If the Boolean expression evaluates to True
}else{
If the Boolean expression evaluates to False
}
Instance
public class Test {public
static void Main (String args[]) {
int x =;
if (x <) {
System.out.print ("This is an if statement");
} else{
System.out.print ("This is Else Statement");}}
The simplest example of a if-else statement
Suppose I go to the office and ask Huang Wenjiang? If he were to say in the absence of an enthusiastic colleague who answered, "He is not there," then I do not immediately understand. We use the program to simulate:
public class Demo {public
static void Main (string[] args) {
//set Huang Wenjiang not
boolean flag = false;
System.out.println ("Start");
if (flag) {
System.out.println ("on");
} else{
System.out.println ("He is not");
}
System.out.println ("End");
}