Lab # 7 – Programming Fundamentals (CPE-121)
Lab Manual # 07
Title: Implementation of For Loop and Do While Loop
CLO: CLO-1
Student Name: [Link] saghar Class Roll:2019-CPE-27
Subject Teacher : Engr. Muhammad Baqer
Lab Engineer : Engr. Abdul Rehman
Lab Performed In : Networking Lab (CPED)
LAB POINTS SCORE
Lab Performance [10] 0 1 2 3 4 5
Lab Participation
Lab Activity Completion on the Same Day
Lab Submission[10] 0 1 2 3 4 5
Completeness & Correctness
Required Conclusion & Results
No of Checks
SUB TOTAL
TOTAL SCORE
______________________
Course Instructor / Lab Engineer
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
There may be a situation, when you need to execute a block of code several number of times.
In general statements are executed sequentially: The first statement in a function is executed
first, followed by the second, and so on. Programming languages provide various control
structures that allow for more complicated execution paths. A loop statement allows us to
execute a statement or group of statements multiple times.
C++ for loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs
to execute a specific number of times.
Syntax:
The syntax of a for loop in C++ is:
for ( init; condition; increment )
{
statement(s);
}
Here is the flow of control in a for loop:
The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here, as
long as a semicolon appears.
Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is
false, the body of the loop does not execute and flow of control jumps to the next
statement just after the for loop.
After the body of the for loop executes, the flow of control jumps back up to
the increment statement. This statement allows you to update any loop control
variables. This statement can be left blank, as long as a semicolon appears after the
condition.
The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
condition becomes false, the for loop terminates.
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
Flow Diagram:
Example:
#include <iostream>
using namespace std;
int main ()
{
// for loop execution
for( int a = 20; a >=10; a--)
{
cout << "value of a: " << a << endl;
}
return 0;
}
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
C++ do...while loop
Unlike for and while loops, which test the loop condition at the top of the loop,
the do...while loop checks its condition at the bottom of the loop. A do...while loop is similar to
a while loop, except that a do...while loop is guaranteed to execute at least one time.
Syntax:
The syntax of a do...while loop in C++ is:
do
{
statement(s);
}while( condition );
Notice that the conditional expression appears at the end of the loop, so the statement(s) in
the loop execute once before the condition is tested. If the condition is true, the flow of control
jumps back up to do, and the statement(s) in the loop execute again. This process repeats until
the given condition becomes false.
Flow Diagram:
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
Example:
#include <iostream>
using namespace std;
int main ()
{
int a = 10;
do
{
cout << "value of a: " << a << endl;
a = a + 1;
}
while( a < 20 );
System("pause");
return 0;
}
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
P-1 Write a C++ program to add numbers entered by user until user enters 0.
Your Code:
#include<iostream>
using namespace std;
int main()
{
int a,b,sum;
cout<<"Enter first number: ";
cin>>a;
while(a!=0)
{
cout<<"Enter 2nd Number: ";
cin>>b;
sum=a+b;
cout<<"SUM IS: "<<sum<<endl;
cout<<"Enter first number: ";
cin>>a;
}
return 0;
}
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
Paste your output here
P-2 Write a program that initialize the loop control variable with negative integer value and
outputs the next ten values that are less than the loop control variable values.
Your Code:
#include<iostream>
using namespace std;
int main()
{
int i;
for(i=-10;i>=-20;)
{
cout<<i<<endl;
i=i-1;
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
}
return 0;
}
Paste your output here
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
P-3 Write a program that will print the table of user entered number using for loop
Your Code:
#include<iostream>
using namespace std;
int main()
{
int a,b,c,i;
cout<<"Enter table number ";
cin>>a;
cout<<"Enter starting number ";
cin>>b;
cout<<"Enter span number ";
cin>>c;
for(i=b;i<=c;++i)
{
cout<<a<<" * "<<i<<" = "<<a*i<<endl;
}
return 0;
}
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
Paste your output here
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
P-4 Write a program that count down from 10 to zero and the counter aborted when the value
of counter become 3 display the message using for loop
10 ,9 ,8 ,7 ,6 ,5 ,4 ,3 counter aborted.
Your Code:
#include<iostream>
using namespace std;
int main()
{
int i=10;
cout<<"Countdown Starts: "<<endl;
while (i!=2)
{
cout<<i<<endl;
i--;
}
cout<<"Counter Aborted";
return 0;
}
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
Paste your output here
P-5 Write a program that will print the following patterns
Your Code:
#include<iostream>
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
using namespace std;
int main()
{
int i,j,k,l;
cout<<"Pattern 1\n"<<endl;
for(i=0;i<=5;i++)
{
for(j=0;j<=i;j++)
{cout<<j<<" ";}
cout<<endl;
}
cout<<endl<<"Pattern 2\n"<<endl;
for (k=5;k>=1;k--)
{
for(l=1;l<=k;l++)
cout<<l<<" ";
cout<<endl;
}
return 0;
}
Paste your output here
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 7 – Programming Fundamentals (CPE-121)
Comments:
In this manual I learn how to draw different patterns using loops and some programs using
loops.
Version 1.0 Department of Computer Engineering UCE&T BZU Multan