Labs ICT
Pro Login

Logical Operators

Logical operators let you combine multiple conditions. Is this and that both true? Is either of them true? Is something not true? These operators work with boolean values — 1 for true, 0 for false.

The Three Logical Operators

  • && — AND. True only if both sides are true.
  • || — OR. True if at least one side is true.
  • ! — NOT. Flips true to false and false to true.

Short-Circuit Evaluation

C is lazy in a good way. With &&, if the left side is false, C does not even evaluate the right side — the result is already false. With ||, if the left side is true, C skips the right side. This is called short-circuit evaluation, and it can prevent errors if used carefully.


#include 

int main() {
  int age = 25;
  int hasID = 1;

  if (age >= 18 && hasID) {
    printf("You can enter.\n");
  }

  int isWeekend = 0;
  int isHoliday = 1;

  if (isWeekend || isHoliday) {
    printf("No work today!\n");
  }

  printf("NOT isWeekend: %d\n", !isWeekend);
  return 0;
}
    
Try it Yourself →