C Logical Operators in Conditions

C Tutorial

 C Logical Operators in Conditions (Beginner → Advanced)

In C language, logical operators are used to combine or modify conditions.
They are most commonly used in if statements, loops, and conditional expressions.


 What Are Logical Operators?

Logical operators work with conditions (true/false) and return either 1 (true) or 0 (false).

C has three logical operators:

OperatorNameMeaning
&&Logical ANDAll conditions must be true
``
!Logical NOTReverses the condition

 Logical AND (&&)

Rule

  •  Result is true only if all conditions are true

Example


 

  •  Both conditions must be satisfied

 Logical OR (||)

Rule

  •  Result is true if any one condition is true

Example


 

  •  Only one condition is enough

 Logical NOT (!)

Rule

  •  Reverses the result of a condition

Example


 

  • !01 (true)
  • !10 (false)

Using Logical Operators in if-else


 

  •  Common in grading systems

Logical Operators with while Loop


 

  • Loop runs only while condition is true

Logical Operators with for Loop

 Stops when i == 7


Truth Table (Very Important for Interviews)

AND (&&)

ABA && B
000
010
100
111

OR (||)

ABA || B
000
011
101
111

NOT (!)

A!A
01
10

 Short-Circuit Evaluation (Very Important)

AND (&&)



 

  • Second condition executes only if first is true

OR (||)

  • Second condition executes only if first is false
  • Prevents runtime errors (like divide by zero)

Logical vs Bitwise Operators

LogicalBitwise
&&&
`
!~
Works onConditions
Result0 or 1
  •  Do NOT confuse them

 Common Mistakes

  •  Using & instead of && 
  •  Using | instead of ||
  •  Forgetting parentheses
  • Assuming non-zero means only 1

 Any non-zero value is true


Interview Questions (Must Prepare)

  1. Difference between && and &

  2. What is short-circuit evaluation?

  3. Output of if (5 && 10)?

  4. Difference between logical and bitwise operators

  5. When || skips second condition?


Real-Life Use Cases

  • Login validation (username && password)

  • Range checking

  • Menu conditions

  • Input validation

  • Loop control


Summary

  •  Logical operators combine conditions
  • && → all true
  • || → any true
  • ! → reverse condition
  •  Short-circuit improves safety
  •  Essential for conditions, loops & interviews

You may also like...