conditional construct

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
https://java4school.com/calculate-the-courier-charges
https://java4school.com/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
https://java4school.com/solved-programs-class-9-beginner

LEAVE A REPLY

Please enter your comment!
Please enter your name here