Java, .NET, PHP, PYTHON, ANGULAR, ML, Data Science, Testing, CI Tutorials in Easy Languages.

"Best Software Training, Internship, Project Development center of Indore India, Helpline 780506-3968"

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;

    }


note:-  we should use {} when we want to contain multiple statements.

WAP to increase the salary of employees from 500, if the salary is less than 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);
    }


}



/*WAP to pass student by grace if entered subject mark is >28 otherwise same mark 

will display.*/
import java.util.Scanner;
class CalculateMarks
{
    public static void main(String args[])
    {
          Scanner sc = new Scanner(System.in);
          System.out.println("Enter number");
       
          int mark,g;
          mark = sc.nextInt();
          if(mark>=28 && mark<33)
           {
            g = 33-mark; 
            mark =mark+g;
            System.out.println("Grace marks is "+g);
           }
          System.out.println("mark is "+mark);
            

    }


}

2)  If--else:-

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

Syntax:-

  if(condition)
 {
      Statements;
 }
else
{

    Statements;



WAP to calculate square if the number is odd and calculate the 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.


Syntax:-

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"); 

        }

    }


}


Limitation:-

Its structure is complex for multiple conditions, for example, if we want to check maximum using five different numbers?





4) Ladder If--Else or Else If Block:-

It will work step by step means if the first condition will 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.


Syntax of Ladder if else if

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 than one if statement to solve multiple conditions with multiple results.


Syntax of Multiple If:-


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?



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

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");  


   }


}


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 Subjects 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 passing 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");   

}


}



}





