Search This Blog

Conditional Statement in Java:-



It is used to solve a condition-based problem, the ternary operator only provides a single statement under the true condition and false condition but the conditional statement provides a separate block for true condition and false condition using the if, else, and else if statement.


Type of Conditional Statement:-

1)  Simple If:-  It is used to display result based on true condition,

      if(condition)
       statement;


     or

     if(condition)
    {
               multiple statements;

    }

WAP to increase the salary of employees from 500, if the salary is less then 10000 otherwise the same salaries will print?

class Salary
{
    public static void main(String args[])
    {
         int sal=8000;
         if(sal<10000)
           sal=sal+500;
         System.out.println(sal);
    }


}


2)  If--else:-

 Using this we can create a program when the condition is true and false both if block for true condition and else block for the false condition.

Syntax:-

  if(condition)
 {

 }
else
{


}

WAP to calculate square if the number is odd and calculate cube if the number is even?

class Checksq
{
    public static void main(String args[])
    {
         int num=4;
         int res=0;
         if(num%2==0)
           res=num*num*num;
         else
           res=num*num;
         System.out.println(res);
    }


}

3) Nested If--else:-

  We will solve more than one conditions using nested if-else, we will create more than one if-else statement using a nested sequence to solve multiple conditions.

if(condition)
  {
        if(condition)
       {



       }
    else
     {


      }


  }
  else
  {
          if(condition)
          {


         }
        else
        {


        }


  }


WAP to calculate the greatest number?

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


         }
        else
        {
           if(b>c)
           System.out.println("b is greatest");
           else
           System.out.println("c is greatest"); 

        }

    }


}

4) Ladder If--Else:-

It will work step by step means if the first condition will be true then it will be executed when it will be false then the next condition will be checked and finally, if no one condition will be true then else statement will be executed.

we will use if, else if, and else statement using Ladder If--Else.

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

}
...
else
{
   Statement;
}


WAP to check the greatest number using Ladder if-else?


class Greatest
{

public static void main(String args[])
{
   int a=10000,b=3000,c=2000;
   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:-

We can write more then one if statement to solve multiple conditions with multiple results.

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

WAP to check divisibility of number that number is divisible by 3,5 and 9 with all combinations?

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



Assignment of If--else:

Q1)  WAP to display yes, no, and cancel when the user assigns y,n, and c?

  char ch='y';
  if(ch=='y')
   System.out.println("Yes")
  else if(ch=='n')
   System.out.println("NO");
  else if(ch=='c')
   System.out.println("Cancel")
   else
     System.out.println("Other");


Q2)  WAP to check 1 digit, 2 digits,3 digits, and above 3 digit number?

class Checkmiddle
{

   public static void main(String args[])
   {
      int a=10,b=12,c=340;
      if(a>b && a<c || a<b && a>c)
      System.out.println("a is middle number");
      else if(b>a && b<c || b<a && b>c)
      System.out.println("b is middle number");
      else
      System.out.println("c is middle number");  


   }


}

Q3) WAP to find the middle number in three different numbers?




Q4)WAP to create a mark-sheet of Students using five different subjects with the following condition?

1)  All Subject Marks Should be 0 to 100.

2) If only Subject Mark is <33 Then Student will Suppl

3) If Minimum Two Subject Marks is <33 Then Student Will Fail

4) IF all Subject Marks is > 33 then percentage and division should be calculated.

5)  IF only one subject Mark is >28 and <33 then 5 grace marks will be applied and the student will be pass by grace.

6)  Display Grace Subject Name, Distinction Subject name, Supp Subject name, and Failed Subject name. 



The solution of This Program:-

