Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Operators Part 2: Logical & Assignments

Lesson 8 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

3. Logical Operators

Used to build complex conditions by combining multiple relational expressions.

OperatorNameDescriptionExampleResult
&&ANDTrue if BOTH sides are true(5 > 3) && (8 < 10)1
``ORTrue if AT LEAST ONE side is true`(5 > 8)(2 < 4)`1
!NOTFlips the logic (True to False)!(5 > 3)0

Short-Circuit Evaluation (Crucial for Safety):

  • AND (&&): If the left side is False, C doesn't even look at the right side (because the whole thing is already False).
  • OR (||): If the left side is True, C skips the right side (because the whole thing is already True).
  • Example: if (x != 0 && 10/x > 2). If x is 0, the first part is False, and C skips the second part, preventing a "Division by Zero" crash!

4. Assignment Operators

Used to store values in variables.

  • Standard (=): x = 10; (Read as: "Assign 10 to x", not "x equals 10").
  • Compound Assignments: Combine an operation with assignment to save typing.
  • x += 5 is the same as x = x + 5
  • x -= 2 is the same as x = x - 2
  • x = 3 is the same as x = x 3
  • x /= 4 is the same as x = x / 4

5. Increment (++) and Decrement (--)

Extremely common in loops. They add or subtract 1 from a variable.

  • Prefix (++x): Increment the variable first, then use it in the calculation.
  • Postfix (x++): Use the current value in the calculation, then increment the variable.
int a = 5;
int b = a++; // b is 5, then a becomes 6
int c = ++a; // a becomes 7, then c is 7

6. The sizeof Operator

This is a unique operator that tells you how many bytes of memory a variable or data type occupies.

printf("%zu", sizeof(int)); // Usually prints 4

7. Conditional (Ternary) Operator

A one-line if-else statement. Syntax: condition ? value_if_true : value_if_false;

int max = (a > b) ? a : b;
Logical operators work on "Boolean Logic" (0 or 1), while bitwise operators (which we'll see later) work on the individual 0s and 1s inside the binary representation of the number.