CFORBEGINNERS.COM

C Booleans Operators

Some other useful operators are called boolean operators which are and, or, and not. When I first learned these I didn't see how I would ever put these to use but now it seems I use these in almost every program I write. What the and, or, and not operators do is give your program much more versatility. First lets take a look at the and operator. Instead of using x and y I'll put actual numbers in them so you can see them more clearly.

if(2 > 1) && (5 > 3)

The and statement requires that both conditions be true in order for the if statement be true. So what this is saying is if 2 is greater than 1 AND if 5 is greater then 3 than the if statement is true and execute whatever command, both statements are true so the entire if statement is true, now lets look at one that isn't true.}

if (2 < 1) && (5 > 3)

Now what this statement is saying is if 2 is less than 1 and if 5 is greater than 3 than the if statement is true but it is not true. 2 will never be less than 1. Both statements have to be true in order for the whole statement to be true. Let us take a look at the or operator.

<
if (5 < 3) || (7 > 6)

This or statement is true and will execute. This is because unlike an and statement an or statement requires only one to be true in order for the entire if statement to be true. 5 is not less than 3 but 7 is certainly greater than 6 so the or statement will execute. Last there is the not boolean operator.

if !(2 > 1 && 5 > 3)

Looking back we can remember that this is an and statement and that it returned true. However this statement will return false, this is because of the exclamation mark (which is the symbol for not) what this operator does is return the opposite. If the statement were false it would return true but in this case the statement is true but it ultimately returns false. So lets check out a simple program that uses the boolean operators.

#include <stdio.h>
#include <stdlib.h>

int main()
{
   int x;
   printf("Please enter your age");
   scanf("%i",&x);
   
if (x > 19) && (x < 30)
    printf("You are in your twenties");

else
    printf("You are not in your twenties");

system("pause");
return 0;
}

This program is very straight forward, it contains an and boolean operator. The program asks for your age and tells you if are in twenties or not, not the most useful program but it gives you a clear example on how to use these things. The and operator checks to see if x is 20 through 29 if so then it prints out you are in your twenties and if not it will print out you are not. Now remember that program that converted from mph to kph, referring back to what I mentioned earlier about having the option to choose from within the program to convert from mph to kph or from kph to mph. This is made possible with just a simple if statement, here is the code.

#include <stdio.h>
#include <stdlib.h>

int main()
{
int z;
double x,y;
  printf("Enter 1 for MPH to KPH or 2 for KPH to MPH\n");
  scanf("%i",&z);
  if (z == 1)
  {
  printf("Please enter a speed in miles per hour\n");
  scanf("%lf",&x);
  y=1.609344 * x;
  printf("The equivalent speed in kilometers per hour is %.2f\n",y);
  }

  if (z == 2)
  {
   printf("Please enter a speed in kilometers per hour\n");
   scanf("%lf",&x);
   y = x/1.609344;
   printf("The equivalent speed in miles per hour is %.2f\n",y);
  }
  else
      {
         printf("Invalid input\n");
       }
system ("pause");
return 0;
}

Now lets break down this code in comparison to the other code that just took in mph and converted it to kph. Immediately you will notice that another variable was declared, integer z. z will be used just to determine what calculation to do. So if a user were to enter 1 for z the program would pass control to everything in the curly braces of the if statement that check to see if z equals 1. If a user wants to convert for kph to mph than they would enter 2 passing control to everything in the curly braces of the if statement that checks to see if z equals 2. Notice the else statement, this is sort of the default statement in this program, remember that else is everything else z can possibly be, so if z is not 1 or 2 than the user must have entered something else, the program will print invalid input and end. Try typing this code and running it, I encourage anyone to play around with it and see what they come up with it.