Conditionals

In programming languages, conditionals (also known as conditional statements, conditional expressions, and conditional constructs), are how we handle decisions in our programs. Specifically, conditionals perform different operations based on whether a programmer defined condition evaluates to true or false.

If/Else

The most basic form of conditionals is the if statement. Almost every programming language has some form of if statement. We use if statements to execute a block of code "if" a definded condition is true for our program. Most programming languages use the almost same syntax for if statements, let's look at some examples:

  • Bash:

    if [[ $NUM -gt 10 ]]
    then
       echo "$NUM is greater than 10."
    fi
    
  • C:

    if (num > 10) {
      printf("%d is greater than 10.", num);
    }
    
  • Java:

    if (num > 10) {
      system.out.println(num + 'is greater than 10.');
    }
    
  • Python:

    if num > 10:
      print(num + "is greater than 10.");
    

    The other half of an if/else statement is the else statement. We use else to define what we want our program to execute if we didn't fulfill the condition of our if statement. Most programming languages also support an elseif statement. An elseif statement is essentially followup if statements we want to check for before falling back on our else statement. Let's add some elseif and else statements to our examples above:

  • Bash:

    if [[ $NUM -gt 10 ]]
    then
      echo "$NUM is greater than 10."
    elif [[ $NUM -lt 10 ]]
    then
      echo "$NUM is less than 10."
    else
      echo "$NUM is 10."
    fi
    
  • C:

    if (num > 10) {
      printf("%d is greater than 10.", num);
    }
    else if (num < 10) {
      printf("%d is less than 10.", num);
    }
    else {
      printf("%d is 10.", num);
    }
    
  • Java:

    if (num > 10) {
      system.out.println(num + 'is greater than 10.');
    } else if (num < 10) {
      System.out.println(num + 'is less than 10.');
    } else {
      System.out.println(num + 'is 10.');
    }
    
  • Python:

    if num > 10:
      print(num + "is greater than 10.");
    elif num < 10:
      print(num + "is less than 10.");
    else:
      print(num + "is 10.");
    
This page was last updated: 2023-04-12 Wed 20:29. Source