Article 6J03E [SOLVED] I don't understand behavior of logic operator in C if statement - why do I need logical AND?

[SOLVED] I don't understand behavior of logic operator in C if statement - why do I need logical AND?

by
amikoyan
from LinuxQuestions.org on (#6J03E)
This is a snippet from a very basic 'Rock, paper, scissor' game, written in C.
I am trying to write some basic error handling, to catch incorrect input from the user. The code works as intended.

Code:printf("\n\n\n\n\tEnter r for ROCK, p for PAPER and s for SCISSOR\n\t");

// scanf input from user

scanf("%c", &you);

// error handling

if (you != 's' && you != 'r' && you != 'p' ) { // this works
printf("Error - usage: Enter r for ROCK, p for PAPER and s for SCISSOR\n");
The problem is I don't understand why I need logical AND instead of logical OR.
In normal language we would use an OR, as in:

Code:if you does not equal 's' OR you does not equal 'r' OR you does not equal 'p'

then print error statementSo why do I need the && not || ?
External Content
Source RSS or Atom Feed
Feed Location https://feeds.feedburner.com/linuxquestions/latest
Feed Title LinuxQuestions.org
Feed Link https://www.linuxquestions.org/questions/
Reply 0 comments