[英]C logical or in if
I know I'm not very good in C but I thought that I could get this right: 我知道我的C语言不是很好,但是我认为我可以做到这一点:
if(strlen(type) == 0 || strcmp(type,"in")!=0 || strcmp(type,"out")!=0)
type
comes as a char*
and I've tested this code with the first part of the condition.It works well.If I have the second part of the condition and my type
contains "in"
it's ok but if all three conditions are available,if i input "out"
,the if isn't skipped.Why's that? type
以char*
type
出现,并且我已经在条件的第一部分测试了此代码。它很好用。如果我具有条件的第二部分并且我的type
包含"in"
,则可以,但是如果所有三个条件都可用,如果我输入"out"
,则不会跳过if。为什么呢?
your code: 您的代码:
if(strlen(type) == 0 || strcmp(type,"in")!=0 || strcmp(type,"out")!=0){
" your code-1"
}
else{
" your code-2"
}
Is equivalent to: 等效于:
if(strlen(type) == 0 ){
" your code-1"
}
else{
if(strcmp(type,"in")!=0){
" your code-1"
}
else{
if(strcmp(type,"out")!=0){
" your code-1"
}
else{
" your code-2"
}
}
}
Point is if you have first if()
executes if string type
have something, then else never executes. 关键是,如果字符串
type
包含某些内容,则首先 if()
执行if()
,否则就永远不执行。 Because a empty string( in else part ) can't be equals to "in"
or "out"
. 因为空字符串( 其他部分 )不能等于
"in"
或"out"
。 So you always have choice to execute "code-1" if string is not empty and nothing to executes if string is empty ( that is length = 0 ). 因此,如果string 不为空 ,则始终选择执行“ code-1”,而如果string为空( 即length = 0 ) , 则不执行任何操作。
Edit: 编辑:
I think you wants something like if type
string is "in" then execute "code-1" if type is "out" then execute second code-2. 我认为您想要类似的
type
字符串为“ in”,然后执行“ code-1”,如果类型为“ out”,然后执行第二个code-2。 like: 喜欢:
if(strlen(type) == 0 ){
}
else{
if(strcmp(type,"in")!=0){
" your code-1"
}
else{
if(strcmp(type,"out")!=0){
" your code-2"
}
}
}
you can do like: 您可以这样做:
flag = 'o';// this will save string comparison again
if(strlen(type) == 0 || strcmp(type,"in")==0 ||
strcmp(type,"out")!=0 && !(flag='?')){
"code-1"
}
else{
if(flag=='o'){ //no strcmp needed
"code-2"
}
}
Here I posted a Code based on my logic and it run as: 在这里,我根据自己的逻辑发布了一个代码 ,其运行方式为:
:~$ ./a.out
Enter string: in
in
:~$ ./a.out
Enter string: out
out
:~$ ./a.out
Enter string: xx
:~$
The branch will be taken if type
is empty, or if type
contains either "in" or "out". 如果
type
为空,或者type
包含“ in”或“ out”,则将采用该分支。
Given the expression a || b
给定表达式
a || b
a || b
, the following are true: a || b
,是正确的:
a
is evaluated first; a
首先评估; a
evaluates to non-zero (true), then the entire expression evaluates to true, and b
is not evaluated ; a
取值为非零(true),则整个表达式的取值为true,而b
的取值不为 ; a
evaluates to zero (false), then b
is evaluated; a
取值为零(假),则b
的取值为; a
and b
evaluate to zero (false), then the entire expression evaluates to false; a
和b
都为零(false),则整个表达式的值为false; otherwise, the expression evaluates to true; So if type
contains the string "out", then 因此,如果
type
包含字符串“ out”,则
strlen(type) == 0
evaluates to false
, meaning we evaluate strlen(type) == 0
为false
,这意味着我们评估 strcmp(type, "in") != 0
, which evaluates to false
, meaning we evaluate strcmp(type, "in") != 0
,其结果为false
,表示我们评估 strcmp(type, "out") != 0
, which evaluates to true
, so strcmp(type, "out") != 0
,其结果为true
,所以 Based on what you say you're expecting, it sounds like you got the sense of the last test wrong, and that you really want 根据您的期望,听起来好像您对上次测试有错误的认识,并且您确实想要
if( strlen( type ) == 0 ||
strcmp( type, "in" ) != 0 ||
strcmp( type, "out" ) == 0 )
{ // ^^ note operator
...
}
This will enter the branch if type
is empty, if type
contains "in", or if type
doesn't contain "out". 如果
type
为空, type
包含“ in”或type
不包含“ out”,则将进入分支。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.