bscodinglab

Decision Making or Conditional Statements

  1. Conditional Statements:-

Conditional statements in Java allow you to execute different blocks of code based on whether a given condition is true or false. The primary conditional statements in Java are Conditional statements allow you to execute code blocks conditionally, based on whether a given condition is true or false.  I Will provide Conditional Statements of each type

      1. If
      2. If-else
      3. Nested if-else
      4. Ladder if-else
      5. Swithc Case
  • IF Condition :- In Java, the if statement  are used for making decisions in your code The if statement is used to execute a block of code if a specified condition is true. If the condition is false, the code block inside the if statement is skipped.

Here’s an  examples of If Condition and expected outputs

 Syntax :-   if(Condition)

                  {

                        // Write Statements Hare.

}

 Example :-   int a = 10;

   if(a>=5)

                                     {

                                        System.out.println(“a is greater than 5”);// Hare condition is true then output is 5.

                 }

         Output :-   a is greater than 5 

 

  • If-else :- The if-else statement allows you to execute one block of code if the condition is true and another block of code if the condition is false. If the condition evaluates to true, the code block inside the if statement is executed. If the condition evaluates to false, the code block inside the else statement is executed.

Syntax :-

if (condition)

 {

  // code block executed if the condition is true

}

else

{

            // code block executed if the condition is false

}

Example :-

int age = 18;

if (age >= 18)

   {

       System.out.println(“You are an adult.”);

   }

 else

   {

        System.out.println(“You are not an adult.”);

    }

     Output :-   You are an adult.

  • Nested if-else :- A nested if-else statement in Java is a conditional statement that checks for multiple conditions. It is used when we need to execute a block of code only if more than one condition is met. Nested if-else statements can be nested to any level. This means that we can have if statements inside other if statements. Nested if-else statements can be useful for complex decision-making logic.

 

  Syntax :-  if (condition1)

{

            if (condition2)

   {

                     // code block executed if both condition1 and condition2 are true

                         }

            else

                {

                    // code block executed if condition1 is true but condition2 is false

                 }

   }

else

  {

             // code to be executed if condition1 is false

  }   

  Example :-

                                int  n1 = -4, n2 = 3, n3 = 5;

 if(n1 >= n2)

     {

                                        if(n1 >= n3)

                                               System.out.println(n1 + ” is the largest number.”);

   else

                  System.out.println(n3 + ” is the largest number.”

       }

                                   else

                                      {

                                                if(n2 >= n3)

                                                                   System.out.println(n2 + ” is the largest number.”);

                                                  else

                                                                    System.out.println(n3 + ” is the largest number.”);

                                        }

            Output :-   5 is the largest number. 

  • Ladder if-else :- Ladder if-else in Java is a control flow statement that allows you to execute different blocks of code based on multiple conditions. It is a series of if-else statements chained together, so that only one block of code is executed, depending on which condition is met. The conditions are evaluated from top to bottom. If the first condition is true, the code block associated with that condition is executed and the rest of the ladder is skipped. If the first condition is false, the next condition is evaluated. This process continues until one of the conditions is true or the bottom of the ladder is reached. If none of the conditions are true, the default code block is executed. Ladder if-else statements can be used to implement a variety of different algorithms and logic flows.

    Syntax :-

if (condition1)

                {

                       // code block 1

                }

else if (condition2)

                {

                      // code block 2

                }

 else {

                      // default code block

         }

                Example :-

int marks = 85;

if (marks >= 90)

                {

                                System.out.println(“Grade A”);

                }

 else if (marks >= 80)

                {

                                System.out.println(“Grade B”);

                }

 else if (marks >= 70)

                {

                                System.out.println(“Grade C”);

                }

  else {

                                System.out.println(“Grade D”);

            }                             

           Output :-   Grade B

  •  Swithc Case :- Switch case is a control statement in Java that allows you to execute one of multiple code blocks based on the value of a variable. It is similar to an if-else-if ladder statement, but it is more efficient for cases with multiple conditions. If the value of the expression matches one of the case values, the corresponding block of code is executed. If the value of the expression does not match any of the case values, the default block of code is executed.
    1. The expression is evaluated once.
    2. The value of the expression is compared to the values of each case label.
    3. If there is a match, the associated code block is executed.
    4. If there is no match, the code block for the default label is executed (if it exists).

    Syntax :-

switch (expression)

{

    case value1:

            //Statements Write hare block 1;

     break;

    case value2:

            //Statements Write hare block 2;

     break;

         

     default:

             //Statements Write hare in Defualt block ;

       }

Example :-

int day = 5;

switch (day)

 {

  case 1:

            System.out.println(“Monday”);

  break;

  case 2:

            System.out.println(“Tuesday”);

  break;

   case 3:

            System.out.println(“Wednesday”);

   break;

   case 4:

            System.out.println(“Thursday”);

   break;

   case 5:

             System.out.println(“Friday”);

   break;

   case 6:

               System.out.println(“Saturday”);

   break;

   case 7:

               System.out.println(“Sunday”);

   break;

   default:

                System.out.println(“Invalid day”);

  }

Output :-   Friday

If you remove the break statements from the code blocks in the switch case statement, the code will continue to execute until it reaches the end of the switch block. This is known as fall-through. It is important to use break statements explicitly to control the flow of execution in your switch case statements.