1. 主页
  2. 文档
  3. C语言教程
  4. C语言运算符
  5. C 中的关系和逻辑运算符

C 中的关系和逻辑运算符

我们已经讨论了 C中的运算符简介,我们对什么类型的运算符、C 和 C++ 支持及其基本实现有了一个总体了解。之后,我们研究了算术运算符,详细了解了 C 和 C++ 中算术运算符的类型和使用。在本文中,让我们尝试了解关系运算符和逻辑运算符的类型和用途。

关系运算符
关系运算符用于比较两个值以了解一对数字份额的关系类型。比如小于、大于、等于等,我们一一来看 

  1. 等于运算符:表示为‘==’,等于运算符检查两个给定操作数是否相等。如果是,则返回 true。否则返回false。例如,5==5将返回 true。
  2. 不等于运算符:表示为‘!=’,不等于运算符检查两个给定操作数是否相等。如果不是,则返回 true。否则返回false。它是‘==’运算符的精确布尔补码。例如,5!=5将返回 false。
  3. 大于运算符:表示为‘>’,大于运算符检查第一个操作数是否大于第二个操作数。如果是,则返回 true。否则返回false。例如,6>5将返回 true。
  4. 小于运算符:表示为‘<‘,小于运算符检查第一个操作数是否小于第二个操作数。如果是,则返回 true。否则返回false。例如,6<5将返回 false。
  5. 大于或等于运算符:表示为‘>=’,大于或等于运算符检查第一个操作数是否大于或等于第二个操作数。如果是,则返回 true,否则返回 false。例如,5>=5将返回 true。
  6. 小于或等于运算符:表示为 ‘<=’,小于或等于运算符检查第一个操作数是否小于或等于第二个操作数。如果是,则返回 true,否则返回 false。例如,5<=5也将返回 true。

例子:

// C program to demonstrate working of relational operators
#include <stdio.h>

int main()
{
int a = 10, b = 4;

// greater than example
if (a > b)
printf("a is greater than b\n");
else
printf("a is less than or equal to b\n");

// greater than equal to
if (a >= b)
printf("a is greater than or equal to b\n");
else
printf("a is lesser than b\n");

// less than example
if (a < b)
printf("a is less than b\n");
else
printf("a is greater than or equal to b\n");

// lesser than equal to
if (a <= b)
printf("a is lesser than or equal to b\n");
else
printf("a is greater than b\n");

// equal to
if (a == b)
printf("a is equal to b\n");
else
printf("a and b are not equal\n");

// not equal to
if (a != b)
printf("a is not equal to b\n");
else
printf("a is equal b\n");

return 0;
}
// C++ program to demonstrate working of logical operators
#include <iostream>
using namespace std;

int main()
{
int a = 10, b = 4;

// greater than example
if (a > b)
cout << "a is greater than b\n";
else
cout << "a is less than or equal to b\n";

// greater than equal to
if (a >= b)
cout << "a is greater than or equal to b\n";
else
cout << "a is lesser than b\n";

// less than example
if (a < b)
cout << "a is less than b\n";
else
cout << "a is greater than or equal to b\n";

// lesser than equal to
if (a <= b)
cout << "a is lesser than or equal to b\n";
else
cout << "a is greater than b\n";

// equal to
if (a == b)
cout << "a is equal to b\n";
else
cout << "a and b are not equal\n";

// not equal to
if (a != b)
cout << "a is not equal to b\n";
else
cout << "a is equal b\n";

return 0;
}

输出: 

a is greater than b
a is greater than or equal to b
a is greater than or equal to b
a is greater than b
a and b are not equal
a is not equal to b

