Tutorial 1: Conditionals


Conditionals are what makes programs able to handle situations that are not constant. It basically tells a computer if something happened. The first thing you need to know is the new variable type:

Boolean (type bool in C++)

The result of a conditional is always a boolean. A boolean can either be true or false. Or it can be 1 for true and 0 for false.

So what does that mean? It means that C++ can tell if something is right or wrong, nothing in between, no grey area (unless you program it in). The boolean operations that you can usually do are the comparisons. These are the comparisons that you can do:

Operation
Description

==

Equality:this tells you if the left side of this sign is identical to the right side of it

!
InEquality: if the left side is not equal to the right side
>
Greater Than: if the left side is greater than the right side
<
Less Than: if the left side is less than the right side
>=
Greater Than or Equal To: this combines the greater than and the equality into one, it tries to see if both are true
<=
Less Than or Equal To: this combines the less than and the equality into one, it tries to see if both are true

So basically those will convert two (one for inequality) of your data objects and turn them into a boolean.

Another thing about conditionals is that you can also combine them in a meaningful way. You can use either of the following to do a bunch of complexed conditionals:

Operator
Description
&&
And: only true if the left and right are both true, false otherwise
||
Or: false only if both sides are false, true otherwise
!
Not: turns true to false, and vise versa

Here's their truth table

a && b
b = true
b = false
a = true
true
false
a = false
false
false

 

a || b
b = true
b = false
a = true
true
true
a = false
true
false

 

a
a!
true
false
false
true

 

These are the stuff that can be joined. The most common use of conditionals is an if statement:

if (condition)
   <stuff to do>

example:

if (a > b)
   c = a - b; //this insures that c is never negative

Now for some advanced topic: Demorgan's Theorem. Demorgan's is just the following equation (memorize it):

!(a && b) = !a || !b

!(a || b) = !a && !b