0% found this document useful (0 votes)
108 views3 pages

10 Repetition Structures - For Statement

This document discusses repetition (looping) control structures in C++, specifically covering for, while, and do-while loops. It provides examples of using for loops to print numbers in a range, print text a set number of times, and calculate a sum. Key aspects of for loops explained are the initialization, condition, and update statements. Exercises are provided to practice using for loops to print even/odd numbers, calculate sums, and find maximum/factorial of inserted values. While and do-while loops are also introduced.

Uploaded by

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

10 Repetition Structures - For Statement

This document discusses repetition (looping) control structures in C++, specifically covering for, while, and do-while loops. It provides examples of using for loops to print numbers in a range, print text a set number of times, and calculate a sum. Key aspects of for loops explained are the initialization, condition, and update statements. Exercises are provided to practice using for loops to print even/odd numbers, calculate sums, and find maximum/factorial of inserted values. While and do-while loops are also introduced.

Uploaded by

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

1

Repetition (looping) Control Structures


Why Is Repetition Needed?
o Can input, add, and average multiple numbers using a limited number of
variables
o For example, to add five numbers:
Declare a variable for each number, input the numbers and add the
variables together
cin>>x; cin>>y; cin>>z;
sum=(x+y+z)/3;
What if we need to find the sum of 1000 numbers?
Create a loop that reads a number into a variable and adds it to a variable
that contains the sum of the numbers
Repetition (looping) structures to be covered:
o for looping structure
o while looping structure
o dowhile looping structure

for Looping (Repetition) Structure


syntax:
for (initialization statement; loop condition;
update statement)
statement;
next statement;
initialization statement usually initializes a variable (called the for loop control
variable, or for indexed variable)
In C++, for is a reserved word
Update statement may increment (or decrement) the loop variable by any fixed
number

Example 1: Write C++ program to print the numbers from 1 to 10.


# include <iostream.h>
void main ()
{
for ( int i = 1 ; i <=10; i++)
cout <<i<<endl;
}
Exercise: print the numbers from 10 to 1
Example 2:
#include <iostream.h>
void main ()
{
for (int i = 1 ; i <=5; i++)
{
cout <<"Hello"<<endl;
cout<<"*"<<endl;
}
}
Example 3:
#include <iostream.h>
void main ()
{
for (int i = 1 ; i <=5; i++)
cout <<"Hello"<<endl;
cout<<"*"<<endl;
}
Exercise: print the even numbers between 1 and 20.
Exercise: print the odd numbers between 1 and 20.
Exercise: write a C++ program to calculate and print the sum of numbers 1 to 10.
Exercise: write a C++ program to insert 10 values and calculate their sum.
Exercise: write a C++ program to insert 10 values and print out the odd values only.
Example 4: what is the output of the following code?
#include <iostream.h>
void main()
{
int i;
for (i=0;i<5;i++);
cout<<"*"<<endl;
}

*
Example 5: what is the output of the following code?
2

#include <iostream.h>
void main()
{
for ( ; ; )
cout << "Hello" << endl;
}
Infinite loop
Try:
for (int i=1; ; )
cout << "Hello" << endl;
Try:
for (int i=1; ; i++ )
cout << "Hello" << endl;
Example 5: what is the output of the following code?
#include <iostream.h>
void main()
{
for (int x=1, n=10; x<=5&&n>=5 ; x++, n--) cout<<x<<" "<<n<<endl;
}
Exercise: write a C++ program to insert n values and print out the maximum.
Exercise: write a C++ program to insert an integer values and print out its factorial
Exercise: write a C++ program to calculate S where
S=1+22+32+42++102
S=1/n+3/n+5/n++9/n

You might also like