[go: up one dir, main page]

0% found this document useful (0 votes)
34 views4 pages

Hsslive-Xi-Cs-Siraj-07 Chapter CS

Uploaded by

haasinijv2008
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)
34 views4 pages

Hsslive-Xi-Cs-Siraj-07 Chapter CS

Uploaded by

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

Control Statements: Normally the statements are executed sequentially as written in the program.

But some times the operational


flow of control of a program has to be altered depending upon a condition. Such statements are called control statements. They are
classified into two. a). Decision making/selection statements b). Iteration statements.
Selection Statements: This statements allows to select an option from the alternatives based on a condition. They are if statement
and switch statement. The if statement is used to select a set of statements for execution based on a condition. Syntax is if (test
expression ) { statement block ; }. If we want to execute some action when the condition becomes false, then we use if...........else
statements. Nested If : If statement inside another if statement is called nested if . The else if ladder: It is used in the
programs when multiple branching is required. Different conditions will be given and each condition will decide which statement is
selected for execution. Ex.
Switch – case statement: This is used for multi branching. The expression is
Check whether the character is upper case,
evaluated to get an integer or character constant and it is matched against the constants
lower case, digits or other characters.
specified in the case statement. When a match is found the statement block associated
#include<iostream>
with that case is executed until the break statement is reached. If none of the case is
using namespace std;
matched, the default case will be executed. The default statement is optional, and if it is
int main( )
missing no action takes place when all matches fail. If a case statement does not
{ char ch;
include a break statement, then the control continues right on the next case
cout<<”Enter a character “<<endl;
statement until either a break is encountered or end of the switch is reached.
cin>>ch;
Note: All switch statements can be replaced by else if ladder, but all else if ladder
if( ch>= 'A' && ch<= 'Z' )
cannot be substituted by switch. Conditions involve only equality checking or it
cout<<”Capital letter “<<endl;
should be converted into equality expressions. Relational operators should not be used.
else if (ch>='a' && ch<='z')
cout<<”Small letter “<<endl;
To display the day of a week using switch statement. else if (ch>=0 && ch<=9
#include<iostream> cout<<”Digit”<<endl;
using namespace std; else
int main( ) cout<<”Other character”<<endl;
{ int day; return 0; }
cout<<”Enter a number between 1 and 7”<<endl;
cin>>day;
switch(day) Conditional operator ( ? : ) It is a ternary operator. It requires
{ case 1: cout<<”sunday “; three operands to operate upon. It is an alternative to if ... else
break; statement. The first operand will be a logical expression (condition )
case 2: cout<<”Monday”; and the remaining two are values or expressions. If the condition is true,
breake; the first value will be selected other wise the second. Ex. Largest
case 3: cout<<”Tuesday “; among two numbers. max=(a>b) ? a : b ; Largest
break; among three numbers max=(a>b) ?
.............................................. ( (a>c ) ? a : c ) : ( ( b> c) ? b : c );
case 7: cout<<”saturday “;
break;
default: cout<<”invalid input”;
}
return 0; }

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

To find the sum of first n natural numbers.


To find the sum of first n natural numbers. ( Using for loop ) #include<iostream>
#include<iostream> using namespace std;
using namespace std; int main( )
int main( ) { int n, sum=0;
{ int n, sum=0; cout<<”Enter the limit “;
cout<<”Enter the limit “; cin>> n;
cin>> n; i=1;
for(int i=1 ; i<=n ; i++) do
sum=sum+i; { sum=sum+i;
cout<<”Sum of the first “<<n<<” natural numbers = “<<sum; i=i+1;
return 0; } }while(i<=n);
cout<<”Sum of the numbers is = “<<sum;
return 0; }

06.07.2021 Downloaded from www.Hsslive.in ®


To find the sum of first n natural numbers. (while loop ) To find the sum of digits of a number.
#include<iostream> #include<iostream>
using namespace std; using namespace std;
int main( ) int main( )
{ int n, sum=0; { int n, sum=0,dig,temp;
cout<<”Enter the limit “; cout<<”Enter a number “;
cin>> n; cin>> n;
i=1; temp=n;
while(i<=n) while(n>0)
{ sum=sum+i; { dig=n%10;
i=i+1; sum=sum+dig;
} n= n/10;
cout<<”Sum of the first “<<n<<” numbers = “<<sum; }
return 0; } cout<<”Sum of the digits of “<<n<<” is = “<<sum;
return 0; }

Jump statements: We can change this sequential manner of the program


Nested Loop:
execution by using jump statements. They perform unconditional branching in
Placing a loop inside a body of another loop is
a program and they are return, goto, break and continue. C++ provides a
called nesting of a loop. When we nest two loops,
standard library function exit( ) which is used to terminate the program. a)
the outer loop counts the number of completed
repetitions of the inner loop. The loop control goto statement ; By using goto, we can transfer the prog ram control to
variable for two loops should be different. Ex. anywhere in the program without any condition. The syntax is

