2. C Operators, Decision Making & Iteration Loops
Comprehensive Guide: Arithmetic, bitwise & logical operators, if-else ladders, switch case, while, for & do-while loops
Module 1: C Operators & Bitwise Manipulations
Operators execute arithmetic, logical, and bit-level calculations on variables.
🇬🇧 English Definition
Operators are symbols that inform the compiler to perform specific mathematical, logical, or bitwise operations on operands.
🇮🇳 Hinglish Definition
Operators wo special symbols hote hai jo data par calculation ya comparison perform karte hai (jaise +, -, &&, ||, &, |).
Core Concepts & Rules
- Arithmetic: +, -, *, /, % (Modulus returns integer remainder).
- Relational: ==, !=, >, <, >=, <=.
- Logical: && (AND), || (OR), ! (NOT).
- Bitwise: & (Bitwise AND), | (Bitwise OR), ^ (XOR), ~ (NOT), << (Left Shift), >> (Right Shift).
🔍 Line-by-Line Code Breakdown
- Line 2-3: Initialize int a = 12 (1100 in binary), b = 5 (0101 in binary).
- Line 4: a & b yields 4 (0100 in binary).
- Line 5: a | b yields 13 (1101 in binary).
- Line 6: a << 1 left shifts bits by 1 (multiplies 12 * 2 = 24).
int main() {
int a = 12, b = 5;
printf("Bitwise AND (a & b): %d\n", a & b);
printf("Bitwise OR (a | b): %d\n", a | b);
printf("Left Shift (a << 1): %d\n", a << 1);
return 0;
}
Module 2: Control Flow: if-else Ladder & switch case
Directing execution paths based on Boolean condition evaluations.
🇬🇧 English Definition
Control Flow structures evaluate conditions to determine which code blocks are executed or skipped during runtime.
🇮🇳 Hinglish Definition
Control Flow se hum tay karte hai ki kaunsa code kab chalega, jaise if-else se condition match hone par aage badhna.
Core Concepts & Rules
- if-else ladder checks conditions sequentially.
- switch case matches integral/char expressions against case constants.
- break statement is mandatory in switch cases to prevent fall-through.
🔍 Line-by-Line Code Breakdown
- Line 2: Set char code = 'B'.
- Line 3-9: Switch statement checks code matching case 'A' or 'B'.
- Line 6: Case 'B' matches -> prints 'Grade B: Good Effort' and breaks out of switch.
int main() {
char code = 'B';
switch (code) {
case 'A':
printf("Grade A: Excellent!\n");
break;
case 'B':
printf("Grade B: Good Effort by Instructor Student!\n");
break;
default:
printf("Grade Other\n");
}
return 0;
}
Module 3: Iteration Loops: for, while & do-while
Repeating code blocks until termination condition turns false.
🇬🇧 English Definition
Loops execute a block of statements repeatedly as long as a specified condition remains true.
🇮🇳 Hinglish Definition
Loop ek hi task ko baar-baar repeat karne me kaam aata hai. for loop counter ke liye aur while condition ke liye best hota hai.
Core Concepts & Rules
- for loop: for (initialization; condition; increment/decrement).
- while loop: Entry-controlled loop; checks condition before execution.
- do-while loop: Exit-controlled loop; executes code at least ONCE even if condition is false.
🔍 Line-by-Line Code Breakdown
- Line 2-4: for loop initializes i=1, runs while i<=3, increments i after each iteration.
- Line 3: Prints current loop counter value.
int main() {
int i;
for (i = 1; i <= 3; i++) {
printf("Iteration Count: %d\n", i);
}
return 0;
}