0% found this document useful (0 votes)
3 views6 pages

Loops

The lesson plan covers iterative statements, specifically the for loop, while loop, and do-while loop, including their syntax and examples in C++. It provides practical exercises such as printing 'hello world' n times, numbers from 1 to 100, even numbers, and arithmetic and geometric progressions. The document serves as a comprehensive guide for teaching loops in programming.

Uploaded by

dopana9776
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)
3 views6 pages

Loops

The lesson plan covers iterative statements, specifically the for loop, while loop, and do-while loop, including their syntax and examples in C++. It provides practical exercises such as printing 'hello world' n times, numbers from 1 to 100, even numbers, and arithmetic and geometric progressions. The document serves as a comprehensive guide for teaching loops in programming.

Uploaded by

dopana9776
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/ 6

Lesson Plan

Loops
Topics to be covered:

Introduction to Iterative statements/Loop

The for loo

The while loo

The do-while loop

Introduction to Iterative statements/Loops

The simple dictionary meaning of loop is a structure, series, or process, the end of which is connected to the

beginning. We have the following types of iterative statements -

The while loo

The for loo

The do-while loop

The For Loop

The advantage of a for loop is we know exactly how many times the loop will execute even before the actual

loop starts executing.

Syntax:

for (init-statement; condition; final-expression)

statement

// logic

Init-statement: This statement is used to initialize or assign a starting value to a variable which may be altered

over the course of the loop (we will see this while solving examples). It is used/referred only once at the start of

the loop.

Condition: This condition serves as a loop control statement. The loop block is executed until the condition

evaluates to true.

Final-expression: It is evaluated after each iteration of the loop. It is generally to update the values of the loop

variables.

Example:

for(int i = 1; i < 6; i++ ){

co ut << i << " ";

Output: 1 2 3 4 5

Java
C++ &+ DSA
DSA
The while loop

A while loop is a loop that runs through its body, known as a while statement, as long as a predetermined
condition is evaluated as true.

Syntax:

while (condition)

statement;

Example:

int i = 1;

while (i <= 5)

cout << i << “ “;

i = i + 1;

Output: 1 2 3 4 5

The do-while loop

Do-while loop tests the condition at the end of each execution for the next iteration. In other words, the loop is
executed at least once before the condition is checked. Rest everything is the same as while loop.

Syntax:

do

statement;

} while (condition);

Example:

int idx = 1;

do

cout << idx << “ “;

idx++;

} while (idx <= 5);

Output: 1 2 3 4 5

Q1. Print hello world ‘n’ times. Take ‘n’ as input from user
Solution:

#include<iostream>

Using namespace std;

Java
C++ &+ DSA
DSA
int main()

int n;

cin>>n;

for(int i=1;i<=n;i++){

cout<<”hello world”<<endl;

Q2. Print numbers from 1 to 100.


Solution:

#include<iostream>

Using namespace std;

int main()

for(int i=1;i<=100;i++){

cout<<i<<endl;

Q3. Print all even numbers from 1 to 100.


Solution:

#include<iostream>

Using namespace std;

int main()

for(int i=2;i<=100;i+=2){

cout<<i<<endl;

Q4. Print the table of 19.


Solution:

#include<iostream>

Using namespace std;

int main()

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

cout<<19*i<<endl;

Java
C++ &+ DSA
DSA
Q5. Display this AP - 1,3,5,7,9.. upto ‘n’ terms.

#include<iostream>

Using namespace std;

int main()

int n;

cin>>n;

for(int i=1;i<=n;i+=2){

cout<<i<<endl;

Q6. Display this GP - 1,2,4,8,16,32,.. upto ‘n’ terms.

Solution:

#include<iostream>

Using namespace std;

int main()

int n;

cin>>n;

for(int i=1;i<=n;i*=2){

cout<<i<<endl;

Q7. Display this AP - 100,97,94,..upto all terms which are positive.

#include<iostream>

Using namespace std;

int main()

for(int i=100;i>0;i-=3){

cout<<i<<endl;

Java
C++ &+ DSA
DSA
THANK

YOU !

You might also like