Skip to content

Lecture 6: Loops and Conditionals

1. Conditional Statements

The if Statement

Executes a block of code if a specified condition is true.

cpp
if (condition) {
  // code to execute if condition is true
}

Example:

cpp
int x = 20, y = 18;
if (x > y) {
  cout << "x is greater than y";
}

The if...else Statement

Used to execute one block if the condition is true, and another if it's false.

cpp
if (condition) {
  // if true
} else {
  // if false
}

Example:

cpp
int d;
cout << "Enter student degree (0–100): ";
cin >> d;

if (d >= 50)
  cout << "Student passed";
else
  cout << "Student failed";

The else if Ladder

Used when multiple conditions need to be tested.

cpp
if (condition1) {
  // code block 1
} else if (condition2) {
  // code block 2
} else {
  // code block 3
}

Comparison and Logical Operators

OperatorDescriptionExample
==Equal tox == y
!=Not equalx != y
>Greater thanx > y
<Less thanx < y
>=Greater or equalx >= y
<=Less or equalx <= y
&&Logical ANDa > 4 && a < 10
|Logical ORa < 9 | a < 10
!Logical NOT!(a < 7 && a < 15)

The switch Statement

Used to execute different actions based on multiple possible values.

cpp
switch(expression) {
  case value1:
    // code block
    break;
  case value2:
    // code block
    break;
  default:
    // code if no case matches
}

Example:
A simple calculator using switch:

cpp
float n1, n2;
char op;

cout << "Enter first number: ";
cin >> n1;
cout << "Enter second number: ";
cin >> n2;
cout << "Enter operator (+, -, *, /, %): ";
cin >> op;

switch(op) {
  case '+': cout << n1 + n2; break;
  case '-': cout << n1 - n2; break;
  case '*': cout << n1 * n2; break;
  case '/':
    if (n2 != 0)
      cout << n1 / n2;
    else
      cout << "Cannot divide by zero";
    break;
  case '%':
    cout << int(n1) % int(n2);
    break;
  default:
    cout << "Invalid operator";
}

2. Loops in C++

While Loop

Repeats a block of code while a condition is true.
Used when the number of iterations is unknown.

cpp
int i = 0;
while (i < 5) {
  cout << i << "\n";
  i++;
}

Trace Example:

Stepi (before)ConditionActioni (after)Output
10trueprint 010
21trueprint 121
32trueprint 232
43trueprint 343
54trueprint 454
65falsestop

Do-While Loop

Executes the loop body at least once, then checks the condition.

cpp
int i = 0;
do {
  cout << i << "\n";
  i++;
} while (i < 5);

Trace Example:

Stepi (before)Outputi (after)ConditionContinue?
1001trueyes
2112trueyes
3223trueyes
4334trueyes
5445falseno

For Loop

Used when the number of iterations is known.

cpp
for (int i = 0; i <= 10; i += 2) {
  cout << i << "\n";
}

Trace Example:

Stepi (start)ConditionOutputi (next)
10true02
22true24
34true46
46true68
58true810
610true1012
712false

3. Factorial Calculation

Formula:

n!=n×(n1)×(n2)×...×1

Example Program:

cpp
int n;
long factorial = 1;

cout << "Enter a non-negative integer: ";
cin >> n;

if (n < 0) {
  cout << "Factorial not defined for negative numbers.";
} else {
  for (int i = 1; i <= n; ++i) {
    factorial *= i;
  }
  cout << "Factorial of " << n << " is " << factorial;
}

Trace Example (n = 5):

iOperationfactorial
11 × 11
21 × 22
32 × 36
46 × 424
524 × 5120

Result: 5! = 120

4. Nested Loops (Pattern Printing)

Example: Right-Angled Triangle

cpp
int n;
cout << "Enter number of rows: ";
cin >> n;

for (int i = 1; i <= n; ++i) {
  for (int j = 1; j <= i; ++j) {
    cout << "*";
  }
  cout << endl;
}

Output (for n = 4):

*
**
***
****

Trace Explanation:

  • Outer loop controls rows (i).
  • Inner loop controls number of stars per row (j).
  • Each new row prints one more star.

5. Practice Tasks

  1. Print the following patterns:

    ****
    ***
    **
    *
  2. Create a program to print a multiplication table.

Summary

  • Conditional statements (if, else if, switch) allow decision making.
  • Loops (while, do-while, for) enable repeated execution.
  • Nested loops are used for complex repetitive patterns.
  • Control structures are essential for logical flow in C++ programming.