0% found this document useful (0 votes)
37 views

CH5 - Homework and Solution

This document contains instructions and questions for a homework assignment on iteration statements (loops) in C++. It includes multiple choice and true/false questions testing knowledge of loop syntax and behavior. Short questions assess writing basic loop code and identifying errors. Longer questions involve writing loop programs to print sequences of numbers. The answer key provides the correct responses.

Uploaded by

Jim Demitriyev
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)
37 views

CH5 - Homework and Solution

This document contains instructions and questions for a homework assignment on iteration statements (loops) in C++. It includes multiple choice and true/false questions testing knowledge of loop syntax and behavior. Short questions assess writing basic loop code and identifying errors. Longer questions involve writing loop programs to print sequences of numbers. The answer key provides the correct responses.

Uploaded by

Jim Demitriyev
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/ 7

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

1) Choose the correct answer


1) The condition being tested within the ____________ loop may be relational operations.
a) while b) break c) switch d) continue
2) If you want to sum the number from 1 to 20, you can use __________________
a) if b) switch c) while d) none
3) Which of the following loop uses three things initialization, condition to terminate loop and
increasing the value of loop counter?
a) switch b) if c) while d) break
4) The three things inside the for loop are separated by ____________.
a) colon b) comma c) semicolon d) hyphen
5) What is the final value of x when the code int x; for(x=0;x<=10;x++){ } is run ?
a) 10 b) 9 c) 0 d) 11
6) Which of the following is not a loop statement in C++?
a) repeat until b) while c) for d) do-while
7) Which of the following loops will definitely execute at least once even if the condition is not
satisfied?
a) do-while b) for c) while d) None
8) To repeat a set of the statements for 25 times, which kind of statement will be required?
a) Iterative b) Selective c) Both (a) and (b) d) None of them
9) What is the output of the following statements in C++?
#include<iostream.h>
void main ( )
{
int i ;
for ( i= 1; i <= 5; i +=2)
cout<< i;
}
a) 1 2 3 4 5 b) 1 3 5 c) 5 6 7 d) None

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

2) Which of the following is true or false?


1) do while loop is executed at least once. [ ]
2) The for loop execution has statements inside the loop executed before checking the
condition for the first time. [ ]
3) Do while has to end with semicolon. [ ]
4) While loop is a selection statement in C++. [ ]
5) The while loop can be replaced by for loop in all the cases. [ ]
6) for loop allows a statement or group of statements to be performed repeatedly in a
loop. [ ]
7) Initial value in loops will be executed more times. [ ]
3) Solve these short questions
1) Find the errors and write the correct program for the following:
a) @include<iostream.h>
Main( )
{
Intt i;
for(i=0; i>=10 , i++)
Cout<<Allah;
{
b) whil ( c <= 5 )
{
product *= c;
++c;
c) int x = 1, total=0;
while ( x =< 10 )
}
total += x;
++x;
}

    /
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)

4) Solve these questions in details


1) Write a C++ program to print the number from 1 to 30.
2) Write a C++ program to print the odd numbers from 11 to 77.
3) Write a C++ program to print natural numbers from 1 to 50 using while loop.
4) Write a C++ program to print the word Allah 100 times.
5) Write a C++ program to print the even numbers from 11 to 77.
6) Write a C++ program to print these numbers: 5 10 15 20 using loop.

    /
CH5- Iteration Statements (Loops)

Chapter-5 Homework Answer Key


1) Choose the correct answer
1-a , 2-c , 3-c , 4-c , 5-d , 6-a , 7-a , 8-a , 9-b , 10-c , 11-c , 12-b
2) Which of the following is true or false?
1-true , 2-false , 3- true, 4- false, 5- true, 6- true, 7- false
3) Solve these short questions
1) Codes correction:
a) #include<iostream.h>
main( )
b) while ( c <= 5 )
{ {
int i; product *= c;
for(i=0; i>=10 ; i++) ++c;
cout<<Allah;
}
}
c) int x = 1, total=0; d) x = 1;
while ( x <= 10 ) while ( x <= 10 )
{ {
total += x; x++;
++x; }
}
e) for ( y = 1; y != 10 ; y += 1 )
cout << y << endl;
2) Define the loop and compare between while and do while.
Loops are used to repeat a set of operations or statements many times.

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)

4) Write the output for these programs:


a) 8
6
b) 30
4
2
c) 3 6 9 d) 1 2 3 4 5 6 7 8 9
e) 24
4) Solve these questions in details
1) Write a C++ program to print the number from 1 to 30.
# include<iostream.h>
main()
{
int i;
for(i=1 ; i<=30 ; i++)
cout<< i << endl;
}
2) Write a C++ program to print the odd numbers from 11 to 77.
# include<iostream.h>
main()
{
int i;
for(i=11 ; i<=77 ; i+=2)
cout<< i << endl;
}
3) Write a C++ program to print natural numbers from 1 to 50 using while loop.
# include<iostream.h>
main()
{
int i=1;
while( i<=50 )
{
cout<< i << endl;
i++;
}
}
4) Write a C++ program to print the word Allah 100 times.
# include<iostream.h>
main()
{
int i;
for(i=1 ; i<=100 ; i++)
cout<< Allah << endl;
}

    /
CH5- Iteration Statements (Loops)

5) Write a C++ program to print the even numbers from 11 to 77.


# include<iostream.h>
main()
{
int i;
for(i=12 ; i<=77 ; i+=2)
cout<< i << endl;
}
6) Write a C++ program to print these numbers: 5 10 15 20 using loop.
# include<iostream.h>
main()
{
int i;
for(i=5 ; i<=20 ; i+=5)
cout<< i << endl;
}

    /

You might also like