Ex. for goto statement


for(i=1; i<=2; i++) goto label; #include<iostream>
{ ...............................; using namespace std;
for(j=1; j<=3; j++) label : ...............................; int main( )
{ { float a, b;
...............................;
cout<<'\n'<<i<<” “<<j; cout<<”Enter two numbers : “;
} } cin>>a>>b;
The out put is of the form if(b==0)
(outer loop) (inner loop) goto end;
1 1 cout<<”The quotient is “<<a/b;
1 2 return 0;
1 3 end: cout<<” Division by zero not defined “;
2 1 return 0; }
2 2
2 3

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

Ex. for break statement Output


#include<iostream>
using namespace std; *
Continue Statement: This is used for skipping over a part of the code
int main( ) * * within the loop body and forces next iteration of the loop. It is used
{
int i,j; * * * only with loops .
for(i=1; i<=6; ++i) * * *
{ Ex. continue statement
cout<<"\n"; * * * #indlude<iostream>
for(j=1; j<=i; j++) * * * using namespace std;
{ cout<<" * "; int main( )
if(j==3) { for(int i=1; i<=10; i++)
break; {
} if(i==3 || i==7 )
} continue;
return 0; } cout<<i<<'\t';
}
return 0;
}
out put is 1 2 4 5 6 8 9 10

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.

Program to display a triangular stars. Multiplication Table


#include<iostream> #indlude<iostream>
using namespace std; using namespace std;
int main( ) int main( )
{ short int i, j; {
for(i=1; i<=5; i++) // outer loop int i, n;
{ cout<<" Enter a number ";
for(j=1; j<=i; j++) //inner loop cin>>n;
cout<<”*”; cout<<” Multiplication table of “ <<n<<endl;
} for(i=1; i<=10; i++)
return 0; cout<<i<<" X "<<n<<" = "<<i*n<<endl;
} return 0; }
06.07.2021 Downloaded from www.Hsslive.in ®
To find the average height of a group of students.
#include<iostream>
// Prime or not
using namespace std;
#include<iostream>
int main( )
using namespace std;
{ float ht, sum=0,avg_ht;
int main( )
short n=0;
{
char ch;
int i,n;
do
cout<<" Enter a number ";
{ cout<<”Enter the height “;
cin>>n;
cin >>ht;
for(i=2; i<=n/2; i++)
n++;
if(n%i==0)
sum=sum+ht;
{
cout<<”Any more student (Y/N) ? “;
cout<<"\n not a prime "<<endl;
cin>>ch;
return 0;
} while(ch=='Y' || ch=='y');
}
avg_ht=sum/n;
cout<<"\n"<<n<<" is a prime number ";
cout<<” Average height is = “<<avg_ht;
return 0;
return 0; }
}

Number is palindrome or not


#indlude<iostream> Check whether a number is Armstrong or not
using namespace std; 3 3 3
(like 1 + 5 + 3 = 153 , the number itself )
int main( )
#indlude<iostream>
{ int n, t,d, rev=0;
using namespace std;
cout<<" Enter a number ";
int main( )
cin>>n;
{ int n, t,d, sum=0;
t=n;
cout<<" Enter a number ";
while(n!=0)
cin>>n;
{ d=n%10;
t=n;
rev=rev*10+d;
while(n!=0)
n=n/10; }
{ d=n%10;
cout<<"\n The reverse is "<<rev<<endl;
sum=sum+d*d*d;
if(rev==t)
n=n/10; }
cout<<" The number is palindrome ";
cout<<"\n The sum of cubes of its digits is "<<sum<<endl;
else
if(sum==t)
cout<<" The number is not palindrome ";
cout<<" The number is Armstrong ";
return 0; }
else
cout<<" The number is not Armstrong ";
return 0; }

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; }

06.07.2021 Downloaded from www.Hsslive.in ®

You might also like