You are on page 1of 6

C++ Programming 1

LOOPING for STATEMENT

I. OBJECTIVES

1. To familiarize and implement the for statement in C++ programming.


2. To differentiate the use of for statement from other looping structures
in programming applications.

II. DISCUSSION

The for loop

Many loops have these characteristics in common:


 initialization,
 a condition which evaluates either to FALSE or TRUE,
 an increment.

The for keyword marks the beginning of the code which will be repeated
according to the conditions supplied in the parenthesis following the for. The
general form of the statement is

for (initialization; condition; increment/decrement)


{
statements;
}

To make this clearer let's take the following while loop

count = 0; // initialization
while (count < 10) // condition
{
cout << count;
count++; //increment
}

and re-write it using a for loop:

for (count=0; count<10; count++)


{
cout << count;
}

 The initialization statement is carried out only once when the loop is first
entered.

Computer Fundamentals and Programming


C++ Programming 2

 The condition is tested before each run through the body of the loop. The
first test is immediately after initialization, so if the test fails the
statements in the body are not run - just like a while loop.
 The third expression, usually an increment or decrement to alter the test
condition variable, is executed after the loop body and before the next
test. This is the same as putting the increment or decrement at the end of
a while loop.

The for loop can be incremented or decremented by any amount, for


example the following are all valid for statements.

for (i = 10.0; i <= 1000; i = i * 2.5) // or i *= 2.5


{
statements;
}
for (k = 1.0; k > 0.001; k = k / 2.0) // or k /= 2.0
{
statements;
}

and its main function is to repeat statement while condition remains true,
like the while loop. But in addition, for statement provides places to specify an
initialization instruction and an increase/decrease instruction. So this loop is
specially designed to perform a repetitive action with a counter.

// countdown using a for loop 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,


#include <iostream.h> FIRE!
#include <conio.h>
int main ()
{
for (int n=10; n>0; n--)
{
cout << n << ", ";
}
cout << "\nFIRE!";
getch();
return 0;
}

Using the comma operator (,) we can specify more than one instruction in
any of the fields included in a for loop statement, like in initialization, for
example. The comma operator (,) is an instruction separator, it serves to
separate more than one instruction where only one instruction is generally
expected.

Computer Fundamentals and Programming


C++ Programming 3

For example, suppose that we wanted to initialize more than one variable in our
loop:

for ( n=0, i=100 ; n!=i ; n++, i-- )


{
cout<<”My statement here”;
}

This loop will execute 50 times if neither n nor i are modified within the
loop:

n starts with 0 and i with 100, the condition is (n!=i) (that n is not equal to i).
Because n is increased by one and i decreased by one, then the loop's condition
will become false after the 50th loop, when both n and i will be equal to 50.

Computer Fundamentals and Programming


C++ Programming 4

Laboratory Exercise 6
Looping for Statement

Name: Date:
Professor: Schedule:

Direction: Demonstrate the corresponding outputs for each of the succeeding


programs in the boxes below. Give your analysis for each of the
programs.

1.
Analysis:

_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________

2.
Analysis:

_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________

Computer Fundamentals and Programming


C++ Programming 5

Encode the following program and compile them to illustrate the output for
Laboratory Exercise 5: Looping for statement.

Program No.1(save as loop_ex5.cpp)

#include<iostream.h>
#include<conio.h>

int main()
{
clrscr();
int x, sum;
sum = 0;
for (x = 1; x <= 100; x++)
{
sum = sum + x;
}
cout<<”The sum of integers from 1 to 100 is “<<sum;
getch();
return 0;
}

Program No.2(save as loop_ex6.cpp)

#include<iostream.h>
#include<conio.h>

int main()
{
clrscr();
int c;
cout<<”Number\tSquare\tCube\n”;
for (c = 1; c <= 6; c++)
{
cout<<x<<”\t”
<<x * x<<”\t”
<<x * x * x<<endl;
}
getch();
return 0;
}

Computer Fundamentals and Programming


C++ Programming 6

SUPPLEMENTARY PROBLEM

Perform the following programs using C++ Language’s for statement.

1. Make a program that utilizes looping and tab escape \t, to print the following table of
values:

N N2 N3 N4
1 1 1 1
2 4 8 16
3 9 27 81
4 16 64 256
5 25 125 625

Computer Fundamentals and Programming

You might also like