Sunday , January 24 2021
Home / Java / JAVA #12 – If,else-if,else and Ternary Operators in Java

JAVA #12 – If,else-if,else and Ternary Operators in Java

The if-then and if-then-else Statements

The if-then Statement

The if-then statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true.

Syntax of if-else and if-else statement:

if (condition) {

statements // they are executed if variable == c1

}

else if (condition) {

statements // they are executed if variable == c1

}

else {

statements // they are executed if variable == c1

}

 

For example, the Bicycle class could allow the brakes to decrease the bicycle’s speed only if the bicycle is already in motion. One possible implementation of theapplyBrakes method could be as follows:

void applyBrakes() {

// the "if" clause: bicycle must be moving

if (isMoving){

// the "then" clause: decrease current speed

currentSpeed--;

}

}

 

 

If this test evaluates to false (meaning that the bicycle is not in motion), control jumps to the end of the if-then statement.

In addition, the opening and closing braces are optional, provided that the “then” clause contains only one statement:

void applyBrakes() {

// same as above, but without braces

if (isMoving)

currentSpeed--;

}

 

Deciding when to omit the braces is a matter of personal taste. Omitting them can make the code more brittle. If a second statement is later added to the “then” clause, a common mistake would be forgetting to add the newly required braces. The compiler cannot catch this sort of error; you’ll just get the wrong results.

The if-then-else Statement

The if-then-else statement provides a secondary path of execution when an “if” clause evaluates to false. You could use an if-then-else statement in theapplyBrakes method to take some action if the brakes are applied when the bicycle is not in motion. In this case, the action is to simply print an error message stating that the bicycle has already stopped.

void applyBrakes() {

if (isMoving) {

currentSpeed--;

} else {

System.err.println("The bicycle has already stopped!");

}

}

 

The following program, IfElseDemo, assigns a grade based on the value of a test score: an A for a score of 90% or above, a B for a score of 80% or above, and so on.

 

class IfElseDemo {

public static void main(String[] args) {


int testscore = 76;

char grade;


if (testscore >= 90) {

grade = 'A';

} else if (testscore >= 80) {

grade = 'B';

} else if (testscore >= 70) {

grade = 'C';

} else if (testscore >= 60) {

grade = 'D';

} else {

grade = 'F';

}

System.out.println("Grade = " + grade);

}

}
class IfElseDemo {

public static void main(String[] args) {


int testscore = 76;

char grade;


if (testscore >= 90) {

grade = 'A';

} else if (testscore >= 80) {

grade = 'B';

} else if (testscore >= 70) {

grade = 'C';

} else if (testscore >= 60) {

grade = 'D';

} else {

grade = 'F';

}

System.out.println("Grade = " + grade);

}

}

The output from the program is:

Grade = C

You may have noticed that the value of testscore can satisfy more than one expression in the compound statement: 76 >= 70 and 76 >= 60. However, once a condition is satisfied, the appropriate statements are executed (grade = ‘C’;) and the remaining conditions are not evaluated.

Syntax of Ternary Operator:

Condition ? “true” : “false”;

The ternary operator “?:” earns its name because it’s the only operator to take three operands. It is a conditional operator that provides a shorter syntax for the if..then..else statement. The first operand is a boolean expression; if the expression is true then the value of the second operand is returned otherwise the value of the third operand is returned:

example

boolean isHappy = true; String mood = (isHappy == true)?"I'm Happy!":"I'm Sad!";

 

 

About Faheem

An adaptable and responsible Software Engineer, who enjoys working with computers and the opportunity to learn new skills and work with new technologies is particularly attractive to me.

Check Also

JAVA #32 – Java application using SQL server (Part: 01)

  In this part we made the GUI front end of a Application.

Leave a Reply

Your email address will not be published.