Professional Documents
Culture Documents
CH5 - Summery and Howmwork and AK
CH5 - Summery and Howmwork and AK
Chapter 5
Iteration Statements (Loops)
Loops are used to repeat a set of operations or statements many times.
1. for loop
2. while loop
3. do while loop
# include<iostream.h>
main()
{
int i;
for(i=1 ; i<=100 ; i++)
cout<< i << endl;
}
ودط/מ−אدאد
CH5- Iteration Statements (Loops)
Example:
Write a program to print even numbers from 1 to 100.
# include<iostream.h>
main()
{
int i;
for(i=2 ; i<=100 ; i+=2)
cout<< i << endl;
}
The output for the above program is:
2
4
6
.
.
100
# include<iostream.h>
main()
{
int i=2;
while( i<=100 ){
cout<< i << endl;
i+=2;
}}
ودط/מ−אدאد
CH5- Iteration Statements (Loops)
# include<iostream.h>
main()
{
int i=1;
do{
cout<< i << endl;
i++;
} while( i<=100 );
# include<iostream.h>
main()
{
int i=2;
do{
cout<< i << endl;
i+=2;
} while( i<=100 );
}
ودط/מ−אدאد
CH5- Iteration Statements (Loops)
Chapter-5 Homework
Student instruction
1. This homework should be completed by the student based on the class room work and
not submit it to the teacher
2. No marks about this homework
ودط/מ−אدאد
CH5- Iteration Statements (Loops)
10) for (int i=5; i<8; i++) , how many times the loop will repeat statements__________.
a) 8 b) 7 c) 3 d) 4
11) Which looping process checks the test condition at the end of the loop?
a) while b) for c) do-while d) None
12) If there is more than one statement in the block of a for loop, which of the following must
be placed at the beginning and the ending of the loop block?
a) parentheses ( ) b) braces { } c) brackets [ ] d) arrows < >
ودط/מ−אدאد
CH5- Iteration Statements (Loops)
d) x = 1;
while ( x <= 10 );
x++;
}
e) for ( y = 1; y =! 10 : y += 1 )
cout << y << endl;
2) Define the loop and compare between while and do … while.
3) Write the syntax of do while loop.
4) Write the output for these programs:
a) int i=8;
while(i>0){
cout<<i<<"\n";
i=i-2;
}
b) int i=30;
do
{
cout << i;
i++;
}while(i<10);
c) # include<iostream.h>
main()
{
int i;
for(i=3 ; i<=10 ; i+=3)
cout<< i << “\t”;
}
d) x=1;
while(x<10){
cout<<x<<"\t";
++x;
}
e) x = 4;
while ( x <= 20 )
{
x+=4;
}
cout<<x;
ودط/מ−אدאد
CH5- Iteration Statements (Loops)
ودط/מ−אدאد
CH5- Iteration Statements (Loops)
while Do-while
Condition come first Condition come last
Don’t use semicolon Use semicolon
If condition not satisfied, then If condition not satisfied, then the loop will
the loop don’t executed execute at least one time
3) Write the syntax of do while loop.
Initial value ;
do
{
statements;
increment/decrement;
} while (condition) ;
ودط/מ−אدאد
CH5- Iteration Statements (Loops)
ودط/מ−אدאد
CH5- Iteration Statements (Loops)
ودط/מ−אدאد