Lab06
Lab06
Lab Outcomes:
Understand Nested Loop Concepts: Gain a solid understanding of how nested loops
operate, including how the outer and inner loops interact to produce patterns or iterate
through multidimensional data.
Pattern-Based Problems: Practice creating various patterns (e.g., triangles, squares,
pyramids) using nested loops, developing logic and control over loop iterations.
Revise:
Definition of for Loop in C++?
A for loop is a control structure that allows you to repeat a block of code a specific number of
times. It is often used when you know in advance how many times you want to execute a
particular task.
Example:
Imagine you are a student helping to arrange chairs for a classroom. You have 10 chairs and you
need to put them in a straight line. Instead of picking up one chair, placing it, then repeating the
same for each chair, you decide to automate your actions:
Nested Loop:
A nested for loop is a loop inside another loop. You use this structure when you need to perform
repetitive tasks within another repetitive task.
#include<iostream>
using namespace std;
// Outer loop
int main(){
for (int i = 1; i <= 2; i++) {
cout << "Outer: " << i << "\n"; // Executes 2 times
// Inner loop
for (int j = 1; j <= 3; j++) {
cout << " Inner: " << j << "\n"; // Executes 6 times (2 * 3)
}
}
return 0;
}
Output:
#include<iostream>
using namespace std;
// Outer loop
int main(){
for (int i = 1; i <= 5; i++) {
// Inner loop
for (int j = 1; j <= 5; j++) {
cout << "#"; // Executes 25 times (5 * 5)
}
cout<<endl;
}
return 0;
}
Output:
Example Code 1.3:
#include<iostream>
using namespace std;
int main(){
int i=5;
int j;
for(i;i>=1;i--){
for(j=1;j<=i;j++){
cout<<"*";
}
cout<<endl;
}
return 0;
}
Output:
While Loop:
Loops can execute a block of code as long as a specified condition is reached.
Loops are handy because they save time, reduce errors, and they make code more readable.
Another Definition
A while loop is a way to repeat a set of instructions as long as a specific condition is true.
Syntax:
while (condition) {
// code block to be executed
}
How it works:
1. The loop starts by checking a condition.
2. If the condition is true, the code inside the loop runs.
3. After running, it goes back and checks the condition again.
4. This keeps going in a "loop" until the condition is no longer true, at which point it stops.
For example: If you're stacking books until you reach a height of 10 books, you'd keep adding a
book, then check, “Is it 10 yet?” If not, you add another. Once you reach 10, you stop. This is
how a while loop works!
Example Code:
int i = 1;
while (i <= 5) {
cout << i <<endl;
i++;
}
#include <iostream>
using namespace std;
int main() {
int i = 1; // Initialize the outer loop variable
while (i <= 2) { // Outer loop condition
cout << "Outer: " << i << endl; // Executes 2 times
Lab Task
Note: Implement Each Task using nested loop and nested while loop.
Problem Statement:
Task 1: Write a C++ program that will print the following pattern.
*
**
***
****
*****
******
*******
Task 2: Write a program to print a pyramid pattern with numbers where each row has
incrementally increasing numbers.
1
12
123
1234
12345
Task 3: Write a program in C++ to print a pattern based on a user-input number of rows. The
pattern should display increasing rows where each row i contains the integer i repeated i times.
Output:
Task 4: Create a program that outputs the days for a specified number of weeks. This will help you
practice using nested loops in C++ and improve your understanding of iteration and output formatting.
Requirements:
Task 5: Let’s consider a seating arrangement in a theater. Imagine a theater with multiple rows of
seats, where each row has several seats numbered from left to right. If we want to display the seat
numbers in each row.
There are 5 rows and each row has 10 seats. A nested loop can help us print each seat's number in each
row.
Rubrics Based LAB