Professional Documents
Culture Documents
I. OBJECTIVES
II. DISCUSSION
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
count = 0; // initialization
while (count < 10) // condition
{
cout << count;
count++; //increment
}
The initialization statement is carried out only once when the loop is first
entered.
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.
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.
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.
For example, suppose that we wanted to initialize more than one variable in our
loop:
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.
Laboratory Exercise 6
Looping for Statement
Name: Date:
Professor: Schedule:
1.
Analysis:
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
2.
Analysis:
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
_________________________
Encode the following program and compile them to illustrate the output for
Laboratory Exercise 5: Looping for statement.
#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;
}
#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;
}
SUPPLEMENTARY PROBLEM
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