CH5 - Homework and Solution
CH5 - Homework and Solution
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
Dont use semicolon Use semicolon
If condition not satisfied, then If condition not satisfied, then the loop will
the loop dont 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)
/