Scholar Assistant-C4 Notes

Explained in a simple, friendly, step-by-step way — like teaching a beginner who wants to master the topic, not just skim it.

We’ll go section by section, explaining what, why, and how, with easy examples and tips.


Chapter 4: How to Code Loops — Easy Mode


WHY THIS CHAPTER IS IMPORTANT:

In the last chapter, you learned how to make your code decide what to do using if and switch.

Now it’s time to learn how to make your code repeat things using loops. This is one of the most important tools in programming — especially in real-world tasks like processing files, handling user input, and building games.

But before loops, let’s get better at math in code.


Part 1: Smarter Math in C++ (Before Looping)

Unary Operators (They work on ONE thing)

These are mini-math shortcuts that work on just one variable:

Operator

What it does

Example

++

Adds 1

++x or x++

--

Subtracts 1

--x or x--

+

Says it's positive

+x (not useful)

-

Makes it negative

-x

Prefix vs Postfix
  • ++x → Add first, then use.

  • x++ → Use first, then add.

Best practice: Use ++x unless you have a reason to use x++. It's faster and cleaner.

Example:
int x = 5;
int y = ++x;  // x = 6, y = 6
int z = x++;  // z = 6, x = 7

Works with characters too:

char c = 'A';
++c;  // c becomes 'B'

Compound Assignment (Shortcuts!)

Instead of:

x = x + 5;

You can just write:

x += 5;

Same for:

  • -= (subtract)

  • *= (multiply)

  • /= (divide)

  • %= (remainder)

It makes your code shorter and easier to read.


Operator Precedence (Who Goes First?)

Just like in math class, some operations go before others.

Order:

  1. ++ or -- (prefix)

  2. + or - (signs)

  3. *, /, %

  4. +, - (addition/subtraction)

If you want to change the order, use parentheses.

Example:
double price = 100;
double discount = 0.2;

price = price * 1 - discount;       // Wrong: gives 99.8
price = price * (1 - discount);     // Right: gives 80

Always use parentheses if you’re not 100% sure. It makes your code safer and easier to understand.


Part 2: Loops — Doing Things Over and Over

There are 3 main types of loops in C++:


While Loops — “Check, then Do”

while (condition) {
  // do something
}
  • It checks the condition first.

  • If it's true, it runs the code inside { }.

  • It keeps going until the condition becomes false.

Example:
int i = 1;
while (i < 5) {
  cout << i;
  ++i;
}
// prints: 1234

⚠️ If the condition never becomes false, the loop runs forever (infinite loop)!


Do-While Loops — “Do First, THEN Check”

do {
  // do something
} while (condition);
  • It runs at least once, even if the condition is false.

  • Good for menus or user input (like “ask again?”)

Example:
int i = 1;
do {
  cout << i;
  ++i;
} while (i < 5);

🟡 Difference: while checks first, do-while checks after.


For Loops — “Count-Controlled Loops”

Perfect when you know how many times you want to loop.

for (start; condition; update) {
  // do something
}
Example:
for (int i = 1; i < 5; ++i) {
  cout << i;
}
// prints: 1234

It’s just a compact version of a while loop with all parts (start, check, update) in one line.

Reverse Loop:
for (int i = 10; i > 0; --i) {
  cout << i;
}

Nested Loops (Loops inside Loops)

You can put one loop inside another.
This is often used to print grids, tables, or compare every item with every other item.

Example:
for (int row = 1; row <= 3; ++row) {
  for (int col = 1; col <= 4; ++col) {
    cout << "*";
  }
  cout << endl;
}
// prints a 3x4 grid of stars

Break and Continue

These help you control your loops better.

🔹 break

Stops the loop immediately.

while (true) {
  string command;
  cin >> command;
  if (command == "exit") break;
}

🔹 continue

Skips the current loop turn and jumps back to the top.

for (int i = 1; i <= 5; ++i) {
  if (i == 3) continue;
  cout << i;
}
// prints: 1245

Summary — Remember These Key Points:

Use while when you don’t know how many times you’ll loop.
Use for when you do.
Use do-while when you must run at least once.
Use break to stop the loop.
Use continue to skip to the next round.