0% found this document useful (0 votes)
12 views9 pages

Loops Lab

Uploaded by

falifarhan893
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

Loops Lab

Uploaded by

falifarhan893
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

BS (Computer Science) 2023

LAB 8:
Objectives:

Loops are programming elements that repeat a portion of code a set number of times
until the desired process is complete.
Repetitive tasks are common in programming, and loops are essential to save time and
minimize errors.

Theoretical Description:

The purpose of loops is to repeat the same, or similar, code a number of times.
Loops are among the most basic and powerful of programming concepts

Lab Task(s):

Use of loop statements in c++ programs

Programming Fundamentals (Lab Manual) Page 46 of 147


BS (Computer Science) 2023

LOOPS
There may be a situation, when you need to execute a block of code several numbers of
times. Loops causes a section of your program to be repeated a certain number of times. The
repetition continues while a condition is true a certain number of times. When the condition
becomes false, the loop ends and control passes to the statements following the loop.
There are three kinds of loops in C++:
for loop.
while loop.
do loop.
FOR LOOP
usually (although not
always) used when you know, before entering the loop, how many times you want to execute
the code.
How does it works?
The for statement controls the loop.
It consists of the keyword for, followed by parentheses that contain three expressions
separated by semicolons.
For(j=0; j<=15; j++)
These three expressions are the initialization expression, the test expression, and the
increment expression.
These three expressions usually (but not always) involve the same variable, which we
call the loop variable.
THE INITIALIZATION EXPRESSION
The initialization expression is executed only once, when the loop first starts. It gives the
loop variable an initial value.
THE TEST EXPRESSION
The test expression usually involves a relational operator.
It is evaluated each time through the loop, just before the body of the loop is executed.
It determines whether the loop will be executed again.
If the test expression is true, the loop is executed one more time.
loop ends, and control passes to the statements following the loop.
THE INCREMENT/DECREMENT EXPRESSION
The increment expression changes the value of the loop variable, often by
incrementing it.
It is always executed at the end of the loop, after the loop body has been executed.

Programming Fundamentals (Lab Manual) Page 47 of 147


BS (Computer Science) 2023

Task No. 01
Q: Write a program that display the counting from 1 to 10 using for loop.
#include<iostream>
using namespace std;
int main()
{
int j;
for(j=1;j<=10;j++)
{
cout<<j;
}
return 0;
}

Task No. 02
Q: Write a program that display the counting from 1 to 10 using for loop
and endl.
#include<iostream>
Programming Fundamentals (Lab Manual) Page 48 of 147
BS (Computer Science) 2023

using namespace std;


int main()
{
int j;
for(j=1;j<=10;j++)
{
cout<<j<<endl;
}
return 0;
}

WHILE LOOP
The while loop looks like a simplified version of the for loop. It contains a test expression but
no initialization or increment expressions.
Although there is no initialization expression, the loop variable must be initialized before the
loop begins.
The loop body must also contain some statement that changes the value of the loop variable;
otherwise the loop would never end.
Int i=0;
While(i<=15)
i++;

Programming Fundamentals (Lab Manual) Page 49 of 147


BS (Computer Science) 2023

Task No. 03
Q: Write a program that display the counting from 1 to 10 using while
loop.
#include<iostream>
using namespace std;
int main()
{
int j=0;
while(j<=10)
{
cout<<j;
j++;
}
return 0;
}

Task No. 04
Q: Write a program that display the counting from 1 to 10 using while loop and endl.
#include<iostream>
using namespace std;
int main()
{ int j=0;
while(j<=10)
{ cout<<j<<endl;
j++;
}
return 0;
}

Programming Fundamentals (Lab Manual) Page 50 of 147


BS (Computer Science) 2023

DO WHILE LOOP
In a while loop, the test expression is evaluated at the beginning of the loop. If the test
expression is false when the loop is entered, the loop
sometimes you want to guarantee that the loop body is executed at least once, no matter what
the initial state of the test expression. When this is the case you should use the do loop, which
places the test expression at the end of the loop.
The keyword do marks the beginning of the loop.
Then, as with the other loops, braces delimit the body of the loop.
Finally, a while statement provides the test expression and terminates the loop.
This while statement looks much like the one in a while loop, except for its position at
the end of the loop and the fact that it ends with a semicolon.
Task No. 05
Q: Write a program on division is perform on two values and also display the quotient
and remainder and ask from user if it
#include<iostream>
using namespace std;
int main()
{ char ch;
int dividend,divisor;
do
{ cout<<"Enter dividend: ";
cin>>dividend;
cout<<"Enter divisor: ";
cin>>divisor;
cout<<"Quotient is "<<dividend/divisor<<endl;
cout<<"Remainder is "<<dividend%divisor<<endl;
cout
<<"If you want to do it again press 'y'";
cin>>ch;
}
while(ch=='y');
cout<<endl;
return 0;
}
Programming Fundamentals (Lab Manual) Page 51 of 147
BS (Computer Science) 2023

NESTED LOOPS
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++
for ( init; condition; increment ) {
for ( init; condition; increment ) {
statement(s);
}
statement(s);
}
Task No. 07
Q: Write a program to print 10 stars horizontally using for loop.
#include<iostream>
using namespace std;
int main()
{ int j;
for(j=1;j<=10;j++)
{ cout<<"*"; }
return 0;
}

Task No. 08
Q: Write a program to print 10 stars vertically using for loop.
#include<iostream>
using namespace std;
int main()
{ int j;
for(j=1;j<=10;j++)
{ cout<<"*"<<endl; }
return 0;
}
Programming Fundamentals (Lab Manual) Page 52 of 147
BS (Computer Science) 2023

Task No. 09
Q. Write a program to create the following pattern using while loop.
*
**
***
****
*****
#include<iostream>
using namespace std;
int main()
{ int a,b;
a=1;
while(a<=5)
{
b=1;
while(b<=a)
{
cout<<"*";
b++;
}
cout<<endl;
a++;
}
return 0;
}

STUDENT TASK:
Programming Fundamentals (Lab Manual) Page 53 of 147
BS (Computer Science) 2023

1. Write a program in c++ to print the sum of odd numbers of first 10 natural numbers
by using the for loop.
2. Write the above program by using while loop.
3. Write the above program by using do while loop.
4. Write a program in c++ to calculate the product of the even numbers from 1 to 12by
using while loop.
5. Write a program in c++ to calculate (ab) without using any built-in-function.

Programming Fundamentals (Lab Manual) Page 54 of 147

You might also like