Lab Session 6

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Lab Session 06

Objective
Repetition Control Structure (for loop)

Theory
A program is usually not limited to a linear sequence of instructions. During its process it may bifurcate,
repeat code or take decisions. For that purpose, C++ provides control structures that serve to specify what
has to be done by our program, when and under which circumstances.
We have already discussed in previous Labs the two of the three types of Control Structure i.e. sequential
Control Structure and Selection Control Structure.
Another very powerful control structure is Repetition Control Structure in C++. Repetition statements allow
to repeat a block of code until a certain condition is true. Repetition statements are commonly referred as
loops and they can be implemented with the following statements
a. for
b. while
c. do while

In this lab session we shall discuss for loop whereas while & do while will be discussed in the next. Loops
are helpful when a certain piece of code is required to be executed in a repeated manner. This can save a
lot of precious time and laborious efforts.

The for() loop


A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a
specific number of times. The syntax of a for loop in C++ is

for(initialization; condition ; increment/decrement)


{
C++ statement(s);
}

The init step is executed first, and only once. This step allows you to declare and initialize any loop control
variables. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the
body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
After the body of the for loop executes, the flow of control jumps back up to the increment statement. This
statement allows you to update any loop control variables. The condition is now evaluated again. If it is
true, the loop executes and the process repeats itself (body of loop, then increment step, and then again
condition). After the condition becomes false, the for loop terminates.

Example 1:
#include<iostream>
using namespace std;
int main(void)
{
int num;
for(int num=0;num<=10;num++)
{
cout<<"\n num = "<<num;
}
return 0;
}
Example 1:
#include<iostream>
using namespace std;
int main(void)
{
int base,exponent,answer,counter;
cout<<"Enter a number(integer):";
cin>>base;
cout<<"Enter an exponent(integer):";
cin>>exponent;
answer=1;
for(counter=exponent;counter>0;counter=counter-1)
{
answer=answer*base;
}
cout<<"\n"<<base<<" raised to power "<<exponent<<" = "<<answer;
return 0;
}

Nested for() loop


Nested Loop is a loop in which one loop resides inside another loop where the inner loop gets executed first
satisfying all the set of conditions prevailed within the loop followed by an outer loop set of conditions.
Execution of statements within the loop flows in a way that the inner loop of the nested loop gets declared,
initialized and then incremented. Once all the condition within the inner loop gets satisfied and becomes
true it moves for the search of the outer loop. It is often called a “loop within a loop”.

for ( init; condition; increment )


{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
Example:
#include <iostream>
using namespace std;

int main()
{
int rows = 5;
int columns = 3;

for (int i = 1; i <= rows; ++i)


{
for (int j = 1; j <= columns; ++j)
{
cout << "* ";
}
cout << endl;
}

return 0;
}
Continue Statement
The continue statement breaks one iteration (in the loop), if a specified condition occurs, and continues
with the next iteration in the loop.
This example skips the value of 4:
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
continue;
}
cout << i << "\n";
}
Break Statement
You have already seen the break statement used in an earlier in switch Lab. It was used to "jump out" of a
switch statement.
The break statement can also be used to jump out of a loop.
This example jumps out of the loop when i is equal to 4:
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
cout << i << "\n";
}

Practice Programs:
1. Write a program to print number from 1 to 10
2. Write a program to calculate the sum of first 10 natural number.
3. Write a program to find the factorial value of any number entered through the keyboard.
4. Write a program to check given number is prime or not.
5. Write a program to print prime number between any given range.
6. Compute the natural logarithm of 2, by adding up to n terms in the series
1 - 1/2 + 1/3 - 1/4 + 1/5 -... 1/n
where n is a positive integer and input by user.
7. Write programs to check whether a number is perfect or not.
8. Write a program to compute sinx for given x. The user should supply x and a positive integer n.
We compute the sine of x using the series and the computation should use all terms in the series
up through the term involving xn
9.
10. sin x = x - x3/3! + x5/5! - x7/7! + x9/9!
Write programs to print following print patterns.

********** * * *
********** ** ** ***
********** *** *** *****
********** **** **** *******
***** ***** *********

You might also like