Java Control Statement – if-else-if Statement


As we saw earlier, the if statement can have an else part attached to it. This else part can in turn contain another if statement. The if statement, coded in this manner sets up a sequence of condition’s. Each of these conditions is tested one after the other starting from top- to-bottom. The body part of the if statement is executed only if the condition executes to True; after which the statements continue to be executed in that order.

A code-snippet displaying the use of if-else-if statement is displayed below:

//Example 1
if(color_paper == YELLOW)) {
System.out.println(“The color of the paper is yellow.”);
}
else if(color_paper == RED) {
System.out.println(“The color of the paper is red.”);
}

//Example 2
if(employee.is.Engineer()) {
System.out.println(“The employee Is an Engineer”);
}
else if(employee.is.Manager()) {
System.out.println(“The employee is a Manager”);
}
else {
System.out.println(“the employee is Chairman of company”);
}

Continue reading Java Control Statement – if-else-if Statement

Java Control Statement – if-else statement

The if statement in Java can also have has an optional else part. This code snippet is executed only if the condition enclosed with if is false. The general syntax of the if statement with the optional else part is displayed below:

if (condition)

{

// body-code to be executed if condition evaluates to true

}

else

{

//   Else_code to be executed if condition evaluates to false

}

As shown above, the else_code is executed only if condition results into a false. Here is an example of the if statement with the optional else part.

Continue reading Java Control Statement – if-else statement

Java Control Statement – If statement


The if statement is a decision-making statement, used for implementing decision-making in your programs.  It is the most basic statement which control the flow of code execution, the if statement guides your program to execute desired section of the code only if a particular condition evaluation results as true.

The general syntax of the if statement in the Java language is shown below:

if (condition)
{ //Do this by performing this action}
else
{ //Do this by performing that action}

The example given below shows you a simple- if statement, as it is written in the Java language.

if (your_age >= 50)
{
System.out.println(“You are an experienced person”);
}

Let’s look at another example of the motor class to understand the if statement better. The Water class will allow to switch off the pump only if the pump is already running. Therefore, the switch_off_pump method can look like this:

void switch_off_pump(){

if (is_on){  // the “if” clause: the water pump must be on

Switch_off = TRUE; // the “then” clause: switch off the power supply to Water pump

}

}

If the above code snippet returns false (i.e. water pump is not running or power is already off), the execution flow will not execute code mentioned in the if-then statement and runs the code after that.

Continue reading Java Control Statement – If statement