PHINMA – Cagayan de Oro College
College of Engineering and Architecture
BES 043
Computer Fundamentals and Programming
Laboratory Manual
Activity No. 9
Iteration Statement Part 3 (Nested loop statement)
Objectives: 1. Demonstrate nested loops operation
2. Create a C++ program using nested loop
Materials: Computer & C/C++ programming language software tool
Nested Loops Program logic gets more complicated when you must use loops
within loops, or nested loops. When one loop appears inside another, the loop
that contains the other loop is called the outer loop, and the loop that is
contained is called the inner loop. You need to create nested loops when the
values of two or more variables repeat to produce every combination of values.
Usually, when you create nested loops, each loop has its own loop control
variable.
A loop can be nested inside of another loop. C++ allows at least 256 levels of
nesting.
The syntax for a nested for loop statement in C++ is as follows −
for ( init; condition; increment ) {
for ( init; condition; increment ) {
statement(s);
statement(s); // you can put more statements.
The syntax for a nested while loop statement in C++ is as follows −
while(condition) {
while(condition) {
statement(s);
}
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
statement(s); // you can put more statements.
The syntax for a nested do...while loop statement in C++ is as follows −
do {
statement(s); // you can put more statements.
do {
statement(s);
} while( condition );
} while( condition );
Procedure:
1. Let's have a look at the following code below
2. #include <iostream>
3. using namespace std;
4.
5. int main () {
6. int i, j;
7.
8. for(i = 2; i<100; i++) {
9. for(j = 2; j <= (i/j); j++)
10. if(!(i%j)) break; // if factor found, not prime
11. if(j > (i/j)) cout << i << " is prime\n";
12. }
13.
14. return 0;
15. }
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
2. Run the program and place the output on the space provided
3. Write your observations on the output or how and why you arrive that output.
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
______________________________________________________________
4. Write a program in C++ to print a square pattern with # character.
Sample Output:
Print a pattern like square with # character:
--------------------------------------------------
Input the number of characters for a side: 4
####
####
####
####
5. Write a program in C++ to display the pattern like right triangle using an asterisk.
Sample Output:
Input number of rows: 5
*
**
***
****
*****
6. Write a program in C++ to make such a pattern like a pyramid using number
and a number will repeat for a row.
Sample Output:
Input number of rows: 5
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Procedure 4 Procedure 5
Procedure 6
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
Observation:
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Conclusion
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
________________________________________________________________
Rubric
1 2 3 4
Student spent too Student spent too Student spent an Student spent an
much time and/or much time and/or adequate amount of adequate amount of
Adequate Time too little time on too little time on time on computer time on computer
Spent on Activity entire computer parts of computer lab activity to ensure lab activity to ensure
lab activity. lab activity. good results. the best results.
Student put little to Student put little Student put a good Student put a great
no effort towards effort towards amount of effort deal of effort
Effort computer lab computer lab towards computer towards computer
activity activity. lab activity. lab activity.
Student completed Student completed Student completed Student completed
less than 1/2 of about 1/2 of the about 80% of the all of the computer
Completion of the computer lab computer lab computer lab activity lab activity by the
Task activity by the due activity by the due by the due date. due date.
date. date.
Responses and Responses and Responses and Responses and
information given information given information given information given
Reasonable are entirely are unreasonable are reasonable are very reasonable
Response and unreasonable in some areas of throughout most of throughout all of the
Information throughout the the activity. the activity. activity.
activity.
Neatness, Responses and Responses and Responses and Responses and
Readability, and information given information given information given information given
Legibility are entirely are unreadable are neat, readable, are very neat,
PHINMA – Cagayan de Oro College
College of Engineering and Architecture
unreadable and and illegible and legible readable, and
illegible throughout most of throughout most of legible throughout all
throughout the the activity. the activity. of the activity.
activity.
Instructor: Percentage Total
Name: Subject
Code