DEV Community

Adam Roynon
Adam Roynon

Posted on

Logical Operators

Logical operators are used to either combine conditional and relational operators or change reverse their conditional output. You can combine conditional operators so that two or more conditions must be accepted, or at least one of multiple conditions are met before proceeding. The final logical operator is used to reverse a condition so that that reverse is accepted as the 'true' boolean response.

The AND Operator

The and operator is written using two ampersands '&' symbols and is used to make sure two or more conditions are met before executing a piece of code. This can be used to make sure that two or more conditions are true. The below code snippets show how this works.

The below code will print the letter "A" as the number 3 is more than the number 2 and the letter 'a' is equal to the letter 'a'.

if(3 > 2 && "a" == "a"){
    print("A");
}else{
    print("B");
}
Enter fullscreen mode Exit fullscreen mode

The below code will print the letter 'B' as even though the letter 'a' is equal to the letter 'a', the number 3 is not more than the number 4, and when using the and operator both conditions must be met.

if(3 > 4 && "a" == "a"){
    print("A");
}else{
    print("B");
}
Enter fullscreen mode Exit fullscreen mode

The OR Operator

The or operator is written using two pipe '|' symbols and is used to make sure at least one condition in multiple conditions is met before executing some code. This can be used to make sure that at least condition in a list of conditions are true. The below code snippets show how this works.

The below code will print the letter "A", this is because even though the number 3 is not more than the number 4 the letter 'a' is equal to the letter 'a'. When using the or operator only one of the conditions must be met.

if(3 > 4 || "a" == "a"){
    print("A");
}else{
    print("B");
}
Enter fullscreen mode Exit fullscreen mode

The below code will print the letter 'B', this is because the number 3 is less than the number 4 and the letter 'a' is not equal to the letter 'b'.

if(3 > 4 || "a" == "b"){
    print("A");
}else{
    print("B");
}
Enter fullscreen mode Exit fullscreen mode

The NOT Operator

The not operator is written using one exclamation mark '!' symbol, and is used to reverse a single condition. This means that when a condition usually returns true, using the not operator will make it return false instead. The below code snippets show how this works.

The below code will print the letter 'B' as the not operator is used to reverse the true boolean value within the if condition.

if(!true){
    print("A");
}else{
    print("B");
}
Enter fullscreen mode Exit fullscreen mode

The below code will print the letter 'A' as even though the number 3 is not more than the number 4 the not operator reversing this condition.

if(! 3 > 4){
    print("A");
}else{
    print("B");
}
Enter fullscreen mode Exit fullscreen mode

Brackets and Complex Operations

Using these operators you can create really complicated if and else if statements, as you can have as many conditions with as many logical and relational operators as you want or need. You can also use brackets to separate complex conditional statements, to ensure the ordering is processed correctly. The below code snippets show how this works.

The below code will print out the letter 'A'. This is due to the order of precedence of the logical operators. Even though the first condition evaluates to false (1 is not equal to 0) due to the or operator at the end the whole condition evaluates to true. Every condition is checked from left to right, so the first two statements are within the and operator and the result of that is combined using the or operator and the last condition.

if( 1 == 0 && 1 == 1 || 0 == 0 ){
    print("A")
}else{
    print("B")
}
Enter fullscreen mode Exit fullscreen mode

The below code will print out the letter 'B'. This is because we have used normal brackets to separate the conditions. Everything inside brackets will be worked out before taking into account the rest of the conditions. This means that even though the two operations inside the brackets are true, due to the combination of the and operator and the brackets, to separate the or conditions, our whole statements evaluates to false.

if( 1 == 0 && (1 == 1 || 0 == 0) ){
    print("A")
}else{
    print("B")
}
Enter fullscreen mode Exit fullscreen mode

This article was originally posted on my website: https://acroynon.com/

Top comments (0)