C の論理演算子は、複数の条件/制約を組み合わせるために使用されます。論理演算子は 0 または 1 を返します。それは式の結果が true か false かによって異なります。意思決定のための C プログラミングでは、論理演算子を使用します。
C 言語には 3 つの論理演算子があります。
- 論理 AND ( && ) 論理 OR ( || ) 論理 NOT ( ! )
論理演算子の種類
1. 論理積演算子 (&&)
両方のオペランドがゼロ以外の場合、条件は true になります。それ以外の場合、結果の値は 0 になります。結果の戻り値の型は int です。以下は、論理 AND 演算子の真理値表です。
バツ | そして | X && Y |
---|---|---|
1 | 1 | 1 |
1 | 0 | 0 |
0 | 1 | 0 |
0 | 0 | 0 |
構文
(operand_1 && operand_2)>
例
C
// C program for Logical> // AND Operator> #include> // Driver code> int> main()> {> > int> a = 10, b = 20;> > if> (a>0 && b> 0) {>> > printf> (> 'Both values are greater than 0
'> );> > }> > else> {> > printf> (> 'Both values are less than 0
'> );> > }> > return> 0;> }> |
>
>出力
Both values are greater than 0>
2. 論理和演算子 ( || )
それらのいずれかが 0 以外の場合、条件は true になります。それ以外の場合は、値として false、つまり 0 を返します。以下は、論理 OR 演算子の真理値表です。
バツ | そして | × ||そして |
---|---|---|
1 | 1 | 1 |
1 | 0 | 1 |
0 | 1 | 1 |
0 | 0 | 0 |
構文
(operand_1 || operand_2)>
例
C
// C program for Logical> // OR Operator> #include> // Driver code> int> main()> {> > int> a = -1, b = 20;> > if> (a>0 || b> 0) {>> > printf> (> 'Any one of the given value is '> > 'greater than 0
'> );> > }> > else> {> > printf> (> 'Both values are less than 0
'> );> > }> > return> 0;> }> |
>
>出力
Any one of the given value is greater than 0>
3. 論理否定演算子 ( ! )
条件が true の場合、論理 NOT 演算子により false になり、その逆も同様です。以下は、論理 NOT 演算子の真理表です。
バツ | !バツ |
---|---|
0 | 1 |
1 | 0 |
構文
! (operand_1 && operand_2)>
例
C
// C program for Logical> // NOT Operator> #include> // Driver code> int> main()> {> > int> a = 10, b = 20;> > if> (!(a>0 && b> 0)) {>> > // condition returned true but> > // logical NOT operator changed> > // it to false> > printf> (> 'Both values are greater than 0
'> );> > }> > else> {> > printf> (> 'Both values are less than 0
'> );> > }> > return> 0;> }> |
>
>
短絡論理演算子
他のオペランドを評価せずに、前の論理式を評価することによって結果を決定できる場合、それはショートサーキットと呼ばれます。
複数の論理演算子を含む方程式では短絡が見られます。 AND、OR、または両方を指定できます。
1. 論理積演算子の短絡
論理 AND 演算子は、すべてのオペランドが true と評価された場合にのみ true を返します。最初のオペランドが false の場合、それ以降のオペランドは評価されません。これは、追加のオペランドが true と評価された場合でも、条件全体が false を返すためです。
例
C++
// C++ Program to illustrate short circuiting in Logical AND> #include> using> namespace> std;> // utility function to check positive> bool> is_positive(> int> number)> {> > if> (number>0)>> true> ;> > else> > return> false> ;> }> // utility function to check if the number is even> bool> is_even(> int> number)> {> > if> (number % 2 == 0)> > return> true> ;> > else> > return> false> ;> }> // driver code> int> main()> {> > int> x = 10;> > // Both conditions are evaluated> > if> (is_positive(x) && is_even(x)) {> > cout <<> 'Both conditions are satisfied.'> << endl;> > }> > else> {> > cout <<> 'Conditions not satisfied.'> << endl;> > }> > int> y = -5;> > // The first condition is evaluated and found to be> > // false, so the second condition is not evaluated> > if> (is_positive(y) && is_even(y)) {> > cout <<> 'Both conditions are satisfied.'> << endl;> > }> > else> {> > cout <<> 'Conditions not satisfied.'> << endl;> > }> > return> 0;> }> |
>
>出力
Both conditions are satisfied. Conditions not satisfied.>
2. 論理和演算子の短絡
少なくとも 1 つのオペランドが true と評価される場合、OR 演算子は true を返します。最初のオペランドが true の場合、それ以降のオペランドは評価されません。これは、追加のオペランドが false と評価された場合でも、条件全体が true を返すためです。
例
C++
// C++ program to illustrate the short circuiting in Logical> // OR> #include> using> namespace> std;> // utility function to check positive number> bool> is_positive(> int> number)> {> > if> (number>0)>> true> ;> > else> > return> false> ;> }> // utility function to check if the number is even> bool> is_even(> int> number)> {> > if> (number % 2 == 0)> > return> true> ;> > else> > return> false> ;> }> // driver code> int> main()> {> > int> x = 8;> > // The first condition is evaluated and found to be> > // true, so the second condition is not evaluated> > if> (is_positive(x) || is_even(x)) {> > cout <<> 'At least one condition is satisfied.'> > << endl;> > }> > else> {> > cout <<> 'Conditions not satisfied.'> << endl;> > }> > int> y = -5;> > // The first condition is evaluated and found to be> > // false, so the second condition is evaluated> > if> (is_positive(y) || is_even(y)) {> > cout <<> 'At least one condition is satisfied.'> > << endl;> > }> > else> {> > cout <<> 'Conditions not satisfied.'> << endl;> > }> > return> 0;> }> |
>
>出力
At least one condition is satisfied. Conditions not satisfied.>
論理演算子に関するよくある質問
Q1.プログラミングにおける論理演算子の優先順位は何ですか?
答え:
論理演算子の優先順位は、NOT、AND、OR です。ただし、評価の順序を明確にして混乱を避けるために、常に括弧を使用することをお勧めします。
Q2.論理演算子を連鎖させることはできますか?
答え:
はい、論理演算子を連鎖させて複雑な条件を作成できます。たとえば、複数の論理 AND (&&) または論理 OR (||) 演算子を 1 つの式に組み合わせて、複数の条件を同時に評価できます。
Q3.次のコードの出力はどうなるでしょうか?
C
#include> void> main()> > > int> a = 1, b = 0, c = 5;> > int> d = a && b> |
Javaスイッチint
>
>
答え:
6>
Q4.次のコードの出力はどうなるでしょうか?
C
#include> int> main()> {> > int> i = 1;> > if> (i++ && (i == 1))> > printf> (> 'techcodeview.com
'> );> > else> > printf> (> 'Coding
'> );> }> |
>
>
答え:
Coding>