Skip to main content

Conditional Statements

Published 4 min readMarkdown
Conditional Statements

Conditional statements are essential programming constructs that allow a program to make decisions based on a particular condition. These statements evaluate a condition or expression, and depending on the result, they execute certain code blocks. In this blog post, we will discuss conditional statements and their usage in programming.

In programming, there are generally three types of conditional statements: if statements, if-else statements, and switch statements.

  1. If Statement : An if statement evaluates a condition and performs a set of instructions if that condition is true. The syntax for the if statement in Java programming language is as follows:
if (condition) {
  // execute this code if the condition is true
}

For example, let's say we want to check if a number is greater than 10. We can use an if statement to do this as follows:

int  number = 15;
if (number > 10) {
 System.out.print("number is greater");
}

Output: "The number is greater than 10"

  1. If-Else Statement : An if-else statement evaluates a condition and performs a set of instructions if the condition is true. If the condition is false, it performs a different set of instructions. The syntax for the if-else statement in Jva programming language is as follows:
if (condition) {
  // execute this code if the condition is true
} else {
  // execute this code if the condition is false
}

For example, let's say we want to check if a person is allowed to vote or not. We can use an if-else statement to do this as follows:

int age = 21;
if (age >=18) {
  System.out.println("Person is allowed to vote");
} 
else {
  System.out.println("Person is not allowed to vote");
}

Output: "Person is allowed to vote"

  1. Switch Statements A switch statement is used when you have multiple conditions to evaluate. The switch statement evaluates an expression and executes different blocks of code depending on the value of the expression. The syntax for the switch statement in Java programming language is as follows:
switch (expression) {
  case value1:
    // execute this code if expression is equal to value1
    break;
  case value2:
    // execute this code if expression is equal to value2
    break;
  ...
  default:
    // execute this code if none of the above conditions are true
    break;
}

For example, let's say we have a variable named day that contains the name of the day (e.g., "Monday", "Tuesday", etc.). We can use a switch statement to do different things depending on the day of the week as follows:

String day = "Monday";
switch (day) {
  case "Monday":
    System.out.println("Today is Monday");
    break;
  case "Tuesday":
   System.out.println("Today is Tuesday");
    break;
  ...
  default:
    System.out.println("Unknown day");
    break;
}

Output: "Today is Monday"

  1. if-else-if : if-else-if statement (also known as else-if ladder) is used when we need to check multiple conditions in sequence. It allows us to execute a block of code only when the previous if or else-if condition was not satisfied. Here is the basic syntax of an if-else-if statement in Java:

     if (condition1) {
        // Code to be executed if condition1 is true
     }
     else if (condition2) {
        // Code to be executed if the condition1 is false and condition2 is true
     }
     else if (condition3) {
        // Code to be executed if the condition1 and condition2 both are false and condition3 is true
     }
     else {
        // Code to be executed if all the above conditions are false
     }
    

    Here is an example of an if-else-if statement that checks the temperature and displays a message accordingly:

     public static void main(String[] args) {
        int temperature = 80;
        if (temperature > 100) {
           System.out.println("It's very hot!");
        } 
        else if (temperature > 80) {
           System.out.println("It's hot.");
        } 
        else if (temperature > 60) {
           System.out.println("It's warm.");
        } 
        else {
           System.out.println("It's cold.");
        }
     }
    

    In the above example, if the temperature is greater than 100, then the message "It's very hot!" is displayed. If the temperature is greater than 80 but less than or equal to 100, then the message "It's hot" is displayed. If the temperature is greater than 60 but less than or equal to 80, then the message "It's warm" is displayed. And if the temperature is less than or equal to 60, then the message "It's cold." is displayed.

In summary, conditional statements are a critical part of programming and help to make decisions based on specific conditions. By using if statements, if-else statements, and switch statements, you can create dynamic programs that respond to different situations. Stay tuned for more blogs and posts in Java programming language.

About the author

  • I am currently an undergraduate in Computer Science and Engineering . I am currently working on my skills in java language and I also having knowledge of C language. Looking forward to improve my skills in android development and making some interesting projects .

No comments yet. Be the first to comment.

More from this blog