C Workshop · Logical Operators
Lesson C

Logical Operators

&& is and, || is or, ! is not. C stops early when the answer is already known (short-circuit).

What it is

Logical operators combine yes/no values. And (&&) needs both. Or (||) needs at least one. Not (!) flips the answer. They are how you write “door closed and seatbelt on.”

Approach

  1. Evaluate the left operand.
  2. If && and the left is 0, skip the right (already false).
  3. Print the matching message.

Where this is used in the real world

Diagram

1

a=1

true

2

&&

and

3

b=0

false

4

false

Not both

Run algorithm

Each step highlights the matching C line. Program output appears only when a line that prints actually runs.

Ready
C code
1#include <stdio.h>
2int main(void) {
3    int a = 1, b = 0;
4    if (a && b) printf("Both\n");
5    else printf("Not both\n");
6    return 0;
7}
What this line does
Press Run algorithm. The highlight will move through the C code.
True branch
the if body runs
False branch
else / skip
Program output
(no output yet)

Check your understanding

Five questions. Pick one answer each, then Check answers. The correct choice turns green.

1. 1 && 0 is:

And needs both true.

2. This program prints:

else branch.

3. || is:

Logical or.

4. !x is true when x is:

Not: 0 becomes 1.

5. Short-circuit && means:

Left false → skip right.