Boolean Data Type

The boolean data type can only hold values that are true or false. Here is a quick syntax:

Boolean variableName = value; 
  • Boolean is the data type declaration.
  • variableName is the name assigned to the variable.
  • = is to assign the value to the variable.
  • value can only be true or false and doesn't use any '.

Here are quick examples of how to declare boolean variables:

Boolean a = true;
Boolean b = false;

Comparison Operators

We use comparison operators to compare the values of two similar variables. Comparison operators are an integral part of programming because it helps in running certain blocks of code when specific conditions are met. Here are comparison operators:

  • == is different from = operator. A single = assigns the value on the right to the variable on the left but the == operator checks if the value on the left IS EQUAL to the value on the right.
  • != operator checks if the value on the right IS NOT EQUAL to the variable on the left. This returns true if the values are different, and false if the values are the same.
  • > operator checks if the value on the left is greater than variable on the left.
  • < operator checks if the value of the right is greater than the variable on the left.
  • <= operator checks if the value of the right is either greater than, or equal to the variable on the left.
  • >= operator checks if the value of the right is either smaller than, or equal to the variable on the left.
  • || is called the OR operator. It takes two values on either side and checks returns true if either value is true, or both values are true and returns false if both values are false.
  • && is called the AND operator. It takes two values on either side and returns true if both values are true, and false if either value is false, or both values are false.

Conditionals

A very widely used conditional is the if-else statement. It's very straight forward and simple to use. here is the syntax:

if(condition){
  //run if code
} else {
  //run else code
}
  • if declares the start of the condition.
  • (condition) holds a combination of comparison operators and values/variables. If the condition evaluates to true, the if code block is run and if it's false, the entire code block is ignored and moves to else. It's a good practice to always keep the condition within brackets.
  • //run if code the // symbol is used to comment in code. Anything towards the right side of the // in the line is not run by the computer.
  • else is run if the first condition evaluates to false.
  • { } brackets are used to denote code blocks.
  • Notice there is no ; to mark the end of the if-else condition. The values inside the code block, however, require ;.

Here's an example to a simple if-else statement:

Integer a = 4;
if (a==4){
  System.debug('A is 4!');
} else {
  System.debug('A is not 4!');
} 

Change the value of a to any other value and see the results! The if-else statement can be chained to form bigger if-else statements:

Integer a = 4;
if (a==4){
    System.debug('A is 4!');
} else if (a==5) {
    System.debug('A is 5!');
} else {
    System.debug('A is not 4 or 5!');
} 

An if-else statement can also be used without the else statement.

Integer a = 4;
if (a==4){
    System.debug('A is 4!');
  }
if (a==5) {
    System.debug('A is 5!');
} 

If-else statements can be put inside each other. This is called a nested if-else statement.

Integer a = 4;
if (a==4 || a == 5){
  if (a==4){
    System.debug('A is 4!');
  } else {
    System.debug('A is 5!');
  }
} else {
  System.debug('A is not 4 or 5');
}

The code uses an OR statement to check the value of a. Line 2 evaluates to true if a is either 4 or 5.

Chaining if-else statements can get really complicated, and to deal with that, we use switch statements. Here is the syntax:

switch on variableName{
    when value1{
      //run code block 1
    }
    when value2 {
      //run code block 2
    }
    when else {
      //run code block 3
    }
} 
  • switch on is used to declare we are making a switch statement
  • variableName This is the variable we are runing the switch statement on.
  • when value1 if the value of variableName is value1, the code block 1 is run
  • when else this code block will run if the value of variableName isn't equal to any values.

Here's an example

Integer a = 10;
switch on a{
  when 10{
    System.debug('A is 10');
  }
  when 20{
    a++;
    System.debug('A is ' + a);
  }
  when else {
    a *= 40;
    System.debug('A is ' + a);
  }
} 

Summary

  • Booleans can only take true or false as their values.
  • Comparison operators help in comparing values.
  • if-else statements can be nested and can only be run as an if statement.
  • Switch statements are a good way to refactor if-else statements when they need to be nested a lot.
Day 5: List, Set, Maps and For Loops