How to write "if x equals 5 or 4 or 78 or..." in C
I have a quick question about using logical operators in an if statement.
Currently I have an if statement that checks if x equals to 5 or 4 or 78:
if ((x == 5) || (x == 4) || (x == 78)) {
blah
}
And I was wondering if I could just condense all that to:
if (x == 5 || 4 || 78) {
blah
}
Sorry for 开发者_运维百科such a basic question, I've just started learning C.
There is no shortcut, but you need to fix your equality operator.
if ((x == 5) || (x == 4) || (x == 78)) {
First, you're using assignments not equality tests in your ifs. The first method (with suitable substitutions for equality) is the best way to do the test, though if you have a lot of possible options, there might be better ways. The second way might compile, but it won't do what you want, it will always return true since both 4 and 78 evaluate to true and what you are doing is evaluating whether 5 (the result of assigning 5 to x) or 4 or 78 are true. A switch statement might be one possible alternative.
switch (x) {
case 4:
case 5:
case 78:
blah...
break;
default:
}
There's no shortcut for the if statement, but I suggest considering:
switch (x)
{
case 4:
case 5:
case 78:
/* do stuff */
break;
default:
/* not any of the above... do something different */
}
No you cannot and the test for equality is ==
, not =
@uncle brad is spot on, but later you'll probably learn about something called a switch
statement. It looks funky but is often used in these sorts of situations (where several possible values of a variable all have the same effect):
switch (x) {
case 4:
case 5:
case 78:
// ...
break;
}
Though you'd only want to use a switch
statement when the meaning of an if
statement is less clear--most compilers these days are smart enough to generate optimal machine code either way.
It's been answered in the time it took me to log in, but you could use the switch, and break it out into a function
int isValid(int toCheck) {
switch(toCheck) {
case 4:
case 5:
case 78:
return 1;
default:
return 0;
}
}
Then you would just call the method every time you needed to check the int against the established cases.
Admittedly, this example is rather silly, but for a bigger selection of cases, and ones that were evaluated repeatedly, you could do something like this to simplify and reuse some code.
No, sorry, you can't; you have to write all the expressions out. For very long lists of numbers to compare to, you could put the numbers in an array, and loop over the list; but you'd have to have a dozen numbers or so before that started to look like a good idea.
No, you cannot do this in C. Your first code sample is also incorrect. There is an important distinction between assignment (=
) and equivalency (==
) in C. When you wrote x = 5
in your expression, this will actually compile and evaluate to either 0 or 1 (false or true) before being logically OR'ed with the next part of the expression!
Your second code sample is also valid C, but it does not do what you want it to do. You read the statement as "(x is assigned to 5) or true or true". This is because any non-zero value in C is logically true. Thus, x will contain the value 5, and evaluate to true, making your if condition true. The rest of the expression does not matter since the ||
operator short-circuits.
One alternate thought: While there's no "shortcut", if you have a lot of numbers it may be easier for code length and typing sanity to put them all into an array and check against the array in a loop. If you have to compare against many numbers more than once, sort them in an array and use binary searching.
For 3 numbers, though, you have to do it the "long" way.
You can do the for loop if it's a long list, how ever that doesn't really handle the logical operators :-...
#include <stdio.h>
int forbiddenList[13] = {5, 4, 78, 34, 23, 56, 4, 7, 6, 4, 33, 2333, 0};
int length = 13;
int main() {
int mysteryNum;
printf("type a number: ");
scanf("%d",&mysteryNum);
int i;
for (i = 0; i <= length; i ++)
{
int target = forbiddenList[i];
if (mysteryNum == target)
{
printf("You have chosen of the forbidden list!\n");
printf("YOU SHALL NOT PASS!!\n");
}
}
return 0;
}
er... haven't done c... ever... you should take C++...
int preconditions[] = { 4,5,78 }; // it should be in most likely order
int i = 0;
for(;i<3;++i) {
if( x == preconditions[i] ) {
// code here.
}
}
The syntax is:
if(condition1 || condition2 || condition3 || ...){
// Do something
} else {
// Do something
}
Answer to your question:
if( (x == 5) || (x == 4) || (x == 78) ){
//do something
} else {
//do something
}
精彩评论