Conditional Statement in Java or IF--else Statement

0

Conditional Statement in Java or IF--else Statement:-

This statement provides a separate block of code for true condition and false condition using if statement, else if statement and else statement.


Type of Conditional Statement:-


1 Simple If:-

   if(condition)
   {
        statement;
   }

WAP to increase the salary of employees from 500, if entered salary will be less then 20000 otherwise the same salaries will be displayed?

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

note:-  if we not use  { } then if will consider only single statement.if we want to consider more then one statement then we use {}.

if(condition)
{



}

means it will contain multiple statements.




2 If--else:-

  It will work when the condition will be true and false. if block for true condition and else block for the false condition.


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

WAP to provide grace marks to students if the mark will be less then <33  otherwise the same marks will be displayed, grace marks will be up-to five. if pass with grace then displays grace + grace mark otherwise mark only.

mark=29
if(mark>=28 && mark<33)
{
    mark = mark + (33-mark)
   System.out.println("Pass by Grace and Grace mark is "+(33-mark));
}
else
System.out.println(mark);


3 Nested If--Else:-

If we have more then one condition in the same program then we prefer nested if-else statement.

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

      }

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

      }

}

WAP to increase the salary of an employee from 500, if entered salary will be less then 20000,1000 if entered salary will be > 20000 but <30000 if salary will be above 30000 then increase 2000.
not use any logical operator

if(salary<30000)
{
    if(salary<20000)
      System.out.println(salary+500);
    else
     System.out.println(salary+1000);
}
else
  System.out.println(salary+2000);


Another Example of Nested If--else:-

import java.util.Scanner;
class NestedGreatest
{
    public static void main(String args[])
    {
      int a,b,c;
      Scanner sc = new Scanner(System.in);
      System.out.println("enter first number");
      a=sc.nextInt();
      System.out.println("enter second number");
      b=sc.nextInt();
      System.out.println("enter third number");
      c=sc.nextInt();
      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");
  
         }
   
     
    }
    
}

}
................................................................................................................................

Assignment of If--else:-


All programs of ternary should be converted in if-else?


WAP to find the middle in three different where the number should not be equal?

a=1
b=7
c=2

output c

WAP  to declare income tax slab according to entered income?

WAP to check that temperature is normal, cold, or heat according to entered temperature should be in Celsius but the comparison will be in Fahrenheit?



4 Ladder If--else:-

It will work step by step means the first statement will be checked if the condition is false after that second statement will be checked, if all else if a condition will false and at last point else statement, will be executed.

Syntax of Ladder If--else:-


if(condition)
{
      statement;

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


else is the optional statement in a conditional statement in java.

WAP to check the greatest number using Ladder If--else.

Solution:-

public class Main
{
    public static void main(String[] args) {
         int a=1000,b=200,c=300;
         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");
         }
    }
}


It provides a simple syntax structure as compare to nested if-else hence we mostly prefer ladder if-else to create program.



5  Multiple If:-

It is the collection of more then one if 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;
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:-


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 subject Mark is >28 and <33 then 5 grace marks will be applied and students will pass by grace.

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











Post a Comment

0Comments

POST Answer of Questions and ASK to Doubt

Post a Comment (0)