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:
- You should draw a flowchart to make programming more clear.
- 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