LAB 6
Control Structure (Repetition) Part II – For loops, nested loops
Objectives
In this lab, you will:
•
Explore the essentials of counter- controlled repetition.
•
Use the for repetition statements o execute statements in a program repeatedly.
•
Learn to use the for loop repetition statements to execute statements in a program repeatedly.
•
Examine the nested loop
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
1
Assignment Cover Sheet Lab 6
Name:
---Student ID:
---Section:
---Date:
---Activities Assigned: check or list exercise numbers
Completed
Submitted Grade
PreLab /10
Inlab Example6-1 /5
Example6-2 /5
Example6-3 /5
Example6-4 /5
Example6-5 /5
Example6-6 /5
Example6-7 /5
Example6-8 /5
Assignmnet1 /10
Assignmnet2 /10
PostLab Project1 /10
Project2 /10
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
2
PreLab
Write a code by using while loop to print 5 “*” then convert it to for loop structure
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
3
Outlines
•
Lesson 6-1: For Loops
•
Lesson 6-2: Example on For Loop
•
Lesson 6-3: Nested Loops
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
4
For Loops Lesson 6-1
The for loop structure is a specialized form of the while loop. Its primary purpose is to simplify the writing of counter-controlled loops.
In order to write a counter-controlled loop, required the following:
1.
The name of a control variable (or loop counter).
2.
The initial value of the control variable.
3.
The loop-continuation condition that tests for the final value of the control variable.
4.
The increment (or decrement) by which the control variable is modified each time through the loop.
Designing for loop:
The general form of the for statement is:
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
5
for(initial counter; loop condition; update counter)
Statement;
Start
expression true Statement
false End
true Update counter Initiate counter
Fig 6-1: The flow chart of for loop structure
Illustrative Example:
Examples on For Loop Lesson 6-2
Explore the following examples to see the output
Example 6-1: Write the following code and show the output
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
6
#include <iostream> //line1 using namespace std; //line2 void main() //line3 { //line4 for(int i=1; i<=5; i++) //line5 cout<<"A "; //line6 cout<<"B "<<endl; //line7
} //line8
for (int counter = 1 ; counter <=10 ; counter ++)
For keyword Initial Statement Required semicolon
separator Final value of control variable for which the condition is true
Update Statement Loop-continuation
condition Initial value of control
variable
The output:
Example6-2: Write the following code and show the output
The output:
Example6-3: Write the following code and show the output
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
7
#include <iostream> //line1 using namespace std; //line2 void main() //line3 { //line4 for(int i=1; i<=5; i++) //line5 { //line6
cout<<"A "; //line7 cout<<"B "<<endl; //line8 } //line9
} //line10
#include <iostream> //line1 using namespace std; //line2 void main() //line3 { //line4 int i; //line5 for(i=1,cout<<"A "; i<=5; i++, cout<<"B ") //line6
cout<<"C "; //line7
} //line8
The output:
Example6-4: Write the following code and show the output
The output:
Example6-5: Write the following code and show the output
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
8
#include <iostream> //line1 using namespace std; //line2 void main() //line3 { //line4 int i, j; //line5 for(i=1, j=10; (i<=5|| j>=0); i=i+2, j--) //line6
cout<<"A "; //line7
} //line8
#include <iostream> //line1 using namespace std; //line2 void main() //line3 { //line4 int i=1; //line5 for(;i<=5;) //line6
cout<<++i<<" "; //line7 cout<<endl; //line8
} //line9
The output:
Example6-6: Write the following code and show the output
The output:
Example6-7: Write the following code and show the output
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
9
#include <iostream> //line1 using namespace std; //line2 void main() //line3 { //line4 int i=1; //line5 for(i=5; i<=100; i--) //line6
cout<<i<<" "; //line7 cout<<endl; //line8
} //line9
#include <iostream> //line1 using namespace std; //line2 void main() //line3 { //line4 int i=1; //line5 for(i=5; i<=1; i--) //line6
cout<<i<<" "; //line7 cout<<endl; //line8
} //line9
The output:
Example6-8: Write the following code and show the output
The output:
Convert Example 6-8 to while loop.
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
10
#include <iostream> //line1 using namespace std; //line2 void main() //line3 { //line4 int num, fact=1; //line5 cout<<"Please insert a number: "; //line6 cin>>num; //line7 for(int i=1; i<=num; i++) //line8 fact*=i;
cout<<"The Factorial of "<<num<<" is "<<fact<<endl; //line9
} //line10
The Nested Loops Lesson 6-3
The nested loops mean that there are two or more repetition structure inside each other.
for(int i=1; i<=3; i++) cout<<"*";
The above for loop is a single loop that will print 3 stars Now:
for(int i=1; i<=3; i++) //loop1 {
for(int j=1; j<=5; j++) //loop2 cout<<"*";
cout<<endl;
}
We have nested loops, loop2 inside loop1, which means that loop2 will be excuted multiple times, but loop1 will be executed only once.
Loop2 : with the control variable j will draw 5 stars *****
Loop1 with the control variable i will repeat drawing the 5 stars 3 times *****
*****
*****
To create the following pattern:
*
* *
* * *
* * * *
* * * * *
We can use the following code:
for (i = 1; i <= 5 ; i++) {
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
11
}
Example6-9: What is the result if we replace the first for statement with the following?
for (i = 5; i >= 1; i--) The output:
Example6-10: Write the following two codes to see the output
The output:
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
12
#include <iostream> //line1 using namespace std; //line2 void main() //line3 { //line4 int i,j; //line5 for(i=1; i<=5; i++) //line6 { //line7 for(j=i; j<=5; j++) //line8 cout<<"*"; //line9 cout<<endl; //line10 } //line11 cout<<i<<" "<<j<<endl; //line12
} //line13
The output:
Example6-11: How many stars the following code will print
The answer:
InLab Assignment
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
13
#include <iostream> //line1 using namespace std; //line2 void main() //line3 { //line4
for(int i=1; i<=5; i++) //line5 {
for(int x=1; x<i; x++) //line6 cout<<" "; //line7 for(int j=i; j<=5; j++) //line8 cout<<"*"; //line9 cout<<endl; //line10 } //line11 // cout<<i<<" "<<x<<" "<<j<<endl; //line12
} //line13
#include <iostream> //line1 using namespace std; //line2 void main() //line3 { //line4
for(int i=1; i<=15; i++) //line5 { //line6 for(int j=i; j<=5; j++) //line7
{ //line8 i = i+2; //line9 cout<<"*"; //line10 } //line11 cout<<endl; //line12 } //line13 } //line14
The code should be full documented
The variables and the functions should be named with suitable names Template Assignments:
- Write a program that prompt the user to insert an integer number then the program will find all the prime numbers between 1 and the inserted number
- Write a program that prompts the user to insert a number and draw an empty square of @ symbols
If the number is 4
@@@@
@ @
@ @
@@@@
PostLab Assignment
The instructors will provide you with the appropriate projects The students should write the algorithms
The code should be full documented
The variables and the functions should be named with suitable names
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
14
Error sheet Lab5
Name:
---Student ID:
---Section:
---Date:
---# OK Type of Error Corrections
1 2 3 4 5
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
15
8 9 10 11 12 13 14 15 16 17 18
# Represents the line number in the program
6 ةظوفحم عبطلا قوقح عيمج
–
ريوطتلل عاعشإ زكرم
16