16 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
  3. class Digits
    {
    public static void main(String args[])
    {
    int num=623673278;
    int i=0;
    int temp;
    temp=num;
    for(num=temp;num>=0;num--)
    {
    if(num!=0)
    {
    num=num/10;
    i++;
    }
    }
    System.out.println("The number is a "+i+" digit number.");
    }
    }

    ReplyDelete
  4. import java.util.Scanner;
    class salaryofEmploy
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("salary of employ");
    int salary=sc.nextInt();

    if(salary<10000)
    salary=salary+500;
    System.out.println(salary);
    }
    }

    ReplyDelete
  5. import java.util.Scanner;
    class ODDEVEN
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter number...");
    int num=sc.nextInt();

    if(num%2==0)
    {
    num=num*num*num;
    System.out.println(num);
    }
    else
    {
    num=num*num;
    System.out.println(num);
    }
    }
    }

    ReplyDelete
  6. class Greatestnoabc
    {
    public static void main(String[]arg)
    {
    int a=2, b=8, c=34;

    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 greater");
    }
    else
    {
    System.out.println("c is greater");
    }
    }
    }
    }

    ReplyDelete
  7. class Greatestnousingladderifelse
    {
    public static void main(String[]arg)
    {
    int a=10000, b=30000, c=2000;

    if(a>b && a>c)
    System.out.println("a is the greatest number");
    else if(b>c)
    System.out.println("b is the greatest number");
    else
    System.out.println("c is the greatest number");
    }
    }

    ReplyDelete
  8. import java.util.Scanner;
    class YesNoCancleusingifelse
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter the value");
    char ch=sc.next().charAt(0);
    if(ch=='y')
    System.out.println("yes");
    else if(ch=='n')
    System.out.println("no");
    else if(ch=='c')
    System.out.println("cancle");
    else
    System.out.println("other");
    }
    }

    ReplyDelete
  9. import java.util.Scanner;
    class Check1digit2digit3digitndabove3digitnumber
    {
    public static void main(String[]arg)
    {
    Scanner sc=new Scanner(System.in);
    System.out.println("enter digits...");
    int num=sc.nextInt();

    if(num<=9)
    {
    System.out.println("1");
    }
    else if(num<=99)
    {
    System.out.println("2");
    }
    else if(num<=999)
    {
    System.out.println("3");
    }
    else
    {
    System.out.println("above 3 digit");
    }
    }
    }

    ReplyDelete
  10. class Marksheet
    {
    public static void main(String[]arg)
    {
    int m1=29, m2=86, m3=80, m4=89, m5=66;

    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" +5+"grace sub 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 sub mark should be 0 to 100");
    }
    }
    }

    ReplyDelete
  11. //WAP to increase the salary of employees from 500, if the salary is less than 10000 otherwise the same salaries will print

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

    ReplyDelete
  12. // WAP to pass student by grace if entered subject mark is >28 otherwise same mark


    import java.util.Scanner;
    class Grace
    {
    public static void main(String args[])
    {
    Scanner sc= new Scanner(System.in);
    System.out.println("Enter number");
    int m, g;
    m=sc.nextInt();
    if(m>=28 && m<33);
    {
    g=33-m;
    m=g+m;
    System.out.println("gracenumber" +g);
    }
    System.out.println("marksobtain" +m);
    }
    }

    ReplyDelete
  13. //WAP to calculate square if the number is odd and calculate the cube if the number is even


    class Calcubs
    {
    public static void main(String[]args)
    {
    int a=9;
    if(a%2==0)
    System.out.println("cube");
    else
    System.out.println("square");
    }
    }

    ReplyDelete
  14. import java.util.Scanner;
    class Oec
    {
    public static void main(String[]args)
    {
    Scanner sc= new Scanner(System.in);
    System.out.println("enter number");
    int num=sc.nextInt();

    if(num%2==0)
    {
    System.out.println("square");
    }
    else
    {
    System.out.println("cube");
    }
    }
    }

    ReplyDelete
  15. class Marksheets
    {
    public static void main(String[]args)
    {
    int s1=27, s2=88, s3=65, s4=55, s5=29;

    if (s1>=28 && s1<33)
    {
    System.out.println("pass with grace");
    }
    if (s1>33)
    {
    System.out.println( "pass" +s1);
    }

    else
    {
    System.out.println("fail" +s1);
    }
    if (s2>=28 && s2<33)
    {
    System.out.println("pass with grace");
    }
    if (s2>33)
    {
    System.out.println( "pass" +s2);
    }

    else
    {
    System.out.println("fail" +s2);
    }
    if (s3>=28 && s3<33)
    {
    System.out.println("pass with grace");
    }
    if (s3>33)
    {
    System.out.println( "pass" +s3);
    }

    else
    {
    System.out.println("fail" +s3);
    }
    if (s4>=28 && s4<33)
    {
    System.out.println("pass with grace");
    }
    if (s4>33)
    {
    System.out.println("pass" +s4);
    }

    else
    {
    System.out.println("fail" +s4);
    }
    if (s5>=28 && s5<33)
    {
    System.out.println("pass with grace");
    }
    if (s5>33)
    {
    System.out.println("pass" +s5);

    }
    else
    {
    System.out.println("fail" +s5);
    }
    if (s1>=33 && s2>=33 && s3>=33 && s4>=33 && s5>=33)
    {
    System.out.println("final result pass");
    }
    else
    {
    System.out.println("final result fail");
    }
    int t;
    t=s1+s2+s3+s4+s5;
    System.out.println("total=" +t +"/500");

    float per= t/5;
    System.out.println("percent=" +per);
    }
    }

    ReplyDelete
  16. class Marks
    {
    public static void main(String[]args)
    {
    int s1=27, s2=88, s3=65, s4=55, s5=29, g;

    if (s1>=28 && s1<33)
    {
    g=33-s1;
    s1=g+s1;
    System.out.println("pass with grace");
    }
    if (s1>33)
    {
    System.out.println( "pass" +s1);
    }
    else
    {
    System.out.println("fail" +s1);
    }

    if (s2>=28 && s2<33)
    {
    g=33-s2;
    s2=g+s2;
    System.out.println("pass with grace");
    }
    if (s2>33)
    {
    System.out.println( "pass" +s2);
    }

    else
    {
    System.out.println("fail" +s2);
    }
    if (s3>=28 && s3<33)
    {
    g=33-s3;
    s3=g+s3;
    System.out.println("pass with grace");
    }
    if (s3>33)
    {
    System.out.println( "pass" +s3);
    }

    else
    {
    System.out.println("fail" +s3);
    }
    if (s4>=28 && s4<33)
    {
    g=33-s4;
    s4=g+s4;
    System.out.println("pass with grace");
    }
    if (s4>33)
    {
    System.out.println("pass" +s4);
    }

    else
    {
    System.out.println("fail" +s4);
    }
    if (s5>=28 && s5<33)
    {
    g=33-s5;
    s5=g+s5;
    System.out.println("pass with grace");
    }
    if (s5>33)
    {
    System.out.println("pass" +s5);

    }
    else
    {
    System.out.println("fail" +s5);
    }
    if (s1>=33 && s2>=33 && s3>=33 && s4>=33 && s5>=33)
    {
    System.out.println("final result pass");
    }
    else
    {
    System.out.println("final result fail");
    }
    int t;
    t=s1+s2+s3+s4+s5;
    System.out.println("total=" +t +"/500");

    float per= t/5;
    System.out.println("percent=" +per);
    }
    }


    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