0% found this document useful (0 votes)
53 views5 pages

Lab 5

This document discusses different control structures in C++ including for, while, and do/while loops. It provides the syntax and examples of using each type of loop, including nested loops. The objectives are to learn how to use for, while, and do/while loops to repeatedly execute blocks of code based on initialization conditions, increment/decrement statements, and loop conditions.

Uploaded by

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

Lab 5

This document discusses different control structures in C++ including for, while, and do/while loops. It provides the syntax and examples of using each type of loop, including nested loops. The objectives are to learn how to use for, while, and do/while loops to repeatedly execute blocks of code based on initialization conditions, increment/decrement statements, and loop conditions.

Uploaded by

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

LAB 5 – C++ CONTROL STRUCTURES-II

Objectives
1. Using ‘for’ repetition structure.
2. Using ‘while’ repetition structure.
3. Using ‘do/while’ repetition structure.

C++ For Loop


Syntax
for (initialize; condition; increment/ decrement) {
// code block to be executed
}
Example:
for (int i = 0; i < 5; i++) {
cout << i << "\n";
}

C++ Nested For Loop


for (initialize; condition; increment/ decrement) {
for (initialize; condition; increment/ decrement) {
// code block to be executed
}

// code block to be executed


}
Example:
1. for (int i = 0; i < 2; i++) {
2. for (int j = 0; j < i; j++) {
3. Cout<<i<<”\t”<< j << "\n";
4. }
5. cout << i << "\n";
6. }
I j i<3 j<i Output
(Execute)
0 0 0<3 (true) 0<0 (false) Line 5
1 0 1<3 (true) 0<1 (true) Line3
1 1<1 (false) Line 5
2 0 2<3 (true) 0<2 (true) Line 3
1 1<2 (true) Line 3
2 2<2 (false) Line 5
3 - 3<3 (false) Exit loop

C++ While Loop


Loops can execute a block of code as long as a specified condition is
reached.

Syntax
while (condition) {
// code block to be executed
}
Example:
int i = 0;
while (i < 5) {
cout << i << "\n";
i++;
}

C++ Nested While Loop


1. int i = 0;
2. while (i < 3) {
3. int j = 0;
4. while (j < i) {
5. cout << i <<"\t"<<j<< "\n";
6. j++;
7. }
8. cout << i << "\n";
9. i++;
10. }
This will produce same result as described in nested for loop
I j i<3 j<i Output
(Execute)
0 0 0<3 (true) 0<0 (false) Line 8
1 0 1<3 (true) 0<1 (true) Line 5,6
1 1<1 (false) Line 8,9
2 0 2<3 (true) 0<2 (true) Line 5,6
1 1<2 (true) Line 5,6
2 2<2 (false) Line 8,9
3 - 3<3 (false) Exit loop
C++ Do/While Loop
The do/while loop is a variant of the while loop. This loop will execute the code
block once, before checking if the condition is true, then it will repeat the
loop as long as the condition is true.

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.

Task 5.1 ‘for’ Statement


Write a C++ program which takes an integer input from the user, prints all even integers
less than or equal to this number and greater than or equal to 0, calculates the sum of
these even and displays results on the screen. Your program should have the following
interface.
Enter a number : 7
Even numbers less than or equal to 7 are :
2 4 6

Sum of even numbers : 12

Solution:

Task 5.2 ‘while’ Statement


Repeat Task. 5.1 using while loop

Exercise 5.1.. ‘for’ Statement


Write a C++ program which takes an integer input from user in the variable named
‘num’, calculates the sum of all integers from 1 to ‘num’ and displays this sum on screen.
Your program should have the following interface.

Enter any value : 10


Sum of integers from 1 to 10 is : 55
Exercise # 5.2
Repeat Task 5.1 using ‘while’ and ‘do/while’ statements.

Exercise # 5.3 (Mileage)


Write a program to calculate factorial of number input by user using both for and while loops.
HINT : Factorial of 5 = 5 * 4* 3 *2 * 1 = 120

Web Resources

http://www.tutorialspoint.com/cplusplus/
http://www.learncpp.com/

You might also like