21.6.25

20250621 15:00 Chapter 3 : Repeating with Loops

                                        

← Previous 🏠 Homepage Next Chapter →

                                                         

The ! (NOT) Operator in C

  • ! is used to reverse a condition.
  • If a condition is true (non-zero), ! makes it false (0).

  • If a condition is false (0), ! makes it true (1).

Examples:

if (!flag)   // same as if (flag == 0)
! (y < 10)   // same as y >= 10

Updated Operator Precedence (Order):


Common Mistakes in if Statements


Mistake 1: Using = instead of ==
if (i = 5)   // Wrong: assigns 5 to i, always true!

Use:

if (i == 5)  // Correct: checks if i equals 5

Mistake 2: Extra semicolon

if (i == 5);  // Wrong: ends the if statement
    printf("You entered 5");  // Always runs!

Logical Operators Truth Table

| x | y | !x | !y | x && y | x || y |
|---------|---------|------|------|----------|----------|
| 0 | 0 | 1 | 1 | 0 | 0 |
| 0 | non-zero| 1 | 0 | 0 | 0 |
| non-zero| 0 | 0 | 1 | 0 | 1 |
| non-zero| non-zero| 0 | 0 | 1 | 1 |

The Conditional Operator (?:)


Used as a shorter version of if-else.

Syntax:

condition ? true_value : false_value;

Example:

y = (x > 5 ? 3 : 4);  // If x > 5, y = 3, else y = 4

You can even use it with printf():

i == 1 ? printf("Amit") : printf("All and sundry");

Be careful with assignments!

a > b ? g = a : (g = b);  // Use () around assignment after :

        

          Chapter 3 : Repeating with Loops


Loops: Doing Things Again and Again

Computers are great at repeating tasks.

Instead of writing the same code multiple times, we use loops to repeat a block of code.

Why Use Loops?


Imagine printing salaries of 10 people or converting temperatures for 15 cities.
Instead of writing 15 separate print statements, you can write just one loop!

Types of Loops in C

There are 3 ways to make a loop in C:

  1. while loop – repeats as long as a condition is true

  2. for loop – repeats for a fixed number of times

  3. do-while loop – similar to while, but runs at least once

while Loop (Repeat until false)

int count = 1;
while (count <= 3) {
// code to repeat
count = count + 1;
}
  • Starts from count = 1

  • Checks if count is <= 3

  • Repeats the block and increases count by 1

Be careful: if you forget to update the counter, the loop may run forever!

for Loop (Best for fixed repeats)

for (int i = 1; i <= 10; i++) {
printf("%d\n", i);
}
  • Starts at 1

  • Stops when i becomes 11

  • Increases i after every loop

You can also do:

for ( ; i <= 10; ) // i already declared

or even:

for ( ; ; ) // Infinite loop

do-while Loop (Runs at least once)

int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 10);

This loop runs the code first, then checks the condition.

Nested Loops (Loop inside a loop)

for (int r = 1; r <= 3; r++) {
for (int c = 1; c <= 2; c++) {
printf("r = %d, c = %d, sum = %d\n", r, c, r + c);
}
}

This prints every combination of r and c. Outer loop runs 3 times, and for each, the inner loop runs 2 times.



                          

No comments:

Post a Comment

rating System

Loading...

Understanding Arrays in C

The Bookshelf Analogy & Book-Author Example Arrays are one of the most essential concepts in C programming. They let you store multiple ...