Learning tutorials for using If...else statements in Java _java

Source: Internet
Author: User

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:

This is an if statement

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"); 

} 



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.