0% found this document useful (0 votes)
7 views13 pages

Loops

The document explains three types of loops in programming: while loops, do-while loops, and for loops. Each loop is described with its syntax and functionality, highlighting how they repeat statements based on certain conditions. Examples in C++ demonstrate the usage of each loop type for countdown and input scenarios.

Uploaded by

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

Loops

The document explains three types of loops in programming: while loops, do-while loops, and for loops. Each loop is described with its syntax and functionality, highlighting how they repeat statements based on certain conditions. Examples in C++ demonstrate the usage of each loop type for countdown and input scenarios.

Uploaded by

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

Loo

z
z

Loops are used to repeat a


statement a certain number of
times or while a condition is
fulfilled.
z

The while loop


Its format is:
while (condition)
statement
z

and its functionality is simply to repeat


statement while the condition set in
expression is true.
#include
z <iostream>
using namespace std;
int main ()
{
int n;
cout << "Enter the starting number : ";
cin >> n;
while z(n>0)
{
cout << n << ", ";
--n;
}
cout << “HAPPY NEW YEAR!";
return 0;
}
z

The do - while loop


Its format is:
do statement while (condition);
Its functionality is exactly the same as
z

the while loop, except that condition in


the do-while loop is evaluated after
the execution of statement instead of
before, granting at least one execution
of statement even if condition is
never fulfilled.
z
include <iostream>
using namespace std;
int main ()
{
int n;
do {
cout << "Enter a number: ";
z

cin >> n;
}
while (n != 0);
return 0;
}
z

The for loop


Its format is:
for (initialization; condition;
increase) statement;
z

It’s main function is to repeat the


statement while the condition remains
true, like the while loop. But in
addition, the for loop provides specific
locations to contain an initialization
statement and an increase statement.
#include
z
<iostream>
using namespace std;
int main ()
{
for (int n=10; n>0; n--)
{
cout << n << ", ";
}
cout << “Happy New Year!";
return 0;

You might also like