Iterative Statements
Iterative Statements
Definition
• Iterative control statements are programming
constructs that allow a block of code to be executed
repeatedly until a certain condition is met.
• This allows for efficient and automated execution of
repetitive tasks, making programming more powerful
and flexible.
Purpose
• Iterative statements can significantly improve the
efficiency of your code by avoiding redundant manual
repetition.
• Iterative statements are essential for tasks that involve
repeating a set of actions multiple times, such as:
• Processing elements in an array or list
• Performing calculations until a desired result is achieved
• Implementing algorithms that require repeated steps
• They are often used to solve problems that involve
iterative processes, such as searching, sorting, and
optimization.
Components of Loops
• Initialization: Sets the starting point of the loop
control variable.
• Condition: Tests whether the loop should continue.
• Update: Modifies the loop control variable to eventually
break the loop.
• Body of the Loop: Contains the actions to be repeated
in each iteration.
Types of loops
1. Based on how the condition is evaluated:
• Count Controlled loops and Event Controlled loops
2. Based on when the condition is evaluated:
• Entry controlled and Exit controlled loops
Based on How the Condition is
Evaluated:
• Count Controlled Loops:
• These loops are controlled by a counter, which means you
know in advance how many times the loop will execute.
• The number of iterations is fixed or predetermined.
• Event Controlled Loops:
• These loops are controlled by an event or condition rather
than a fixed number of iterations.
• The number of iterations is not known beforehand; the loop
runs until a specific condition becomes true or false.
Feature CountControlled
- Loop EventControlled
- Loop
No initialization
No condition
No updating
Are required
However, we generally add conditional break statements to come out of the loop.
We must ensure that no loop runs indefinitely unless the problem demand them to
Variable Description
S.N Identifier Type Purpose
o
1 n Integer To read inputs
2 Sum Integer To accumulate the inputs
3 Avg Float To store average
4 C Integer To count the number of
non-negative inputs
Implementation
• DIY