LEARNINGS OF THE WEEK
BY: Sharra Mae S. Tagaro IV- Rizal
BY: Sharra Mae S. Tagaro IV- Rizal
ITERATIVE STATEMENTS
Iterative statements (loops)- allow a set of instruction to be executed or performed several times until conditions are met.Types of Iterative Statements
☺The For statements
☺The While statements
☺The Do-While statements
☻The For statements
- 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 for loop continues to execute until the condition is True (1).
-Once False (0), program execution resumes on the statement following the for loop.
Note:
*Never place a semicolon right after the for header.
*Never change the value of the for loop’s counter in side the body of the loop. This will affect the result of the program.
*The increment part of the for loop is execute after the first iteration of the loop.
☻The While statement
- 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.
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.
- 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”.
- 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.

No comments:
Post a Comment