Kyurious Minds Computer Academy C++ Chapter 3: Control Structures

Chapter 3: Control Structures


Key Points:

  • Understand and use if, if-else, else if, and switch statements for conditional logic.
  • Utilize for, while, and do-while loops for iterative operations.
  • Implement nested loops to handle multi-dimensional data or complex iterations.

1. Conditional Statements

A. if Statement

  • Syntax:
  if (condition) {
      // code to be executed if condition is true
  }
  • Example:
  int number = 10;
  if (number > 0) {
      cout << "Number is positive." << endl;
  }

B. if-else Statement

  • Syntax:
  if (condition) {
      // code to be executed if condition is true
  } else {
      // code to be executed if condition is false
  }
  • Example:
  int number = -5;
  if (number > 0) {
      cout << "Number is positive." << endl;
  } else {
      cout << "Number is non-positive." << endl;
  }

C. else if Statement

  • Syntax:
  if (condition1) {
      // code to be executed if condition1 is true
  } else if (condition2) {
      // code to be executed if condition2 is true
  } else {
      // code to be executed if none of the above conditions are true
  }
  • Example:
  int number = 0;
  if (number > 0) {
      cout << "Number is positive." << endl;
  } else if (number < 0) {
      cout << "Number is negative." << endl;
  } else {
      cout << "Number is zero." << endl;
  }

D. switch Statement

  • Syntax:
  switch (expression) {
      case value1:
          // code to be executed if expression == value1
          break;
      case value2:
          // code to be executed if expression == value2
          break;
      default:
          // code to be executed if expression does not match any case
  }
  • Example:
  int day = 3;
  switch (day) {
      case 1:
          cout << "Monday" << endl;
          break;
      case 2:
          cout << "Tuesday" << endl;
          break;
      case 3:
          cout << "Wednesday" << endl;
          break;
      default:
          cout << "Weekend" << endl;
  }

2. Loops

A. for Loop

  • Syntax:
  for (initialization; condition; update) {
      // code to be executed in each iteration
  }
  • Example:
  for (int i = 0; i < 5; i++) {
      cout << i << " ";
  }

B. while Loop

  • Syntax:
  while (condition) {
      // code to be executed as long as condition is true
  }
  • Example:
  int i = 0;
  while (i < 5) {
      cout << i << " ";
      i++;
  }

C. do-while Loop

  • Syntax:
  do {
      // code to be executed at least once, and then repeatedly while condition is true
  } while (condition);
  • Example:
  int i = 0;
  do {
      cout << i << " ";
      i++;
  } while (i < 5);

D. Nested Loops

  • Syntax:
  for (int i = 0; i < rows; i++) {
      for (int j = 0; j < columns; j++) {
          // code to be executed for each pair of i and j
      }
  }
  • Example:
  for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
          cout << i << "," << j << " ";
      }
      cout << endl;
  }

Practice Tasks:

  • Write a program that uses if-else statements to determine whether a number is even or odd.
  • Create a multiplication table using nested for loops.
  • Implement a program that counts down from 10 to 1 using a while loop.