Saturday, November 8, 2008

first learnings of the week

This week we tackled about Iterative statements (loops). What is iterative statement?


Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met. It can be predefined as in the loop, or open ended as in while and do-while.

Types of Iterative Statements

1. The For statements

2. The While statements

3. The Do-While statements



♥ The For statements ♥

The For statement or for loop is considered as a predefined loop because the number or times it iterates to perform its body is predetermined in the loop’s definition.
The For loop contains a counter whose values determine the number of times the loop iterates. The iteration stops upon reaching the number of times specified in the loop.
The general form of the for statement is:

for (initialization; condition; increment)
{
statement_sequence;
}

Where:
for is a reserve word in C
Initialization is an assignment statement that is used to set the loop’s counter.
Condition is a relational boolean expression that determines when the loop will exit.
Increment defines how the loop’s counter will change each time the loop is separated.
Statement sequence may either be a single C statement or a block of C statements that make up the loop body.
▼ Example:
Write a program that will print the numbers 1 to 10 using a for statement.

#include
int x;
main()
{
for (x=1; x<=10; x++)
printf (“%d\n”,x);
getch();
}
sample output:
1
2
3
4
5
6
7
8
9
10
♥ The While statement ♥
The while statement or while loop is an open-ended or event-controlled loop.
The while loop iterates while the condition is TRUE (1).
When it becomes FALSE (0), the program control passes to the line after the loop code.
The general form of the while statement is:

while (condition)
{
statement_sequence;
}
▼ Example:
Write a program that will print the numbers 1 to 10 using while statement.
#include
x;
main()
{
x=1;
while (x<=10)
{ printf (“%d\n”,x);
x++;
}
getch();
}
output:
1
2
3
4
5
6
7
8
9
10

♥ The Do-While statement ♥
The second type of open-ended or event-controlled loop is the do-while statement or do-while loop.
The general form of the do-while statement is:
do
{
statement_sequence;
} while (condition);

▼ example:
Write a program that will get the average of all integers from 1 to 10 using do-while loop.

#include
int x, sum;
float average;
main()
{
sum = 0;
x++;
do
{
sum = sum + x;
x++;
}while (x<=10) average = sum / 10.00;
printf (“the computed average is %.2f\n”, average);
getch();
}
Output: The computed average is 5.50.
This is our new lesson in our TLE subject. . .

Iterative statements(Loops)...what is it???

Iterative statements(Loops)...

what is it???...

Iterative statements(Loops)

-allow a set of instruction to be executed or performed several until condition are met. It can be predefined as in the loop, or open ended as in while and do-while.

-a type of control structure. It allows the repetition of a block of statements according to a condition.


Types of Iterative Statements:

I. The For statements

II. The While statements

III. The Do-While statements


The For statements

-The For statement or for loop is considered as a predefined loop because the number or times it iterates to perform its body is predetermined in the loop’s definition.

-The For loop contains a counter whose values determine the number of times the loop iterates. The iteration stops upon reaching the number of times specified in the loop.

The general form of the for statement is:

for (initialization; condition; increment)

{

statement_sequence;

}

Where:

-for is a reserve word in C

-Initialization is an assignment statement that is used to set the loop’s counter.

-Condition is a relational boolean expression that determines when the loop will exit.

-Increment defines how the loop’s counter will change each time the loop is separated.

-Statement sequence may either be a single C statement or a block of C statements that make up the loop body.


The While statement
-
The while statement or while loop is an open-ended or event-controlled loop.

-The while loop iterates while the condition is TRUE (1).

-When it becomes FALSE (0), the program control passes to the line after the loop code.


The general form of the while statement is:

while (condition)

{

statement_sequence;

}

where:

-While is a reserved word in C

-Condition is a relational expression that determines when the loop will exit.

-Statement_sequence may either be a single C statement or a block of C statements that make up the loop body.


The Do-While statement

-The second type of open-ended or event-controlled loop is the do-while statement or do-while loop.

The general form of the do-while statement is:

do

{

statement_sequence;

} while (condition);


Where:

-While and do are reserved words in C Condition is a relational expression that determines when the loop will exit.

-Statement_sequence may either be a single C statement or a block C statements that make up the loop body.

-Do-While is a variation of the while statement which checks the condition at the bottom / end of the loop.

-This means that a do-while loop “always executes at least once”.

-In the do-while loop, when the condition evaluates to TRUE (1), the loop body will be executed, but when FALSE (0), program control proceeds to the next instruction after the do-while loop.