In our last two articles we talked about statements and working with text in C. In this article we are going to learn to use a logical operator alongside our statements, and work with the various loops (hopefully). We'll start with a short review of what we already know about logical operators, and then we'll dive right in.
Contributed by James Payne Rating: / 31 March 10, 2008
Logical AND is used to test whether two conditions are true. For example, if I wanted to make a joke about your mother, I could check to see if she is both fat and ugly. If so, I could make my program say something extra insulting. If not, it would say something only slightly insulting. Let's look at a program that asks you to try to guess what two numbers the computer is thinking of:
#include <stdio.h>
int main()
{
char c,d;
char numone='1';
char numtwo='3';
printf("I am thinking of a number between 1-10.n");
printf("Please enter a number: ");
c=getchar();
fflush(stdin);
printf("nnI am thinking of another number between 1-10.n");
printf("Please enter a second number: ");
d=getchar();
if(c==numone && d==numtwo)
{
printf("nnYou guessed both numbers in the proper sequence!n");
printf("nYou're a genius! An ugly genius, but a genius nonetheless!");
}
else
{
printf("nnYou have failed to guess the numbers in the proper
sequencenn");
printf("Dumb and ugly...what a shame!n");
}
return(0);
}
The program begins by creating four variables: "c" and "d", which will hold the users two guesses, and numone and numtwo. both of which hold the numbers the computer is thinking of. Next the computer asks the user to enter a number between 1-10 and stores the value in "c". Then it asks for a second number, and stores that value in "b".
We then enter an If statement, that says if the value in "c" is equal to numone AND the value in "d" is equal to numtwo then print some text. Note that, in this example, the numbers much match up exactly. For instance, if the user enters in '1' and then '3', they have guessed the right numbers. However, if they enter in '3' and then '1', they have not. If the user enters the wrong number and sequence, then some other text is printed. Here are the results:
If the user enters the correct data:
I am thinking of a number between 1-10.
Please enter a number: 1
I am thinking of another number between 1-10.
Please enter a second number: 3
You guessed both numbers in the proper sequence!
You're a genius! An ugly genius, but a genius nonetheless!
If the user enters the wrong data or enters it out of sequence:
I am thinking of a number between 1-10.
Please enter a number: 3
I am thinking of another number between 1-10.
Please enter a second number: 1
You have failed to guess the numbers in the proper sequence
Let's say we wanted to let the user know that they entered the right numbers, but did not enter them in the proper sequence. We could do so by using the NOT(!) in our code, like so:
#include <stdio.h>
int main()
{
char c,d;
char numone='1';
char numtwo='3';
printf("I am thinking of a number between 1-10.n");
printf("Please enter a number: ");
c=getchar();
fflush(stdin);
printf("nnI am thinking of another number between 1-10.n");
printf("Please enter a second number: ");
d=getchar();
if(c==numone && d==numtwo)
{
printf("nnYou guessed both numbers in the proper sequence!n");
printf("nYou're a genius! An ugly genius, but a genius nonetheless!");
}
else if(c==numtwo && d==numone)
{
printf("nnYou guessed the right numbers but in the wrong sequence!");
printf("nWhat a boob.");
}
else if(c==numone && ! (d==numtwo))
{
printf("nnYou guessed the first number right.nn");
}
else if(! (c==numone) && d==numtwo)
{
printf("nnYou guessed the second number right.nn");
}
else
{
printf("nnYou have failed to guess the numbers in the proper sequencenn");
printf("Dumb and ugly...what a shame!n");
}
return(0);
}
This program starts in the same manner as our previous program, creating the same four variables, which serve the same function. In fact, most of this program functions in the same way. Where it differs is in the If statement.
Our first part of the If statement says if "c" is equal to numone AND "d" is equal to numtwo, print out:
You guessed both numbers in the proper sequence!
You're a genius! An ugly genius, but a genius nonetheless!
If that criteria is not met, it moves on to an Else If, which states: if "c" is equal to numtwo AND "d" is equal to numone, (i.e.; if the numbers are right but out of sequence) print this out:
You guessed the right numbers but in the wrong sequence!
What a boob.
Again, if that criteria is not met it moves on to the next Else If, which says: If "c" is equal to numone, AND "d" is NOT equal to numtwo, print this:
You guessed the first number right.
If the criteria is not met, it moves onto the next Else If, which states: If "c" is NOT equal to numone, AND "d" is equal to numbtwo, print this text:
You guessed the second number right.
And lastly, if none of the criteria is met, it prints out:
You have failed to guess the numbers in the proper sequence
Lastly we come to our dear buddy the Logical OR(||). This bad boy checks to see if any one of two expressions is true. Here we will use the OR to shorten our code from the previous example:
#include <stdio.h>
int main()
{
char c,d;
char numone='1';
char numtwo='3';
printf("I am thinking of a number between 1-10.n");
printf("Please enter a number: ");
c=getchar();
fflush(stdin);
printf("nnI am thinking of another number between 1-10.n");
printf("Please enter a second number: ");
d=getchar();
if(c==numone && d==numtwo)
{
printf("nnYou guessed both numbers in the proper sequence!n");
printf("nYou're a genius! An ugly genius, but a genius nonetheless!");
}
else if(c==numtwo && d==numone)
{
printf("nnYou guessed the right numbers but in the wrong sequence!");
printf("nnYou have failed to guess the numbers in the proper sequencenn");
printf("Dumb and ugly...what a shame!n");
}
return(0);
}
In this example you will notice that our OR(||) did not save us all that much space. In bigger programs it can save you quite a bit of space and time. This program works in the same manner as the prior one, with a slight change in some of the text we print out. It uses the || to say if "c" is equal to numone AND "d" is not equal to numtwo OR if "c" is not equal to numone AND "d" is equal to numtwo, then print out:
You guessed one number right.
You will note that we are seeking the right number in the right sequence. So if the user typed in say 6 and then 1, they would wind up with the following:
You have failed to guess the numbers in the proper sequence
Dumb and ugly...what a shame!
This is because 1 matches the first number and not the second and they typed it out of sequence. If we weren't concerned with the order in which they typed the numbers, we could simply add more Ors and be fine.
Well that's it for this article. In our next action-packed episode we will finally and at long last discuss loops in C. So stick around. You might learn something.