class Marksheet
{

public static void main(String args[])
{

   int m1=85,m2=86,m3=67,m4=68,m5=84;
   if((m1>=0 && m1<=100) && (m2>=0 && m2<=100) && (m3>=0 && m3<=100) && (m4>=0 &&

m4<=100) && (m5>=0 && m5<=100))
{
     int c=0; 
     int mark=0;
     String sub="";
     String dist="";
       if(m1>=75)
     {
       dist+= " Physics "; 
     }
      if(m2>=75)
     {
       dist+= " Chemistry ";
     }
      if(m3>=75)
     {
       dist+= " Maths ";
     }
      if(m4>=75)
     {
       dist+= " English ";
     }
      if(m5>=75)
     {
       dist+= " Hindi ";
     }
     if(m1<33)
     {
     sub+= " Physics ";
     c++;
     mark=m1;
     }
     if(m2<33)
      {
     sub+= " Chemistry ";

     c++;
     mark=m2;
     }
     if(m3<33)
      {
     sub+= " Maths ";
     c++;
     mark=m3;
     }
     if(m4<33)
     {
     sub+= " English ";
     c++;
     mark=m4;
     }
     if(m5<33)
      {
     sub+= " Hindi ";
     c++;
     mark=m5;
     }
     if(c==0 || (c==1 && mark>=28))
     {
          float per = (m1+m2+m3+m4+m5)/5;
          if(per>33 && per<45)
          System.out.println("Pass with third division");
          else if(per<60)
          System.out.println("Pass with second division");
          else
          System.out.println("Pass with first division");
          if(c==1)
          System.out.println("Pass by grace with "+(33-28)+ " Grace Subject name is

"+sub);
         if(dist!="")
         System.out.println("Distinction Subject name is "+dist);
     }
    else if(c==1)
    {
       System.out.println("Try again you are suppl in "+sub);

    }
   else
   {
      System.out.println("Sorry you have failed in "+sub);

   }


}
else
{
   System.out.println("entered subject mark should be 0 to 100");   

}


}



}



2 Comments

If you have any doubt in programming or join online classes then you can contact us by comment .

  1. //WAP to check 1 digit, 2 digit,3 digit and above 3 digit number?
    import java.util.Scanner;
    class digit
    {
    public static void main(String args[])
    {
    Scanner jk=new Scanner(System.in);
    System.out.println("enter any number");
    int in=jk.nextInt();
    if(in>=0||in<=0)
    {
    in /=10;
    if(in==0)
    System.out.println("1 Digit");
    }
    if(in>0||in<0)
    {
    in/=10;
    if(in==0)
    System.out.println("2 Digits");


    }
    if(in>0||in<0)
    {
    in/=10;
    if(in==0)
    System.out.println("3 Digits");
    else
    System.out.println("more than 3 Digits");

    }






    }


    }

    ReplyDelete
  2. Complete Solution of Marksheet program,Challenging program to you
    class Marksheet
    {
    public static void main(String args[])
    {
    int m1=95,m2=75,m3=67,m4=37,m5=29;
    String s1="phy",s2="chem",s3="maths",s4="english",s5="hindi";
    float per;
    String sub="";
    String dist="";

    if((m1>=0 && m1<=100) && (m2>=0 && m2<=100) && (m3>=0 && m3<=100) &&

    (m4>=0 && m4<=100) && (m5>=0 && m5<=100))
    {
    int c=0;
    int g=0;
    if(m1<33)
    {
    g=m1;
    sub=sub+s1+" ";
    c++;
    }
    if(m2<33)
    {
    c++;
    g=m2;
    sub=sub+s2+" ";

    }
    if(m3<33)
    {
    g=m3;
    c++;
    sub=sub+s3+" ";
    }
    if(m4<33)
    {
    g=m4;
    c++;
    sub=sub+s4+" ";
    }
    if(m5<33)
    {
    g=m5;
    c++;
    sub=sub+s5+" ";
    }
    if(m1>=75)
    dist = dist +s1 + " ";
    if(m2>=75)
    dist = dist +s2 + " ";
    if(m3>=75)
    dist = dist +s3+ " ";
    if(m4>=75)
    dist = dist +s4 + " ";
    if(m5>=75)
    dist = dist +s5 + " ";


    if(c==0 || (c==1 && g>=28))
    {
    per=(m1+m2+m3+m4+m5)/5;
    if(per>=33 && per<45)
    System.out.println("Third division " + per + "%");
    else if(per<60)
    System.out.println("Second division " + per + "%");
    else
    System.out.println("First division " + per + "%");
    if(g>0)
    System.out.println("Pass by grace and grace mark is "+(33-g) +"

    grace subject is "+sub);
    if(dist!="")
    System.out.println("Distinction Subject name is "+dist);

    }
    else if(c==1)
    {

    System.out.println("suppl subjects is "+sub);
    }
    else
    {
    System.out.println("fail subjects is "+sub);

    }
    }
    else
    {
    System.out.println("Subject marks should be 0 to 100");

    }

    }



    }

    ReplyDelete

Post a Comment

If you have any doubt in programming or join online classes then you can contact us by comment .

Previous Post Next Post