C++ Programming (Loops and Files) Notes

Increment and Decrement Operators
  • Increment Operator (++)

  • Function: Adds one to a variable.

  • Examples:

    • val++; is equivalent to val = val + 1;
    • Can be used in prefix form ++val; or postfix form val++;.
  • **Decrement Operator (--)

  • Function: Subtracts one from a variable.

  • Examples:

    • val--; is equivalent to val = val - 1;
    • Can also be used in prefix form --val; or postfix form val--;.
Usage in Programming
  • The following program demonstrates usage of increment and decrement operators:
  #include <iostream>
  using namespace std;
  int main() {
      int num = 4;
      cout << "The variable num is " << num << endl;
      cout << "I will now increment num. \n\n";
      num++;  // Postfix increment
      cout << "Now the variable num is " << num << endl;
      ++num;  // Prefix increment
      cout << "Now the variable num is " << num << endl;
      num--;  // Postfix decrement
      cout << "Now the variable num is " << num << endl;
      --num;  // Prefix decrement
      cout << "Now the variable num is " << num << endl;
      return 0;
  }
  • Output:
    • The variable num is 4
    • Now the variable num is 5
    • Now the variable num is 6
    • Now the variable num is 5
    • Now the variable num is 4
Prefix vs Postfix
  • Prefix Mode (e.g. ++val)
  • Increments/decrements the value, then returns it.
  • Postfix Mode (e.g. val++)
  • Returns the value, then increments/decrements.
  • Example:
  int val = 12;
  cout << val++;  // Outputs 12; val becomes 13
  cout << ++val;  // val becomes 14; outputs 14
Introduction to Loops
  • Loop Definition: A control structure that repeats a set of statements.
  • Types of Loops:
  • while loop: repeats as long as a condition is true.
  • do-while loop: executes at least once, then checks condition.
  • for loop: typically used for counter-controlled iterations.
The while Loop
  • Structure:
  while (expression) { statement; }
  • Execution Flow:
  1. Evaluate expression.
  2. If true, execute statement; repeat.
  3. If false, exit loop.
  • Example:
  int number = 1;
  while (number <= 5) {
      cout << "Hello\n";
      number++;
  }
  • Output: Prints "Hello" 5 times.
Input Validation with while Loops
  • Purpose: Ensures user input meets specific criteria before proceeding.
  • Approach:
  while (input is invalid) {
      display error;
      read input again;
  }
  • Example:
  cout << "Enter a number less than 10: ";
  cin >> number;
  while (number >= 10) {
      cout << "Invalid Entry!\n";
      cout << "Enter a number less than 10: ";
      cin >> number;
  }
Counters
  • Counter Definition: A variable that is incremented or decremented with each loop iteration. It controls the number of loop executions.
  • Initialization: Must be initialized before entering the loop.
The do-while Loop
  • Structure:
  do statement; while (expression);
  • Execution: Code executes first, then checks condition. Always executes at least once.

  • Example:

  do {
      cout << "Hello\n";
  } while (number < 0);
The for Loop
  • Structure:
  for (initialization; test; update) { statement; }
  • When to use: When you know exactly how many iterations are needed.

  • Example:

  for (int count = 1; count <= 5; count++) {
      cout << "Hello" << endl;
  }
Breaking and Continuing Loops
  • break Statement: Immediately terminates the loop. Use cautiously as it may complicate code readability.
  • continue Statement: Skips the current iteration and proceeds to the next. Use cautiously for similar reasons.