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

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

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...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...