逻辑运算符:
它们用于组合两个或多个条件/约束或补充对所考虑的原始条件的评估。它们描述如下: 

  1. 逻辑 AND 运算符:当考虑的两个条件都满足时,‘&&’运算符返回 true。否则返回false。例如,当 a 和 b 都为真(即非零)时,a && b返回真。
  2. 逻辑OR运算符:“||” 即使满足考虑中的一个(或两个)条件,运算符也会返回 true。否则返回false。例如,一个 || 如果 a 或 b 之一或两者都为真(即非零),则b返回真。当然,当 a 和 b 都为真时,它返回真。
  3. 逻辑非运算符:“!” 运算符返回 true 考虑的条件不满足。否则返回false。例如,如果a 为假,即a=0 ,则!a返回真。

例子:

// C program to demonstrate working of logical operators
#include <stdio.h>

int main()
{
int a = 10, b = 4, c = 10, d = 20;

// logical operators

// logical AND example
if (a > b && c == d)
printf("a is greater than b AND c is equal to d\n");
else
printf("AND condition not satisfied\n");

// logical OR example
if (a > b || c == d)
printf("a is greater than b OR c is equal to d\n");
else
printf("Neither a is greater than b nor c is equal "
" to d\n");

// logical NOT example
if (!a)
printf("a is zero\n");
else
printf("a is not zero");

return 0;
}

// C++ program to demonstrate working of
// logical operators
#include <iostream>
using namespace std;

int main()
{
int a = 10, b = 4, c = 10, d = 20;

// logical operators

// logical AND example
if (a > b && c == d)
cout << "a is greater than b AND c is equal to d\n";
else
cout << "AND condition not satisfied\n";

// logical OR example
if (a > b || c == d)
cout << "a is greater than b OR c is equal to d\n";
else
cout << "Neither a is greater than b nor c is equal "
" to d\n";

// logical NOT example
if (!a)
cout << "a is zero\n";
else
cout << "a is not zero";

return 0;
}

输出: 

AND condition not satisfied
a is greater than b OR c is equal to d
a is not zero

逻辑运算符中的短路:

  • 逻辑 AND 的情况下,如果第一个操作数为假,则不计算第二个操作数。例如,下面的程序 1 不会打印“GeeksQuiz”,因为逻辑与本身的第一个操作数是假的。 
#include <stdbool.h>
#include <stdio.h>
int main()
{
int a = 10, b = 4;
bool res = ((a == b) && printf("GeeksQuiz"));
return 0;
}
#include <iostream>
using namespace std;

int main()
{
int a = 10, b = 4;
bool res = ((a == b) && cout << "GeeksQuiz");
return 0;
}

输出: 

No Output

但是下面的程序将“GeeksQuiz”打印为逻辑与的第一个操作数为真。 

#include <stdbool.h>
#include <stdio.h>
int main()
{
int a = 10, b = 4;
bool res = ((a != b) && printf("52cxydh"));
return 0;
}
#include <iostream>
using namespace std;

int main()
{
int a = 10, b = 4;
bool res = ((a != b) && cout << "52cxydh");
return 0;
}

输出: 

52cxydh
  • 逻辑 OR 的情况下,如果第一个操作数为真,则不计算第二个操作数。例如,下面的程序 1 不打印“GeeksQuiz”,因为逻辑 OR 的第一个操作数本身为真。 
#include <stdbool.h>
#include <stdio.h>
int main()
{
int a = 10, b = 4;
bool res = ((a != b) || printf("52cxydh"));
return 0;
}
#include <stdbool.h>
#include <stdio.h>
int main()
{
int a = 10, b = 4;
bool res = ((a != b) || cout << "52cxydh");
return 0;
}

输出: 

No Output

但是下面的程序将“GeeksQuiz”打印为逻辑 OR 的第一个操作数为假。 

#include <stdbool.h>
#include <stdio.h>
int main()
{
int a = 10, b = 4;
bool res = ((a == b) || printf("52cxydh"));
return 0;
}
#include <stdbool.h>
#include <stdio.h>
int main()
{
int a = 10, b = 4;
bool res = ((a == b) || cout << "52cxydh");
return 0;
}

输出: 

52cxydh

 

这篇文章对您有用吗?