Professional Documents
Culture Documents
Lab 5
Lab 5
Lab 5
Objectives
1. Using ‘for’ repetition structure.
2. Using ‘while’ repetition structure.
3. Using ‘do/while’ repetition structure.
Syntax
while (condition) {
// code block to be executed
}
Example:
int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}
Syntax
do {
// code block to be executed
}
while (condition);
Example
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 5);
Output if above program is
0
1
2
3
4
Example 2
int i = 0;
do {
cout << i << "\n";
i++;
}
while (i < 0);
Output if above program is
0
Note: Notice that in do while, condition will be checked after
executing code i.e. it will always run for the first time but
afterwards it depends on the validity of specified condition in while.
Solution:
Web Resources
http://www.tutorialspoint.com/cplusplus/
http://www.learncpp.com/