Skip to main content

Conditional Statement in Java or IF--else Statement


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.











Comments

Popular posts from this blog

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

Conditional Statement in Python

It is used to solve condition-based problems using if and else block-level statement. it provides a separate block for  if statement, else statement, and elif statement . elif statement is similar to elseif statement of C, C++ and Java languages. Type of Conditional Statement:- 1) Simple if:- We can write a single if statement also in python, it will execute when the condition is true. for example, One real-world problem is here?? we want to display the salary of employees when the salary will be above 10000 otherwise not displayed. Syntax:- if(condition):    statements The solution to the above problem sal = int(input("Enter salary")) if sal>10000:     print("Salary is "+str(sal)) Q)  WAP to increase the salary of employees from 500 if entered salary will be less than 10000 otherwise the same salaries will be displayed. Solution:- x = int(input("enter salary")) if x<10000:     x=x+500 print(x)   Q) WAP to display th...