Weekend Session1.5 hoursPhase 2: Control Structures
Learn to make programs repeat actions automatically. Master different types of loops for counting, conditions, and user interaction.
🎯 Key Skills You'll Learn:
for loops
while loops
do-while loops
Loop control (break, continue)
🚀 Session Project:
Pattern Printer with Multiple Designs
Learning Objectives
Understand what loops are and why they’re useful
Learn the for loop for counting
Learn the while loop for conditions
Practice creating repetitive programs
Warm-up (10 minutes)
Imagine you want to print numbers 1 to 100. How would you do it with what you know so far? Write out the first 10 lines of such a program. Do you see a problem with this approach?
New Concept Introduction (30 minutes)
What are Loops?
Loops let you repeat code without writing it over and over. Instead of:
// Basic structure
for(initialization; condition; update){// code to repeat
}// Example: count from 1 to 10
for(int i =1; i <=10; i++){ cout <<"Count: "<< i << endl;}
Breaking it down:
int i = 1: Start with i = 1
i <= 10: Keep going while i is 10 or less
i++: Add 1 to i after each loop
Common for Loop Patterns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Count up
for(int i =0; i <5; i++){ cout << i <<" ";// Prints: 0 1 2 3 4
}// Count down
for(int i =5; i >0; i--){ cout << i <<" ";// Prints: 5 4 3 2 1
}// Count by 2s
for(int i =0; i <=10; i +=2){ cout << i <<" ";// Prints: 0 2 4 6 8 10
}
The while Loop
Use when you don’t know exactly how many times to repeat:
1
2
3
4
5
6
7
8
9
10
11
int number;cout <<"Enter a positive number (0 to quit): ";cin >> number;while(number >0){ cout <<"You entered: "<< number << endl; cout <<"Enter another number (0 to quit): "; cin >> number;}cout <<"Goodbye!"<< endl;
// break: exit the loop immediately
for(int i =1; i <=10; i++){if(i ==5){break;// Stop when i reaches 5
} cout << i <<" ";// Prints: 1 2 3 4
}// continue: skip to next iteration
for(int i =1; i <=10; i++){if(i %2==0){continue;// Skip even numbers
} cout << i <<" ";// Prints: 1 3 5 7 9
}
Hands-on Practice (40 minutes)
Exercise 1: Multiplication Table (15 minutes)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include<iostream>usingnamespace std;intmain(){int number; cout <<"Enter a number for multiplication table: "; cin >> number; cout <<"Multiplication table for "<< number <<":"<< endl;for(int i =1; i <=12; i++){ cout << number <<" x "<< i <<" = "<< number * i << endl;}return0;}
Exercise 2: Number Guessing Game with Attempts (15 minutes)
#include<iostream>usingnamespace std;intmain(){int secretNumber =73;int guess;int attempts =0;int maxAttempts =5; cout <<"I'm thinking of a number between 1 and 100."<< endl; cout <<"You have "<< maxAttempts <<" attempts!"<< endl;while(attempts < maxAttempts){ cout <<"Attempt "<<(attempts +1)<<": Enter your guess: "; cin >> guess; attempts++;if(guess == secretNumber){ cout <<"Congratulations! You got it in "<< attempts <<" attempts!"<< endl;break;}elseif(guess < secretNumber){ cout <<"Too low!"<< endl;}else{ cout <<"Too high!"<< endl;}if(attempts == maxAttempts){ cout <<"Sorry! You've used all attempts. The number was "<< secretNumber << endl;}}return0;}
#include<iostream>usingnamespace std;intmain(){int choice;do{ cout <<"\n=== Calculator Menu ==="<< endl; cout <<"1. Add two numbers"<< endl; cout <<"2. Multiply two numbers"<< endl; cout <<"3. Count to a number"<< endl; cout <<"4. Exit"<< endl; cout <<"Enter your choice: "; cin >> choice;switch(choice){case1:{double a, b; cout <<"Enter two numbers: "; cin >> a >> b; cout <<"Result: "<< a + b << endl;break;}case2:{double a, b; cout <<"Enter two numbers: "; cin >> a >> b; cout <<"Result: "<< a * b << endl;break;}case3:{int n; cout <<"Count to what number? "; cin >> n;for(int i =1; i <= n; i++){ cout << i <<" ";} cout << endl;break;}case4: cout <<"Goodbye!"<< endl;break;default: cout <<"Invalid choice!"<< endl;}}while(choice !=4);return0;}
Challenge/Quiz (10 minutes)
Challenge: Pattern Printer
Create a program that asks the user for a number and prints these patterns:
For input 5:
Right triangle:
*
**
***
****
*****
Countdown:
5 4 3 2 1 Blast off!
Number pyramid:
1
12
123
1234
12345
Use different types of loops for each pattern.
Validation Prompt
🤖 AI Validation Prompt
Copy this prompt and your code to ChatGPT or Claude for instant feedback:
```
I'm learning C++ and just completed Session 8. Here's my solution to the Pattern Printer challenge:
[PASTE YOUR CODE HERE]
The challenge was: Create three different patterns using different types of loops.
Please check if my solution:
1. Uses for loops appropriately for counting patterns
2. Creates the correct visual patterns
3. Uses nested loops where needed
4. Has clear output formatting
5. Demonstrates understanding of loop control
If there are issues, please explain what's wrong and give me a hint to fix it (don't give me the complete solution).
```
What You Learned
✅ For loops for counting and known repetitions
✅ While loops for condition-based repetition
✅ Do-while loops for at-least-once execution
✅ Loop control with break and continue
✅ Creating interactive menu systems
Next Session Preview
In Session 9, you’ll learn about nested loops and create more complex patterns and programs!