0% found this document useful (0 votes)
13 views16 pages

Iterative Statement

Iterative statement on c++

Uploaded by

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

Iterative Statement

Iterative statement on c++

Uploaded by

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

ITERATIVE STATEMENTS

Iterative Statement

Introduction to Iterative Statements

An iterative statement, or loop, is a control structure used to repeat a set of instructions until
a specific condition is met. Iterative statements are essential in programming because they
allow us to automate repetitive tasks efficiently.

The three main types of loops are:


1. For Loop
2. While Loop
3. Do-While Loop

Each type has its unique use case, which we will explore with examples.
Iterative Statement

1. The For Loop

A for loop is ideal when the number of iterations is known beforehand. It consists of three parts:

Initialization: Sets the starting point of the loop.


Condition: Checks if the loop should continue running.
Update: Updates the loop variable after each iteration.

Syntax:
for (initialization; condition; update) {
// Code to execute repeatedly
}
Iterative Statement

Example 1: Print numbers from 1 to 10


for (int i = 1; i <= 10; i++) {
cout<<i<<endl;
}

Explanation

• int i = 1: The loop starts with i set to 1.


• i <= 10: The loop continues as long as i is less than or equal to 10.
• i++: After each iteration, i is incremented by 1.
Iterative Statement

2. The While Loop

A while loop is used when the number of iterations is not known and depends
on a condition that must remain true. It checks the condition before executing
the loop body

Syntax:
while (condition) {
// Code to execute repeatedly
}
Iterative Statement

Example 2: Keep doubling a number until it exceeds 100

int number = 1;
while (number <= 100) {
cout<<number<<“\n”;
number = number * 2; // Double the number
}

Explanation:
• The loop checks number <= 100 before each iteration.
• If true, the body executes, doubling the value of number.
• Once number > 100, the loop exits.
Iterative Statement

3. The Do-While Loop

A do-while loop is similar to the while loop but with one key difference: it guarantees that the
loop body executes at least once, regardless of the condition.

Syntax:
do {
// Code to execute repeatedly
} while (condition);
Iterative Statement

Example 3: Accept user input until a positive number is


entered

int number;
do {
cout<<Enter a positive number:;
cin>>number;
} while (number <= 0);
Iterative Statement

Explanation:

• The body executes first, asking the user for input.


• The condition number <= 0 is checked afterward.
• If the input is non-positive, the loop repeats.
Iterative Statement

COMPARISON OF LOOPS

FEATURE FOR LOOP WHILE LOOP DO-WHILE LOOP

Condition Check At the start At the start At the end

Guaranteed one
Use Case Known iterations Unknown iterations
execution

Code Execution 0 or more times 0 or more times At least once


Iterative Statement

Example: Summing Numbers

Task: Write a program that calculates the sum of numbers from 1 to n.

For Loop Implementation:

int n = 10, sum = 0;


for (int i = 1; i <= n; i++) {
sum = sum + i;
}
cout<<sum<<endl;
Iterative Statement

Example: Summing Numbers

Task: Write a program that calculates the sum of numbers from 1 to n.

While Loop Implementation:

int n = 10, sum = 0, i = 1;


while (i <= n) {
sum = sum + i;
i++;
}
cout<<sum<<endl;
Iterative Statement

Example: Summing Numbers

Task: Write a program that calculates the sum of numbers from 1 to n.

Do-While Loop Implementation:

int n = 10, sum = 0, i = 1;


do {
sum += i;
i++;
} while (i <= n);
cout<<sum<<endl;
Iterative Statement

Tips for Choosing the Right Loop

• Use a for loop when you know how many times the loop should run.
• Use a while loop when the end condition depends on dynamic
inputs or changes during runtime.
• Use a do-while loop when you want the body to execute at least
once, regardless of the condition.
Iterative Statement

Interactive Exercise

1. Write a program using a for loop to print all even numbers between 1 and
20.
2. Modify the program to use a while loop instead.
3. Use a do-while loop to validate user input for a number in the range 1-50.
Thank You

You might also like