我们讨论过 C语言中的运算符介绍 在这里,我们全面了解了什么类型的操作符,C和C++支持及其基本实现。接下来,我们研究了 算术运算符 在这里我们详细了解了C和C++中算术运算符的类型和用法。在本文中,让我们尝试了解 关系运算符和逻辑运算符 .
null
关系运算符 关系运算符用于比较两个值,以了解一对数字共享的关系类型。例如,小于、大于、等于等。让我们逐一查看它们
- 等于运算符: 代表为 ‘==’ ,等于运算符检查两个给定的操作数是否相等。如果是,则返回true。否则返回false。例如 5==5 将返回真值。
- 不等于运算符: 代表为 ‘!=’ ,not equal to运算符检查给定的两个操作数是否相等。如果不是,则返回true。否则返回false。它是 ‘==’ 操作人员例如 5!=5. 将返回false。
- 大于运算符: 代表为 ‘>’ ,大于运算符检查第一个操作数是否大于第二个操作数。如果是,则返回true。否则返回false。例如 6>5 将返回真值。
- 小于运算符:表示为 ‘ ,小于运算符检查第一个操作数是否小于第二个操作数。如果是,则返回true。否则返回false。例如 6<5 将返回false。
- 大于或等于运算符: 代表为 ‘>=’ ,大于或等于运算符检查第一个操作数是否大于或等于第二个操作数。如果是,则返回true,否则返回false。例如 5>=5 将返回真值。
- 小于或等于运算符:表示为“<=” ,小于或等于运算符检查第一个操作数是否小于或等于第二个操作数。如果是,则返回true,否则返回false。例如 5<=5 也将返回true。
例如:
C
// 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" ); else printf ( "a is less than or equal to b" ); // greater than equal to if (a >= b) printf ( "a is greater than or equal to b" ); else printf ( "a is lesser than b" ); // less than example if (a < b) printf ( "a is less than b" ); else printf ( "a is greater than or equal to b" ); // lesser than equal to if (a <= b) printf ( "a is lesser than or equal to b" ); else printf ( "a is greater than b" ); // equal to if (a == b) printf ( "a is equal to b" ); else printf ( "a and b are not equal" ); // not equal to if (a != b) printf ( "a is not equal to b" ); else printf ( "a is equal b" ); return 0; } |
C++
// 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" ; else cout << "a is less than or equal to b" ; // greater than equal to if (a >= b) cout << "a is greater than or equal to b" ; else cout << "a is lesser than b" ; // less than example if (a < b) cout << "a is less than b" ; else cout << "a is greater than or equal to b" ; // lesser than equal to if (a <= b) cout << "a is lesser than or equal to b" ; else cout << "a is greater than b" ; // equal to if (a == b) cout << "a is equal to b" ; else cout << "a and b are not equal" ; // not equal to if (a != b) cout << "a is not equal to b" ; else cout << "a is equal b" ; return 0; } |
输出:
a is greater than ba is greater than or equal to ba is greater than or equal to ba is greater than ba and b are not equala is not equal to b
逻辑运算符: 它们用于组合两个或多个条件/约束,或补充对所考虑原始条件的评估。具体描述如下:
- 逻辑与运算符: 这个 ‘&&’ 当所考虑的两个条件都满足时,运算符返回true。否则返回false。例如 a&b 当a和b都为真(即非零)时返回真。
- 逻辑OR运算符: 这个 ‘||’ 即使所考虑的条件中的一个(或两个)得到满足,运算符也返回true。否则返回false。例如 a | | b 如果a或b或两者中的一个为真(即非零),则返回真。当然,当a和b都为真时,它返回真。
- 逻辑NOT运算符: 这个 ‘!’ 当所考虑的条件不满足时,运算符返回true。否则返回false。例如 !A. 如果a为false,即a=0,则返回true。
例如:
C
// 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" ); else printf ( "AND condition not satisfied" ); // logical OR example if (a > b || c == d) printf ( "a is greater than b OR c is equal to d" ); else printf ( "Neither a is greater than b nor c is equal " " to d" ); // logical NOT example if (!a) printf ( "a is zero" ); else printf ( "a is not zero" ); return 0; } |
C++
// 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" ; else cout << "AND condition not satisfied" ; // logical OR example if (a > b || c == d) cout << "a is greater than b OR c is equal to d" ; else cout << "Neither a is greater than b nor c is equal " " to d" ; // logical NOT example if (!a) cout << "a is zero" ; else cout << "a is not zero" ; return 0; } |
输出:
AND condition not satisfieda is greater than b OR c is equal to da is not zero
逻辑运算符中的短路:
- 万一 逻辑与 ,如果第一个操作数为false,则不计算第二个操作数。例如,下面的程序1不将“Geeksquick”打印为逻辑运算的第一个操作数,并且其本身为false。
C
#include <stdbool.h> #include <stdio.h> int main() { int a = 10, b = 4; bool res = ((a == b) && printf ( "GeeksQuiz" )); return 0; } |
C++
#include <iostream> using namespace std; int main() { int a = 10, b = 4; bool res = ((a == b) && cout << "GeeksQuiz" ); return 0; } |
输出:
No Output
- 但下面的程序将“Geeksquick”打印为逻辑运算的第一个操作数,并且是真的。
C
#include <stdbool.h> #include <stdio.h> int main() { int a = 10, b = 4; bool res = ((a != b) && printf ( "GeeksQuiz" )); return 0; } |
C++
#include <iostream> using namespace std; int main() { int a = 10, b = 4; bool res = ((a != b) && cout << "GeeksQuiz" ); return 0; } |
输出:
GeeksQuiz
- 万一 逻辑或 ,如果第一个操作数为真,则不计算第二个操作数。例如,下面的程序1不打印“Geeksquick”,因为逻辑OR的第一个操作数本身为true。
C
#include <stdbool.h> #include <stdio.h> int main() { int a = 10, b = 4; bool res = ((a != b) || printf ( "GeeksQuiz" )); return 0; } |
C++
#include <stdbool.h> #include <stdio.h> int main() { int a = 10, b = 4; bool res = ((a != b) || cout << "GeeksQuiz" ); return 0; } |
输出:
No Output
- 但下面的程序将“Geeksquick”打印为逻辑OR的第一个操作数。
C
#include <stdbool.h> #include <stdio.h> int main() { int a = 10, b = 4; bool res = ((a == b) || printf ( "GeeksQuiz" )); return 0; } |
C++
#include <stdbool.h> #include <stdio.h> int main() { int a = 10, b = 4; bool res = ((a == b) || cout << "GeeksQuiz" ); return 0; } |
输出:
GeeksQuiz
C语言中的运算符测试 本文由Ayush Jaggi撰稿。如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写评论
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END