Conditional Constructs in JAVA
if condition, if...else, if...elseif, if...elseif ladder, switch ...case
if condition, if...else, if...elseif, if...elseif ladder, switch ...case
Java allows us to check a condition and execute certain instructions, depending on the result whether it is true or false. Such statements are called conditional construct.
The if statement:-
If conditional statement is used when the programmer wants
to perform a particular operation only when a condition is true.
If(Condition)
{
Java code
}
The condition is checked first, if the condition(boolean
expression ) is true the statements enclosed within a curly braces are
executed.
if......else:-
It is used when you have two options for execution and any
one of them has to be executed depending on the condition is true or false. The condition is checked first, if the
condition(boolean expression ) is true the statements enclosed within a curly
braces are executed otherwise the block of code enclosed with the else statements
executes.
The syntax of if...else block is
if(Condition)
{
Java code
}
else
{
Java code
}
if......else...if Ladder:-
It is used when you have more than two options for execution and any one of them has to be executed depending on the more than one condition. The condition is checked first, if the condition(boolean expression ) is true the statements enclosed within a curly braces are executed otherwise the condition with the elseif statement is checked ,whenever condition is true the block of code enclosed with that statement executes, if no condition is true the statements with the else block are executed. For example visit the given links
Calculate the courier charges
Electricity Bill program
The syntax of if...elseif ladder is
if(Condition)
{
Java code
}
elseif(condition)
{
Java code
}
elseif(condition)
{
Java code
}
else
{
Java code
}
switch....case:-
switch ...case is used to execute a particular statement, it checks for the equality of a particular variable. The syntax of the switch...case is as follows.
switch(variable)
{
case (Label):
break;
case (Label):
break;
case (Label):
break;
default:
break;
}
In switch case conditional statement switch can be considered as a lock and cases can be considered as keys. So the key that matches the lock will be executed. The difference between if and switch case is that switch case checks for the equality of a value and it cannot have a boolean expression. Switch case should be used for writing menu driven programs or programs where the range of input values are limited. for solved program related to switch case check this link
Solved Programs (class 9-Beginner Level)
Related Posts
Continue learning with these related articles
Sum of First n Natural Numbers in Java (Loop and Formula)
Find the sum of the first n natural numbers in Java with a for loop, a while loop and the formula n(n + 1)/2, with a dry run and the sums of even numbers, odd numbers, squares and cubes.
Prime Number Program in Java: Check a Number and Print Primes in a Range
Check whether a number is prime in Java by counting its factors, a faster square-root method with isPrime(), and a program that prints all prime numbers in a range.

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.