Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Logical Operators | Operators
C Basics

book
Logical Operators

Logical operators enable you to evaluate the truth of multiple expressions, establishing intricate and accurate conditions for your program.

Imagine you need to plot a point on a Cartesian coordinate system. You can only plot this point when you have both the x-coordinate and the y-coordinate. If even one of these coordinates is absent, the point can't be plotted.

We'll focus on three primary logical operators:

SymbolNameUsage ExampleDescription
&&ANDa && ba AND b
||ORa || ba OR b
!NOT!aNOT a

These operators return either true or false, depending on the given conditions.

With the introduction of the C99 standard, working with boolean data has become more straightforward thanks to the bool (or _Bool) data type. This data type can only hold two values: 1 (true) or 0 (false).

c

Main

copy
#include <stdio.h>

int main()
{
int x = 45;
int y = -4;

/* Using AND operator:
if `x` and `y` equals `true` (exists)),
point will equal `1` */
bool point = (x && y); // x = true AND y = true

printf("Point exists, because x and y exists\n");
printf("Result of expression equal: %d\n", point);

return 0;
}
1234567891011121314151617
#include <stdio.h> int main() { int x = 45; int y = -4; /* Using AND operator: if `x` and `y` equals `true` (exists)), point will equal `1` */ bool point = (x && y); // x = true AND y = true printf("Point exists, because x and y exists\n"); printf("Result of expression equal: %d\n", point); return 0; }

To provide more context:

c

Main

copy
#include <stdio.h>

int main()
{
int x = 45;
int y = 150;

bool point = (x < 50 && y > 100); // (x < 50) AND (y > 100)

printf("Is the expression logically correct?\n");
printf("%d\n", point);

return 0;
}
1234567891011121314
#include <stdio.h> int main() { int x = 45; int y = 150; bool point = (x < 50 && y > 100); // (x < 50) AND (y > 100) printf("Is the expression logically correct?\n"); printf("%d\n", point); return 0; }

Logical operators really shine when used in conjunction with conditional statements and loops.

question mark

Arrange the logical operators in the specified order: &&, ||, !

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 6

Demandez à l'IA

expand
ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

some-alt