我们已经讨论了《 C语言中的运算符简介》,在此我们对什么类型的运算符,C和C++支持及其基本实现有了一个整体的了解。之后,我们研究了算术运算符,从而对C和C++中的算术运算运算符的类型和用法有了详细的了解。在本文中,让我们尝试了解关系运算符和逻辑运算符的类型和用法。
关系运算符用于比较两个值,以了解一对数字份额的关系类型。例如,小于,大于,等于等。让我们一一看一下
- 等于运算符:表示为‘==’ ,等于运算符检查两个给定的操作数是否相等。如果是这样,则返回true。否则,它返回false。例如, 5 == 5将返回true。
- 不等于运算符:用‘!=’表示,不等于运算符检查两个给定的操作数是否相等。如果不是,则返回true。否则,它返回false。它是‘==’运算符的精确布尔补码。例如, 5!= 5将返回false。
- 大于运算符:表示为“>” ,大于运算符检查第一个操作数是否大于第二个操作数。如果是这样,则返回true。否则,它返回false。例如, 6> 5将返回true。
- 小于运算符:表示为“ <” ,小于运算符检查第一个操作数是否小于第二个操作数。如果是这样,则返回true。否则,它返回false。例如, 6 <5将返回false。
- 大于或等于运算符:表示为‘> =’ ,大于或等于运算符检查第一个操作数是否大于或等于第二个操作数。如果是这样,则返回true,否则返回false。例如, 5> = 5将返回true。
- 小于或等于运算符:表示为‘<=’ ,小于或等于运算符检查第一个操作数是否小于或等于第二个操作数。如果是这样,则返回true,否则返回false。例如, 5 <= 5也将返回true。
例子:
C
// C program to demonstrate working of relational operators
#include
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++
// C++ program to demonstrate working of logical operators
#include
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;
}
C
// C program to demonstrate working of logical operators
#include
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 AND 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++
// C++ program to demonstrate working of
// logical operators
#include
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 AND 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;
}
C
#include
#include
int main()
{
int a = 10, b = 4;
bool res = ((a == b) && printf("GeeksQuiz"));
return 0;
}
C++
#include
using namespace std;
int main()
{
int a = 10, b = 4;
bool res = ((a == b) && cout << "GeeksQuiz");
return 0;
}
C
#include
#include
int main()
{
int a = 10, b = 4;
bool res = ((a != b) && printf("GeeksQuiz"));
return 0;
}
C++
#include
using namespace std;
int main()
{
int a = 10, b = 4;
bool res = ((a != b) && cout << "GeeksQuiz");
return 0;
}
C
#include
#include
int main()
{
int a = 10, b = 4;
bool res = ((a != b) || printf("GeeksQuiz"));
return 0;
}
C++
#include
#include
int main()
{
int a = 10, b = 4;
bool res = ((a != b) || cout << "GeeksQuiz");
return 0;
}
C
#include
#include
int main()
{
int a = 10, b = 4;
bool res = ((a == b) || printf("GeeksQuiz"));
return 0;
}
C++
#include
#include
int main()
{
int a = 10, b = 4;
bool res = ((a == b) || cout << "GeeksQuiz");
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
它们用于组合两个或多个条件/约束或补充对所考虑的原始条件的评估。如下所述:
- 逻辑AND运算符:当同时考虑两个条件时, “ &&”运算符返回true。否则,它返回false。例如,当a和b都为真(即非零)时, && b返回真。
- 逻辑OR运算符: “ ||”即使满足所考虑条件中的一个(或两个),运算符将返回true。否则,它返回false。例如, ||如果a或b之一或两者均为true(即非零),则b返回true。当然,当a和b都为true时,它将返回true。
- 逻辑非运算符: “!”运算符返回true,即不满足考虑条件。否则,它返回false。例如,如果a为假,即a = 0时, !a返回true。
例子:
C
// C program to demonstrate working of logical operators
#include
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 AND 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++
// C++ program to demonstrate working of
// logical operators
#include
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 AND 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的情况下,如果第一个操作数为false,则不评估第二个操作数。例如,下面的程序1不打印“ GeeksQuiz”,因为逻辑AND的第一个操作数本身为false。
C
#include
#include int main() { int a = 10, b = 4; bool res = ((a == b) && printf("GeeksQuiz")); return 0; } C++
#include
using namespace std; int main() { int a = 10, b = 4; bool res = ((a == b) && cout << "GeeksQuiz"); return 0; } 输出:No Output
但是下面的程序将“ GeeksQuiz”打印为逻辑AND的第一个操作数为true。
C
#include
#include int main() { int a = 10, b = 4; bool res = ((a != b) && printf("GeeksQuiz")); return 0; } C++
#include
using namespace std; int main() { int a = 10, b = 4; bool res = ((a != b) && cout << "GeeksQuiz"); return 0; } 输出:GeeksQuiz
- 在逻辑OR的情况下,如果第一个操作数为true,则不评估第二个操作数。例如,下面的程序1不打印“ GeeksQuiz”,因为逻辑OR的第一个操作数本身为true。
C
#include
#include int main() { int a = 10, b = 4; bool res = ((a != b) || printf("GeeksQuiz")); return 0; } C++
#include
#include int main() { int a = 10, b = 4; bool res = ((a != b) || cout << "GeeksQuiz"); return 0; } 输出:No Output
但是下面的程序将打印“ GeeksQuiz”,因为逻辑OR的第一个操作数为false。
C
#include
#include int main() { int a = 10, b = 4; bool res = ((a == b) || printf("GeeksQuiz")); return 0; } C++
#include
#include int main() { int a = 10, b = 4; bool res = ((a == b) || cout << "GeeksQuiz"); return 0; } 输出:GeeksQuiz
C语言中的操作员测验
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。