Professional Documents
Culture Documents
While For DoWhile LOOP
While For DoWhile LOOP
Computer
Programming 2
Bhon-Bhon C.
Agcaoili
Computer Programming 2
Loop
Consider the
following problem.
Problem 1:
Write a program that will output the numbers
from 1 to 5. The output should look like:
1
2
3
4
5
Source Code Output
#include <iostream> 1
using namespace std; 2
3
int main() 4
{ 5
cout << "1 \n";
cout << "2 \n";
cout << "3 \n";
cout << "4 \n";
cout << "5 \n";
return 0;
}
• This solution solves the problem.
• However, what will happen if we
modify the problem such that we
would like to output the values from
1 to 100?
• 1,000?
• 1,000,000?
Before we do the correct solution, try to
understand the following program first:
Source Code Source Code
ctr=ctr+1;
cout << ctr << endl;
initialize ctr to 1
repeat the following while ctr is less than or
equal equivalent to 5
print the value of ctr
increment ctr by 1
A loop usually has the following components:
• initialization
• condition
• body of loop
• change of state
In the case of the algorithm above:
initialize ctr to 1 Initialization
repeat the following while ctr is Condition
less than or equal equivalent to
5
Body of Loop
print the value of ctr
Body of Loop
increment ctr by 1
Statement that changes the
content of variable
Types of Loop
•While Loop
•For Loop
•Do While Loop
While Loop
While Loop
The while statement continually executes a block of statements
while a particular condition is true.
Infinite Loop
Exercise
3. Consider again the original program. What will
happen if the programmer committed a typographical
error, such that instead of pressing the less than
symbol, the greater than symbol was pressed, i,e., the
condition becomes ctr>= 5 ?
Infinite Loop
Infinite Loop
while(ctr<=1)
{
printf("%.1f \n", ctr);
ctr=ctr+0.2;
}
return 0;
}
For Loop
For Loop
The for loop is simply a shorthand way of expressing a while
statement.
int main()
{
int ctr;
return 0;
}
To print numbers from 100 For Loop
int main()
{
int ctr;
do
{
statement(s);
change of state;
} while (expression);
Do While Loop vs While Loop
Do While Loop
#include <iostream>
To print the numbers from 1 using namespace std;
to100 using a do while loop.
int main()
{
int ctr;
ctr=1;
do
{
} while (ctr<=100);
return 0;
}
Do While Loop
#include <iostream>
To print the numbers 100 using namespace std;
down to 0:
int main()
{
int ctr;
ctr=100;
do
{
} while (ctr>=1);
return 0;
}
QUESTION:
#include <iostream>
int main()
{
int sum=0, num;
do
{
cout <<"Enter an integer(enter 0 to quit): ";
cin>>num;
sum=sum+num;
} while(num!=0);
int main()
{
int sum=0, num, ctr, ctr2, fact;
}
sum=sum+fact;
}
cout <<"\n\nSum of " << num << " factorial is " << sum;
return 0;
}
Nested Loop
i=1;
while(i<=10)
{
j=1;
while(j<=10)
{
cout << "\t" << i*j;
j++;
}
cout << "\n";
i++;
}
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i,j;
}while(resp=='Y' || resp=='y');
cout<<"Thank you!\n";
Continue statement
#include <iostream>
1
using namespace std; 2
int main()
{
int ctr;
if (ctr == 2)
{
//loop will now stop
break;
}
}
return 0;
}
Source Code Output
#include <iostream>
int main()
{
int ctr;
for ( ctr = 1 ; ctr <= 10 ; ctr++ )
{
cout<<"\n"<< ctr << "\t";
if ( ctr == 2 )
{
continue;
}
cout<<"ctr is not 2";
}
return 0;
}
Summary
• A loop is a control structure that allows a statement or a
group of statements to be executed several times.