Lab 5

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 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