logo

C Operatory logiczne

Operatory logiczne w C służą do łączenia wielu warunków/ograniczeń. Operatory logiczne zwracają 0 lub 1, zależy to od tego, czy wynik wyrażenia jest prawdziwy, czy fałszywy. W programowaniu C do podejmowania decyzji używamy operatorów logicznych.

W języku C mamy 3 operatory logiczne:



    Logiczne AND ( && ) Logiczne LUB ( || ) Logiczne NIE ( ! )

Rodzaje operatorów logicznych

1. Operator logiczny AND ( && )

Jeśli oba operandy są różne od zera, wówczas warunek staje się prawdziwy. W przeciwnym razie wynik ma wartość 0. Zwracanym typem wyniku jest int. Poniżej znajduje się tabela prawdy dla operatora logicznego AND.

X

I X i& Y

1



1

1

1



0

0

0

1

0

0

0

0

Składnia

podciąg ciągu Java
(operand_1 && operand_2)>

Przykład

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;> }>

>

>

Wyjście

Both values are greater than 0>

2. Operator logiczny OR ( || )

Warunek staje się spełniony, jeśli którykolwiek z nich jest różny od zera. W przeciwnym razie zwraca wartość false, tj. 0. Poniżej znajduje się tabela prawdy dla logicznego operatora OR.

X I X || I

1

regexp_like w mysql

1

1

1

0

1

0

1

1

0

0

0

Składnia

(operand_1 || operand_2)>

Przykład

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;> }>

>

>

Wyjście

Any one of the given value is greater than 0>

3. Operator logiczny NOT ( ! )

Jeśli warunek jest prawdziwy, operator logiczny NOT sprawi, że stanie się on fałszywy i odwrotnie. Poniżej znajduje się tabela prawdy dla operatora logicznego NOT.

X !X

0

1

1

0

Składnia

 ! (operand_1 && operand_2)>

Przykład

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;> }>

>

>

math.random Java

Operatory logiczne zwarcia

Kiedy wynik można określić poprzez ocenę poprzedniego wyrażenia logicznego bez oceny dalszych operandów, nazywa się to zwarciem.

Zwarcie można zaobserwować w równaniu posiadającym więcej niż jeden operator logiczny. Mogą zawierać AND, OR lub jedno i drugie.

1. Zwarcie w operatorze logicznym AND

Operator logiczny AND zwraca wartość true wtedy i tylko wtedy, gdy wszystkie operandy mają wartość true. Jeśli pierwszy operand ma wartość false, dalsze operandy nie będą oceniane. Dzieje się tak, ponieważ nawet jeśli dalsze operandy zwrócą wartość true, cały warunek nadal zwróci wartość false.

Przykład

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)> >return> 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;> }>

>

>

Wyjście

Both conditions are satisfied. Conditions not satisfied.>

2. Zwarcie w operatorze logicznym OR

Operator LUB zwraca prawdę, jeśli co najmniej jeden operand ma wartość prawda. Jeśli pierwszy operand ma wartość true, dalsze operandy nie będą oceniane. Dzieje się tak, ponieważ nawet jeśli dalsze operandy zwrócą wartość false, cały warunek nadal zwróci wartość true.

Przykład

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)> >return> 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;> }>

rj12 kontra rj11
>

>

Wyjście

At least one condition is satisfied. Conditions not satisfied.>

Często zadawane pytania dotyczące operatorów logicznych

Pytanie 1. Jakie jest pierwszeństwo operatorów logicznych w programowaniu?

Odpowiedź:

Pierwszeństwo operatorów logicznych to: NOT, AND, OR. Zawsze jednak zaleca się używanie nawiasów, aby wyraźnie określić kolejność oceniania i uniknąć nieporozumień.

Pytanie 2. Czy operatory logiczne można łączyć w łańcuchy?

Odpowiedź:

Tak, operatory logiczne można łączyć ze sobą w celu tworzenia złożonych warunków. Na przykład możemy połączyć wiele operatorów logicznych AND (&&) lub logicznych OR (||) w jednym wyrażeniu, aby ocenić wiele warunków jednocześnie.

Pytanie 3. Jaki będzie wynik poniższego kodu?

C


sieciowy system operacyjny



#include> void> main()> > >int> a = 1, b = 0, c = 5;> >int> d = a && b>

>

>

Odpowiedź:

6>

Pytanie 4. Jaki będzie wynik poniższego kodu?

C




#include> int> main()> {> >int> i = 1;> >if> (i++ && (i == 1))> >printf>(>'techcodeview.com '>);> >else> >printf>(>'Coding '>);> }>

>

>

Odpowiedź:

Coding>