Hsslive-Xi-Cs-Siraj-07 Chapter CS
Hsslive-Xi-Cs-Siraj-07 Chapter CS
Looping statements (Iteration statements ) : They allow a set of instructions to be performed until a certain condition is fulfilled. A
looping statements contain initialisation expression, test expression, update expression, and loop body. The loop body is used when
an action is to be repeated for execution till the condition is true. The variable used in the test expression ( loop control variable) gets
its initial value through the initialisation expression. Update expression changes the value of the loop control variable. They are
divided into two. Entry controlled loop ( it first check the condition and if it is true, the loop body is executed and the execution will
be continued as long as the condition will be true. Ex. For loop, while loop ) and exit controlled loop (This loop always execute at
least once , even when the test expression evaluates to false initially. Here the loop body is executed first and then condition is
checked. If it is true loop body starts its second execution and continues till the condition becomes false. Ex do-while loop) .
b) break statement : A break statement skips the rest of the loop and jumps over to the statement following the loop. It is used
with switch and loops. A break statement causes an exit only from the innermost loop. Program control goes out of the loop even
though the test expression is true
Here 3 and 7 are not in the list. When the value i becomes 3 or 7 continue statement is executed as a result the out put
statement is skipped and control goes to next iteration.
06.07.2021 Downloaded from www.Hsslive.in ®
Questions :
1. Write the four components of the loop ?
2. In C++ ------------ is used as a statement terminator ? ( semi column )
3. Differentiate between 'a' and “a” in C++? ( a character constant which require 1 byte of memory, a string constant needs 2
bytes of memory).
4. What happens if break statement is not used in switch statement ?
5. What is meant by type conversion? Differentiate between implicit and explicit type conversion? Explain with examples ?
6. Differentiate between entry controlled loop and exit controlled loop? Give examples .
7. What is the usage of default in switch statement ?
8. What is the difference between the expression a=5 and a==5 .
9. ----------- is the key word used with goto statement ( label )
10. ---------- operator in C++ is a ternary operator ( ? : or conditional operator )
11. -------------- statement is used for unconditional jump in a program. ( goto )
12. ---------- statement is used to exit a loop even if the condition is true ( break )
13. A break statement causes an exit ................... ( from the inner most loop or switch )
14. Predict the output : int a = 5; int b= a++ + ++a; cout<<b<<”\t<<”a; (5+7=12, use then change & change then use.
12 7)
15. predict the output of the following code code segment. Int a, b, n=1000; cin>>a; b=n+a>1800?400:200;
cout<<b; ( a) if the input value is 500. (b) if the input is 1000 (200,400)
16. predict the output of the following code segment. for(int i=1; i<=10; i++); cout<<i; ( 11 , there is a semi column
after the loop )
17. Rewrite the following code using while loop and do while loop. for(int i=2, sum=0; i<=20; i=i+2 ) { sum+= i ; }
18. Write a program to produce the following design
19. What is wrong with the following while statement 4
if the value of k=2 4 3
while(k>=0) 4 3 2
sum+=k; 4 3 2 1
( There is no updation statement. The value of k is always 2 and it is an infinite loop )
20. Write the declaration statement for using the identifier PI in the program code in which the value is
always 3.14
21. Meaningful and processed form of data is known as .............. (information )
22. Predict the out put of the following program code if we give ( a) 0 as input (b) 2 as input
( c ) 7 as input justify the answer int a; cin>>a;
switch(a)
{ case 0: cout<<”Zero “;
case 1: cout<<”one” ;
case 2: cout<<”Two” ;
break;
default: cout<<”invalid number “; }
( (a) zero one two - no break statement after each statement. (b) Two (c) invalid number )
23. How many times the following loop will be executed ?
Int s=0, i=0; do { s+=i;
i++; } while(i<=5) ; ( 6 times i have values 0, 1,2,3,4,5 )
24. Write C++ program for the following questions ? (a). To check whether the given number is prime or not. (b). To print
the terms of the Fibonacci series. (c). To find the reverse of a given number . (d). To check whether a given number is
palindrome or not. (e).
25. Distinguish between break and continue statements ? Explain with example ?
break continue
• Used with switch and loop • Used with loops
• Forces termination of the loop even though • It forces next iteration of the loop.
the condition is true. • A continue statement will just abandon
• A break inside a loop will abort the loop and the current iteration and let the loop
transfer control to the statement following start next iteration.
the loop.
Factors of a number
#indlude<iostream> Fibonacci series
using namespace std; #include<iostream>
int main( ) using namespace std;
{ int main( )
int i, n; {
cout<<" Enter a number "; int n, i,f=1,s=1,t ;
cin>>n; cout<<"Enter number of terms in the series ";
cout<<"The factors of "<<n<<" are "<<endl; cin>>n;
for(i=1; i<=n; i++) cout<<f<<" "<<s<<" ";
{ for(i=3; i<=n; i++)
if(n%i==0) {
cout<<i<<"\t"; t=f+s;
} cout<<t<<" ";
return 0; } f=s;
s=t;
}
return 0; }