Java Conditional Statement

Conditional statements are used for making decisions based on conditions.Using conditional statements we can control the flow of program in such a way so that it executes certain statements based on the outcome of a condition.These statements help to jump from one part of program to another depending if a particular condition is satisfied or not.


Note: This is a test or expression that evaluates to either true or false.

Note: It is a block of code that executes only if a condition is true.

if statement

The if statement in java is the most basic form of conditional statement. It allows your program to execute a block of code only when the Test Expression is true.

Syntax:

if(condition){
//code to be executed if condition is true
}

#java program to demonstrate if statement

Program-1


/*OUTPUT*/

you are gifted a pendrive


Note: If the condition (total_purchase>=1000) is true the statement will be executed.If the condition is false the statement will not be executed.

Program-2


/*OUTPUT*/

10 is a positive number

if-else statement

There are many situations where simply skipping code based on a false test expression isn't enough.We need a way for the program to react differently based on whether the Test Expression is true or false. If it's false, we don't want to skip code, but rather execute a separate block entirely.


This is where conditional statements like if-else come in.


In java if-else statements, if the condition evaluates to true the code within the “if” block is executed otherwise the code within the “else” block is executed.


Note: “Else” block is optional in code.There can only be one “else” block per “if” statement.


Syntax:

if (Test Expression):
{ // if body }
else:
{//else body}

Example:-

if (it's not raining):
{ Tell your friend: "Go play outside!" }
else: { Tell your friend: "Stay inside and read a book." }

#java program to demonstrate if-else statement

Program-1: Program to check if a number is even or odd


/*OUTPUT*/

Enter a number: 33
33 is odd.

Program-2: Program to check if a person is eligible to vote based on age


/*OUTPUT*/

Enter your age: 34
You are eligible to vote.

Nested-if statement

In java, nested if statements allows you to multi-level decision-making logic within your code. When an entire if-else statement can be written within either the body of the if statement or the body of an else statement.This is called ‘nesting’ of ifs.


Note:when you nest ifs, the main thing to remember is that an else statement always refers to the nearest if statement that is within the same block as the else and that is not already associated with an else.

Syntax:

if (condition1) {
// Code block to execute if condition1 is true

if (condition2) {
// Code block to execute if both condition1 and condition2 are true
} else {
// Code block to execute if condition1 is true but condition2 is false
}
} else {
// Code block to execute if condition1 is false
}


Program-1: A java program to find the largest number among three numbers.


/*OUTPUT*/

Enter value of a 30
Enter value of b 40
Enter value of c 33
b is greater: 40

Program-2: A java program to check if a given number is positive, negative or zero.


/*OUTPUT*/

The number is negative.

If-else-if ladder

An if-else-if ladder is a control structure in programming that is based upon a sequence of nested ifs.

Syntax:

if(condition)
statement;
else if(condition);
statement;
else if(condition);
statement;
. . . . . . . . .
. . . . . . . . .
else
statement;



Working:

The if statements are executed from the top down.As soon as one of the conditions controlling the if is true,the statement associated with that if is executed, and the rest of the ladder is bypassed.If none of the conditions is true, then the final else statement will be executed.


The final else statement is optional and serves as a default case if none of the conditions are true;that is, if all other conditional tests fail, then the last else statement is performed.If there is no final else and all other conditions are false,then no action will take place.

#Java program to demonstrate if-else-if ladder


Program-1: A java program to calculate IncomeTaxCalculator



/*OUTPUT*/

Enter total amount of your income: 4000000
Your tax is: 1600000


Program-2:A Java program for grading systems.



/*OUTPUT*/

Enter the marks obtained (0-100):
50
Your grade is: E


Previous Next