0% found this document useful (0 votes)
25 views23 pages

Iterative Statements

Uploaded by

AjithaTeja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views23 pages

Iterative Statements

Uploaded by

AjithaTeja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

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

Execution Executes a specific number ofExecutes based on a condition that


Criteria times. can change during execution.

Typically uses a loop variable to


keep track of the number of Does not necessarily use a loop
Loop Variable iterations. variable.
Iterating over arrays or Handling user input, waiting for
Common Use performing tasks a fixed events, or performing actions until a
Cases number of times. certain condition is met.

while loops and do


for loops in many programming while loops used
Examples languages. with input or eventdriven conditions.
Key Differences:
•Control: Count-controlled loops are executed a predetermined
number of times, while event-controlled loops continue until a
specific condition is no longer met.
•Flexibility: Event-controlled loops are more flexible as they can
adapt to changing conditions during execution.
•Efficiency: Count-controlled loops can be more efficient in some
cases, as they avoid unnecessary iterations.
•Usage: Count-controlled loops are often used for repetitive tasks
with a known number of repetitions, while event-controlled loops
are common in scenarios where the loop termination depends on
external factors or user input.
Based on When the Condition is
Evaluated:
• Entry Controlled Loops:
• In these loops, the condition is checked before entering the
loop body.
• If the condition is false at the start, the loop body will not be
executed even once.
• Exit Controlled Loops:
• In these loops, the condition is checked after executing the
loop body.
• The loop body is always executed at least once, regardless of
the condition.
Feature Entry-Controlled Loop Exit-Controlled Loop

Condition is checked beforeCondition is checked after


Condition Check
the loop body executes. the loop body executes.

Loop body may not execute


Loop body always executes
Initial Execution at all if the condition is
at least once.
initially false.

Iterating over a sequence orHandling situations where


performing actions based onthe loop body must execute
Common Use Cases
a condition before entering
at least once, regardless of
the loop. the initial condition.

for loops and while loops in


Do-while loops in many
Examples most programming
programming languages.
languages.
Key Differences:
• Condition Check Timing: Entry-controlled loops check
the condition before each iteration, while exit-controlled
loops check the condition after each iteration.
• Initial Execution: Entry-controlled loops may not
execute at all if the condition is initially false, while exit-
controlled loops always execute at least once.
• Usage: Entry-controlled loops are often used when the
loop body should only execute if a certain condition is
met, while exit-controlled loops are useful when the loop
body must execute at least once, even if the condition is
initially false.
Iterative Statements in C
C provides three types of iterative statements:
1. for loop
2. while loop
3. do-while loop
While loop
Syntax • Condition: Checked
while (condition) { before each iteration. If
true, the loop continues; if
// code to be executed false, the loop terminates.
} • Initialization must be done
before the body
• Updating shall be done as
a part of the body
Example
#include<stdio.h>
int main(){
int i = 0; // Initialization
while (i < 5) { // Condition
printf("Iteration %d\n", i);
i++; // Updating
}
return 0;
}
for Loop
• Syntax • Initialization: Executed
once before the loop
starts.
for (initialization; condition;
updation) { • Condition: Checked
before each iteration. If
// code to be executed true, the loop continues; if
} false, the loop terminates.
• Updation: Executed after
each iteration.
Example
#include<stdio.h>
int main(){
for(int i = 0; i < 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
• Condition: Checked after
do-while loop each iteration. If true, the loop
continues; if false, the loop
terminates.
• Syntax:
• Initialization: It's common to
do { initialize variables used within
// code to be the loop before the loop starts
executed • Updating variables in a do-
while loop is typically done
} while within the loop body. This
(condition); ensures that the condition is
evaluated with the updated
values after each iteration.
Example
#include<stdio.h>
int main(){
int i = 0; // Initialization
do {
printf("Iteration %d\n", i);
i++; // Updating
} while (i < 5); // Condition
return 0;
}
Feature for Loop while Loop do-while Loop

Initializes a loop Initializes a loop Initializes a loop


Initialization variable before the variable outside thevariable outside the
loop starts. loop. loop.
Checks the condition Checks the condition
Checks the condition
Condition Check before each before each
after each iteration.
iteration. iteration.
Updates the loop Updates the loop Updates the loop
Updation variable after each variable within the variable within the
iteration. loop body. loop body.
May not execute at May not execute at
Always executes at
Initial Execution all if the condition isall if the condition is
least once.
initially false. initially false.
Performing actions
Iterating over a
Iterating until a at least once, even if
Common Use Cases known number of
condition is met. the condition is
elements.
initially false.
Write a C program to print the average of
numbers entered by the user.
• Analysis
1. We keep reading the numbers
2. While reading the numbers
2.A If the entered number is positive or zero:
2.A.1 Accumulate the read numbers to sum.
2.A.2 Increase the count by 1
2.B If the entered number is negative:
2.B.1 Exit the loop.

• The best strategy for this problem is to run an infinite loop


• And breaking from the infinite loop when a negative number is
encountered.
Infinite Loop Construct
for while do-while
for( ; ; ){ While(1){ do{
//Body //Body //Body
} } }while(1);

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

You might also like