Booleans and Comparison Operators
Symbol Description
> Greater than
< Less than
== Equal to
>= Greater than or equal to
<= Less than or equal to
!= Not equal to
As we now all know, our computer can only understand 1 language, that’s it uses a series of
logical operations to make decisions.
For our computer to make a decision it converts the statement(s) that are passed in, to a logic
circuit that will give an output as a single value, either a truthy or falsey value.
The output of any logical operation/ boolean opearation results in:
True = 1
False = 0
N.B in C, we use lower case only for true or false.
Lets check the outputs of true or false in a practical example:
Note:
To use Boolean operators, we include the bool.h as our header file.
Now we can safely say, bool is a datatype, just like any other, but then the problem is that it
does not have a specifier.
However, we safely use %d since 0 and 1 can be safely regarded as intergers.
Lets see the results for a true or false :
Lets explore more on how we can use these boolean operators to make our computer make decisions.
We often use the mathematical symbols as in the table above to make our computer make
decisions.
The computer will then evaluate the statements and then try to simulate a logic circuit so that
1 value is produced, either truthy value or falsey value.
Below is an example of how we can use the Boolean operators.
For statement 1.
The only available condition is (25 <= 13) false, thus 0.
For sure 25 is not less than or equal to 13, hence false value returned.
That’s a NOT 1 using Logic gates in logic circuits
For statement 2
This is actually a combination of two condition that need to be evaluated.
The computer evaluates the conditions separately, then checks for the connector.
In computer logic operations, specific symbols represent specific operations to be carried out.
Examples in C
| | --------OR
-(In this case a truth value is returned if either of the 2 condition is true or both are true)
Thus (first condition = 1) and (second condition = 0) ---Result is 1
Or (first condition = 0) and (second condition = 1) ---Result is 1
Or (first condition = 1) and (second condition = 1) ---Result is 1
It actually works as ADD in mathematics
Thus {1 + 0 = 1}, {0 + 1 = 1}, {1 + 1 = 1}
&& ------AND
-(In this case a truth value is returned if both conditions are true.
Thus (first condition = 1) and (second condition = 1) ---Result is 1
It works as a multiplier, thus {1* 1} = 1 and nothing else gives a 1
= = -----Strictly Equal
This condition produces 1 only and only if BOTH conditions are similar
To actually apply all these, certain decision-making statements are put into play, likely
• If statements
• Switch statement, and the likes we will be discussing for a couple of days
Quiz: Imagine you want to calculate the roots of quadratic equation using the quadratic
formular, and the values are to be entered by the user. How would you create an error free
program, that reports if values entered will produce unreal roots, or a single root and the
likes.
----It’s actually Funny, ---YES ----it’s like magic ---------
-- –It’s really funny to make a program that actually gives a decision -- --
----------Let’s look at the so called “SELECTION” in the next discussion--------