Conditionals

:

Articles

Some IFs plus some ANDs, ORs, and BUTs about it!


    Note: This is a multi-part article was written for programmers of varying experience. Part 2 was written for those with slightly more than little to no prior programming knowledge. If you came from Part 1, you should be right at home here. If not, what kind of monster reads Part 2 before Part 1? Who are you?
    

I was browsing PPCG and I saw ! and && and || and ? and : and...

In Part 1, we only scraped the surface of conditionals. In this segment, we'll tackle each of these operators one at a time (except for the last two, they go together). !, &&, and || can all be used in combination, and occasionally are. They're the base operators for tweaking and chaining our conditional statements (like the multiplication and division of mathematics).

Ok, sure. But what about "addition and subtraction"?

If !, &&, and || are the multiplication and division of conditionals, then =, >, and <, are the addition and subtraction. With them, we can form our most basic logical checks: a == b (a is equal to b) a > b (a is greater than b) a < b (a is less than b) a >= b (a is greater than or equal to b) a <= b (a is less than or equal to b) I don't think these deserve much time to be spent on... they're all pretty straightforward, and chances are you've come across all but == in some level of intermediate mathematics.

! (not)

! is a logical operator put directly before a boolean value meaning 'not'. Essentially, we can use it to invert our if statement, or some part of the conditional. Note: Some verbose languages (like Python and VB.NET) will use they keyword not instead.

# Python
if not False:
  print("it is not false")
      

//C++
if (!false) {
  printf("it is not false");
}
          
You can also use it to check if things aren't equal.

# Python
if not 1 == 2:
  print("1 does not equal 2")
      

//C++
if(!(1 == 0)) {
  printf("1 does not equal 2");
}
          
We can also combine ! and == to form != (is not equal to).

# Python
if 1 != 2:
  print("1 does not equal 2")
      

//C++
if(1 != 2) {
  printf("1 does not equal 2");
}
          

&& (and)

! is a logical operator put directly between two conditionals (or sets of conditionals) meaning and. With it, we can combine booleans to form larger and more complex conditional statements. Note: Some verbose languages (like Python and VB.NET) will use they keyword and instead.

# Python
if True and not False:
  print("it is true, and not false")
      

//C++
if (true && !false) {
  printf("it is true, and not false");
}
          

|| (or)

|| is a logical operator put directly between two conditionals (or sets of conditionals) meaning or. The same rules as above apply. Note: Some verbose languages (like Python and VB.NET) will use they keyword or instead.

# Python
if False or True:
  print("one of the above is true")
      

//C++
if (false || true) {
  printf("one of the above is true");
}
          
We can even chain them all together to check for complex conditions, if you're so inclined.

# Python
if (a or b) and not (c and d):
  print("1-3 of the above are true")
      

//C++
if ((a || b) && !(c && d) {
  printf("1-3 of the above are true");
}
          
Here, we're looking at a, b, c, and d in a complicated relationship, but we can break out down into parts. Let's start with a or b. This will be True if either a or b are True. Next is !(c and d). This will be True as long as c and d are not both True.

? : (a more compact if statement)

I'm actually a really big fan of ? :. It's just an if statement squeezed on to a single line, but I find it much more elegant. It's written as a ? b : c works as if a is true do b, otherwise do c. I find it most valuable when setting variables, but there are plenty of other clever uses for it as well. Try to figure out what this does:

# Python
print(b if !b else b - 1)
        

//C++
printf("%d", !b ? b : b - 1);
          
Were you able to figure it out? Here it is expanded out:

# Python
if b == 0:
    print(b)
else:
    print(b - 1)
        

//C++
if (b == 0) {
  printf("%d", b);
} else {
  printf("%d" b - 1);
}
    
What we've essentially done is turned 4 lines of code into 1. You don't need to know it, but you might see it in someone else's code from time to time, and it's a useful trick. Next time, we'll dive into conditionals within loops and other ways to use them (like switch statements)!

1 2