Professional Documents
Culture Documents
>
< Future Academy >
For example:
If we wish to read 100 numbers and compute the
average.
An iteration=1 repetition
General forms of Repetition
Statements
Expression 1 initialization: (k=1)
We need : Expression 2 Continuation condition: (k<=10)
Expression 3 increment : (k=k+1)
#include <iostream>
using namespace std;
int main()
{
int i;
for(i = 0; i < 5; i++) {
cout << " i = "<< i <<endl;
cout << "After Loop, i = " << i
<<" ";
}
return 0;
}
Repetition Statements
(While vs. Do-While Loops)
Notes About Expressions
• Off-by-One Error:
If you wrote counter < 10 instead counter <= 10, then the loop will be
executed only 9 times.
• If the loop-continuation is
initially false:
Expression 2: C++ assumes that the condition is true >> infinite loop
Int count ;
count = 2;
count-- ;
cout<<"\n" <<“Done” ;
Output:
Trace this code segment
2
Int count ;
count = 2;
count-- ;
cout<<"\n" <<“Done” ;
Output:
Trace this code segment
2
Int count ;
count = 2;
count-- ;
cout<<"\n" <<“Done” ;
Output:
Trace this code segment
2
Int count ;
count = 2;
count-- ;
}
2
cout<<"\n" <<“Done” ;
Output:
Trace this code segment
2
Int count ;
1
count = 2;
count-- ;
}
2
cout<<"\n" <<“Done” ;
Output:
Trace this code segment
2
Int count ;
1
count = 2;
count-- ;
}
2
cout<<"\n" <<“Done” ;
Output:
Trace this code segment
2
Int count ;
1
count = 2;
count-- ;
}
2
cout<<"\n" <<“Done” ; 1
Output:
Trace this code segment
2
Int count ;
1
count = 2;
0
while (count > 0)
count-- ;
}
2
cout<<"\n" <<“Done” ; 1
Output:
Trace this code segment
2
Int count ;
1
count = 2;
0
while (count > 0) False!
{
count-- ;
}
2
cout<<"\n" <<“Done” ; 1
Output:
Trace this code segment
2
Int count ;
1
count = 2;
0
while (count > 0)
count-- ;
}
2
cout<<"\n" <<“Done” ; 1
Done
Output:
Calculate the sum of numbers from 1 to 2:
{
int sum = 0;
int i;
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
cout<<“The sum is :”<<sum;
return 0;
} Note: i++ is the same as
i = i + 1, and as i += 1.
sum i
0
{
int sum = 0;
int i;
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
Output
cout<<“The sum is :”<<sum;
return 0;
}
sum i
0 1
{
int sum = 0;
int i;
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
Output
cout<<“The sum is :”<<sum;
return 0;
}
sum i
0 1
{
int sum = 0;
int i; True
0 1
{
int sum = 0; 1
int i; True!
0 1
{
int sum = 0; 1 2
int i;
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
Output
cout<<“The sum is :”<<sum;
return 0;
}
sum i
0 1
{
int sum = 0; 1 2
int i; True!
0 1
{
int sum = 0; 1 2
int i; True!
3
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
Output
cout<<“The sum is :”<<sum;
return 0;
}
sum i
0 1
{
int sum = 0; 1 2
int i; 3 3
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
Output
cout<<“The sum is :”<<sum;
return 0;
}
sum i
0 1
{
int sum = 0; 1 2
int i; False!
3 3
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
Output
cout<<“The sum is :”<<sum;
return 0;
}
sum i
0 1
{
int sum = 0; 1 2
int i; 3 3
for ( i = 1; i <= 2 ; i++)
{
sum = sum + i; // or sum+=i;
}
Output
cout<<“The sum is :”<<sum;
return 0;
} The sum is : 3
The Sum of Even Values using a for Statement
Taghreed Salem
} ..
Thanks!
Do you have any questions?
You can contact me on:
Taghreed.salem@fa-hists.edu.eg