Conditional Statement in Java

0
Conditional Statement in Java 


This Statement provides a separate block for true condition and false condition using if block and else block.

If block will execute when the condition will be true and else block is just opposite of if block.

The ternary operator is only used to contain a single statement when the condition will be true or false if we want to write more then one statement in the ternary true block and false block then the ternary operator will be failed.


Type of Conditional Statement:-

1 Simple if:-

when the condition will true then only simple if statement can be used.

if(condition)
statement

if(condition)

     statement1;
     statement2;

}


WAP to increase salary of the employee from 500, if entered salary will be less then 10000 otherwise same salaries will be printed.

int sal=18000;
if(sal<10000)
sal=sal+500
System.out.println(s);



2) If--else:-

It will solve the program when the condition will true and false.

if(condition)
statement
else
statement


WAP to check that entered number is positive or negative?

int num=0
if(num>=0)
 System.out.println("positive")
else
System.out.println("negative")
.............................................................................................................


3)Nested if-else:-

if program contain more then one condition then we can create the nested if-else block.
Nested means , more than one statement in a nested pattern.

Syntax

if(condition)
{
    if(condition)
    {
         statement;
    }
     else
  {
       statement

   }



}

else
{
  if(condition)
    {
         statement;
    }
     else
  {
       statement

   }



}



Q)WAP to calculate greatest number using three different numbers using nested if-else?

class Greatest
{
   public static void main(String args[])
   {
        int a=22,b=12,c=34;
        if(a>b)
        {
               if(a>c)
                 System.out.println("a is greatest");
               else
                 System.out.println("c is greatest");
 
        }
       else
       {
                if(b>c)
                System.out.println("b is greatest");
                else
                System.out.println("c is greatest");

       }
     

   }


}

note:-  Nested block always will be preferred when we create program without using the logical operator.
...............................................................................................................................................................

4) Ladder If--else or ElseIf Statement:-

We will write more then one else if block level statement under the if-else block.
It will work step by step hence it is called ladder if-else block.

If(condition)
{

}
else if(condition)
{

}
...

else
{


}

class Greatest
{
   public static void main(String args[])
   {
        int a=22,b=12,c=34;
        if(a>b && a>c)
        System.out.println("a is greatest");
        else if(b>c)
        System.out.println("b is greatest");
        else
        System.out.println("c is greatest"); 

    }


}




5) Multiple if:-

When we require multiple conditions and multiple result then we use multiple if statement.

If(condition)
statement
If(condition)
statement



WAP to check divisibility that number is divisible by 3 and 5 both, individual and in combination?

if(num%3==0)
System.out.println("Divisible by 3");
if(num%5==0)
System.out.println("Divisible by 5");





ASSIGNMENT

Q WAP to check vowel and consonant using nested and ladder both?
Q WAP to Calculate Marksheet using five different subjects with the following condition.

1 all subject marks should be 0 to 100.

2 if only one subject mark is <33 then suppl.

3 if all subject marks is >33 then percentage and division should be calculated.

4 if a student is suppl then five bonus mark can be applied to the student to be pass then the student will be passed by grace.

5 Display Grace Subject name, distinction subject name,suppl subject name and failed subject name.

Q WAP to check the greatest using four different numbers using a ladder and nested both?





Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)