1.JAVA Branch Structure-IF statement

Source: Internet
Author: User

In Java programming, the branching structure is done by a conditional statement (if statement). The IF statement can be used either alone or with else. Here's how to use it:

    • If statements are used alone
      if (conditional expression) {
      Purpose Here is a piece of code
      }
    • Use with Else
      if (conditional expression) {
      Objective One; Here is a piece of code
      }
      else{
      Objective two; Here is a piece of code
      }


Note: The conditional expression is a relational expression, and the result is a Boolean data. take a look at the following code:
public class control1{
public static void Main (string[] args) {
int salary=10000;
if ((salary>500) &&salary) {
System.out.println ("If you want to eat! ");
SYSTEM.OUT.PRINTLN ("Want to please Sing!") ");
SYSTEM.OUT.PRINTLN ("Want to please drink!") ");
}
}
}
Operation Result:
Want to eat dinner!
Want to please sing!
You want to drink!

Parse: In conditional statement (salary>500) &&salary, salary>500 evaluates to true,salary with a value of 10000, and is converted to true,true&& when the "and" Operation is performed True, the result is true, and the condition matches.

There are a few things to keep in mind when using the good conditional statement:

      1. You should draw a flowchart to make programming more clear.
      2. When programming, in the simplest form of conditional statements, you can not use curly braces, because it does not create confusion, but it is recommended that you use curly braces regardless of the form of the conditional statement.

1.JAVA Branch Structure-IF statement

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.