You are on page 1of 3

Experiment # 11

OBJECTIVE
To experiment with iterative structures in C++

a) To be familiar with for loop.


b) To be familiar with while loop.
c) To be familiar with do- while loop.

Theory

(a) The for loop

The for loop executes the section of a code a fixed number of times. It’s usually used when you
know, before entering the loop, how many times you want to execute the code.

The for loop contains four major parts

1. The value at which the loop start (initial expression).


2. The condition at which the loop is to continue (conditional expression).
3. The changes that are to take place for each loop (increment expression).
4. The loop instructions / body of loop.

These parts are put together in the for loop as

for ( initial-expression; conditional-expression; increment-expression)

loop instructions;

The initial expression is executed only once, when the loops first starts. The test expression
usually involves a logical operator. It is evaluated each time through the loop, just before the
body of the loop executed. The increment expression changes the value of the loop variable, it
always executed at the end of the loop after the loop body has been executed.

You can execute more than one statements in the body of the loop, by placing the statements in
the brackets.

Example:
#include<iostream>
main()
{
int a;
for(a=1;a<=10;a++)
{
cout<<a;
}} Output: 1 2 3 4 5 6 7 8 9 10
(b) The while loop

The for loop does something a fixed number of times. What happens if you don’t know how
many times you want to do something, in this case the while lop is used.

The structure of the while loop is

While (expression)
Statement(s);

The statement may be a single statement or compound statement (enclosed in the braces {}). The
statement is executed zero or more times until the expression becomes FALSE. In the operation
of while loop the expression is first evaluated, if this evaluation is FALSE (0), then statement is
never executed. And control passed from the while statement to the rest of the program. If the
evaluation is TRUE (non zero), then statement is executed and the process is repeated again.

Example
#include<iostream>
main()
{
int a=1;
while(a<=10)
{
cout<<a;
a++;
}
} Output: 1 2 3 4 5 6 7 8 9 10

(c) The do-while loop

The do-while loop is similar to the while loop, the difference being that the test condition is
evaluated after the loop is executed, recall that the while loop tests the condition before the loop
is executed. Therefore, do…while loop statement(s) will be executed at least once in all
conditions.

The structure of the do-while loop is

do
{
Statement;
while(expression);
}

Where statement may be a single or a compound statement, statement is executed one or more
times until expression becomes FALSE ( a value of 0), if the expression becomes FALSE, the do
statement terminates, and control passes to the next statement in the program. Otherwise if the
expression is TRUE, statement is repeated, and the process stars over again.

The main difference between while and do-while loop is that do-while loop is execute at least
once, even the expression is FALSE.

Example
#include<iostream>
main()
{
int a=1;
do
{
cout<<a;
a++;
}
while(a<=10);
} Output: 1 2 3 4 5 6 7 8 9 10

Exercise:

Q1. With the help of for, while and do- while loop generate the following series
a) Natural numbers from 1 to 100
b) Even numbers between 1 and 40.
c) Odd numbers between 1 and 40.

Q2. Write a program using for loop which inputs an integer and prints its factorial?

Q3. Write a program that will calculates the sum of first 100 natural numbers?

Q4. Write a program that will generate the following output.


1
12
123
1234
12345

Q5.Write a program that counts blanks, digits, uppercase, lowercase letters, new lines & other
characters from a text file, or data entered through keyboard & displays it accordingly. Use
character ‘O’ to terminate the input.

You might also like