Tamilnadu State Board New Syllabus Samacheer Kalvi 11th Computer Science Guide Pdf Chapter 10 Flow of Control Text Book Back Questions and Answers, Notes.
Tamilnadu Samacheer Kalvi 11th Computer Science Solutions Chapter 10 Flow of Control
11th Computer Science Guide Flow of Control Text Book Questions and Answers
Book Evaluation
Part I
Choose The Correct Answer
Question 1.
What is the alternate name of null statement?
a) No statement
b) Empty statement
c) Void statement
d) Zero statement
Answer:
b) Empty statement
Question 2.
In C++, the group of statements should enclosed within:
a) { }
b) [ ]
c)()
d)<>
Answer:
a) { }
Question 3.
The set of statements that are executed again and again in iteration is called as:
a) condition
b) loop
c) statement
d) body of loop
Answer:
d) body of loop
Question 4.
The multi way branching statement:
a) if
b) if… else
c) switch
d) for
Answer:
c) switch
Question 5.
How many types of iteration statements?
a) 2
b) 3
c) 4
d) 5
Answer:
b) 3
Question 6.
How many times the following loop will execute?
for (int i=0; i<10; i++)
a) 0
b) 10
c) 9
d) 11
Answer:
b) 10
Question 7.
Which of the following is the exit control loop?
a) for
b) while
c) do…while
d) if…else
Answer:
c) do…while
Question 8.
Identify the odd one from the keywords of jump statements:
a) break
b) switch
c) go to
d) continue
Answer:
b) switch
Question 9.
Which of the following is the exit control loop?
a) do-while
b) for
c) while
d) if-else
Answer:
a) do-while
Question 10.
A loop that contains another loop inside its body:
a) Nested loop
b) Inner loop
c) In line loop
d) Nesting of loop
Answer:
a) Nested loop
Part – II
Short Answers
Question 1.
What is a null statement and compound statement?
Answer:
null statement; The “null or empty statement” is a statement containing only a semicolon. It takes the flowing form: ;
// it is a null statement
compound Statement:
In C++, a group of statements enclosed by pair of braces {} is called as a compound statement or a block.
The general format of compound statement is:
{
statement1;
statement2;
statement3;
}
Question 2.
What is a selection statement? Write its types?
Answer:
The selection statement means the statements are executed depends upon a condition. If a condition is true, a true block (a set of statements) is executed, otherwise, a false block is executed. This statement is also called a decision statement or selection statement because it helps in making a decision about which set of statements are to be executed.
Types:
- Two-way branching
- Multiway branching
Question 3.
Correct the following code segment:
Answer:
if (x=1)
p= 100;
else
P = 10;
Correct code: (Equal to operator is wrongly given)
if (x==1)
p= 100;
else
p = 10;
Question 4.
What will be the output of the following code:
int year;
cin >> year;
if (year % 100 == 0)
if (year % 400 == 0)
cout << “Leap”;
else
cout << “Not Leap year”;
If the input is given is
(i) 2000
(ii) 2003
(iii) 2010
Answer:
i) 2000 AS INPUT
Output
Leap
ii) 2003AS INPUT
Output
Not Leap year
iii) 2010 AS INPUT
Output
Not Leap year
Question 5.
What is the output of the following code?
for (int i=2; i<=10 ; i+=2)
cout << i;
Answer:
Output
2 4 6 8 10
Question 6.
Write a for loop that displays the number from 21 to 30.
Answer:
# include
using namespace std;
int main()
{
for (int = 21; i < 31; i++)
cout << “value of i:” << i << endl;
return 0;
}
Output:
Value of i: 21
Value of i: 22
Value of i: 23
Value of i: 24
Value of i: 25
Value of i: 26
Value of i: 27
Value of i: 28
Value of i: 29
Value of i: 30
Question 7.
Write a while loop that displays numbers 2,4, 6, 8…….20.
Answer:
While loop to
i = 2;
while(i<=20)
{
cout<< i <<“,”;
i = i+2;
}
Question 8.
Compare an if and a ? : operator.
Answer:
The if statement evaluates a condition, if the condition is true then a true-block is executed, otherwise false-block will be executed! Block may consists of one or more statements. The conditional operator (or Ternary operator) . is an alternative for ‘if else statement’. Here if the condition is true one statement otherwise another statement will be executed.
Part – III
Short Answers
Question 1.
Convert the following if-else to a single conditional statement:
Answer:
if (x >= 10)
a = m + 5;
else
a = m;
a = (x >= 10)? m + 5 : m ;
Question 2.
Rewrite the following code so that it is functional:
Answer:
v = 5;
do;
{
total += v;
cout << total;
while v <=10
CORRECT CODE:
int v = 5;
do
{
total += v;
v++;
} while (v <= 10); cout << total;
Question 3.
Write a C++ program to print the multiplication table of a given number.
Answer:
# include
using namespace std;
int main ()
{
int num;
cout << “Enter Number to find its multiplication table”; cin >> num;
for (int a = 1; a < = 10; a++)
{
cout << num << “*” << a << “=” << num*a << endl;
}
return( );
}
Output
Question 4.
Write the syntax and purpose of switch statement.
Answer:
The syntax of the switch statement is:
switch(expression)
{
case constant 1:
statement(s);
break;
case constant 2:
statement(s);
break;
.
.
.
.
default:
statement(s);
}
Purpose of switch statement:
The switch statement is a multi-way branch statement. It provides an easy way to dispatch execution to different parts of code based on the value of the expression.
Question 5.
Write a short program to print following series: a) 1 4 7 10…… 40
Answer:
PROGRAM
using namespace std;
#inciude
int main()
{
for(int i=1;i<=40;i=i+3)
cout<<i<<” “;
return 0;
}
Output
Part – IV
Explain In Detail
Question 1.
Explain the control statement with a suitable example.
Answer:
Control statements are statements that alter the sequence of flow of instructions.
In a program, statements may be executed sequentially, selectively, or iteratively. Every programming language provides statements to support sequence, selection (branching), and iteration.
Selection statement:
The selection statement means the statement(s) are executed depends upon a condition. If a condition is true, a true block ¡s executed otherwise a false block is executed. This statement is also called a decision statement or selection statement because it helps in making decisions about which set of statements are to be executed.
Iteration statement:
The iteration statement is a set of the statement are repetitively executed depends upon conditions. If a condition evaluates to true, the set of statements (true block) is executed again and again. As soon as the condition becomes false, the repetition stops. This is also known as a looping statement or iteration statement.
Question 2.
What entry control loop? Explain any one of the entry control loops with a suitable example.
Answer:
In an entry-controlled loop, the test- expression is evaluated before entering into a loop, while and for statements are called as entry controlled loop.
Working of while loop:
A while loop is a control flow statement that allows the loop statements to be executed as long as the condition is true. The while loop ¡s an entry-controlled loop because the test-expression is evaluated before the entering into a loop.
The while loop syntax is:
while (Test expression )
{
Body of the loop;
}
Statement-x;
In a while loop, the test expression is evaluated and if the test expression result is true, then the body of the loop is executed and again the control is transferred to the while loop. When the test expression result is false the control is transferred to statement-x.
The control flow and flow chart of the while loop is shown below.
Example:
#include <iostream>
using namespace std;
int main ()
{
int i=1,sum=0;
while(i<=10)
{
sum=sum+i;
i++;
}
cout<<“The sum of 1 to 10 is “<<sum; return 0;
}
Output
The sum of 1 to 10 is 55
Question 3.
Write a program to find the LCM and GCD of two numbers.
Answer:
PROGRAM:
using namespace std;
#include <iostream>
int main()
{
int n1, n2, i, gcd;
cout<<“Enter two integers:”;
cin>>n1>>n2;
for(i=1; i <= n1 && i <= n2; ++i)
{
// Checks if i is factor of both integers
if(n1%i==0 && n2%i==0)
gcd = i;
}
int lcm = n1*n2 /gcd;
cout<<“\nG.C.M.OF”<<nl<<“and”<<n2<<”
is”<<gcd;
cout< < “\nL.C. M. OF “< < n 1 < < ” and ” < < n2< < ” is “<<lcm; return 0;
}
Output
Question 4.
Write programs to find the sum of the following series:
Answer:
PROGRAM
using namespace std;
#include
int main()
{
int i,x,n,f=1,sign=1;
float sum=0,t;
cout<<“\nEnter N value”;
cin>>n;
cout<<“\nEnter x value …”;
cin>>x;
t=x;
for(i=1;i<=n;i++)
{
f = f * i;
sum = sum + sign * t/f;
t = t * x;
sign = -sign;
}
cout<<”SUM OF THE SERIES = “<<sum;
return O;
}
PROGRAM
using namespace std;
#include
int main()
{
int i,x,n;
float sum=0,t;
cout<<“\nEnter N value”;
cin>>n;
cout<<“\nEnter x value …”;
cin>>x;
t=x;
for(i=l;i<=n;i++)
{
sum = sum + t/i;
t = t * x;
}
cout<<“SUM OF THE SERIES = “<<sum;
}
Output
Question 5.
Write a program to find sum of the series s=1 +x +x2 + ……..+xn
Answer:
PROGRAM
using namespace std;
#include
int main()
{
int sum=1,x,i,t,n;
cout<<“\nEnter N value”;
cin>>n;
cout<<“\nEnter x value … “;
cin>>x;
t=x;
for(i=l;i<=n;i++)
{
sum = sum + t;
t = t * x;
}
cout<<“SUM = “<<sum;
}
Output
11th Computer Science Guide Flow of Control Additional Questions and Answers
Choose The Correct Answer 1 Mark
Question 1.
The empty statement is otherwise called as ………………..
(a) Control statement
(b) Zero statement
(c) Null statement
(d) Block statement
Answer:
(c) Null statement
Question 2.
The basics of control structure are……………. statement.
a) Selection
b) Iteration
c) Jump
d) All the above
Answer:
d) All the above
Question 3.
Iteration statement is called as ………………..
(a) Null statement
(b) Block statement
(c) Selection statement
(d) Looping statement
Answer:
(d) Looping statement
Question 4.
In a program, the action may be ……………..
a) Variable declarations
b) Expression evaluations
c) Assignment operations
d) All the above
Answer:
d) All the above
Question 5.
……………….. is a multi-path decision-making statement.
(a) if
(b) if-else
(c) else – if
(d) if-else ladder
Answer:
(d) if-else ladder
Question 6.
The ……………. is a statement containing only a semicolon.
a) Null
b) Empty statement
c) Both A and B
d) None of these
Answer:
c) Both A and B
Question 7.
……………….. is more efficient than the if-else statement.
(a) Control statement
(b) Switch statement
(c) Empty statement
(d) Null statement
Answer:
(b) Switch statement
Question 8.
C++ allows a group of statements enclosed by pair of ……………..braces.
a) ()
b) { }
c) [ ]
d) < >
Answer:
b) { }
Question 9.
C++ supports types of iteration statements.
(a) 3
(b) 2
(c) 4
(d) 5
Answer:
(a) 3
Question 10.
……………. statements alter the sequence of flow of instructions.
a) Control
b) Null
c) Compound
d) None of these
Answer:
a) Control
Question 11.
……………….. is used to transfer the control from one place to another place without any condition in a program.
(a) Break statement
(b) Continue statement
(c) goto statement
(d) All the above
Answer:
(c) goto statement
Question 12.
The ……………. statement are the statements, that are executed one after another only once from top to bottom.
a) Sequential
b) Selective
c) Iterative
d) None of these
Answer:
a) Sequential
Question 13.
…………….. statements do not alter the flow of execution.
a) Sequential
b) Selective
c) Iterative
d) None of these
Answer:
a) Sequential
Question 14.
…………….. statements always end with a semicolon (;).
a) Sequential
b) Selective
c) Iterative
d) None of these
Answer:
a) Sequential
Question 15.
The ……………. statement means the statement (s) are executed depends upon a condition.
a) Sequential
b) Selective
c) Iterative
d) None of these
Answer:
b) Selective
Question 16.
If a condition is true, a true block (a set of statements) is executed otherwise a false block is executed is called …………… statement.
a) Decision
b) Selection
c) Either A or B
d) None of these
Answer:
c) Either A or B
Question 17.
The ……………. statement is a set of the statement are repetitively executed depends upon conditions.
a) Sequential
b) Selective
c) Iterative
d) None of these
Answer:
c) Iterative
Question 18.
The condition on which the execution or exit from the loop is called ………………
a) Exit-condition
b) Test-condition
c) Either A or B
d) None of these
Answer:
c) Either A or B
Question 19.
In C++, any non-zero is treated as …………… including negative numbers
a) False
b) True
c) Complement
d) None of these
Answer:
b) True
Question 20.
In C++, zero is treated as …………..
a) False
b) True
c) Complement
d) None of these
Answer:
a) False
Question 21.
Decisions in C++ are made using …………….. statement.
a) if..else
b) switch., case
c) Either A or B
d) None of these
Answer:
c) Either A or B
Question 22.
……………. statement which chooses between two alternatives.
a) if..else
b) switch., case
c) Either A or B
d) None of these
Answer:
a) if..else
Question 23.
………………. creates branches for multiple alternatives sections of code, depending on the value of a single variable.
a) if..else
b) switch., case
c) Either A or B
d) None of these
Answer:
b) switch., case
Question 24.
…………. is a two-way branching statement.
a) if..else
b) switch., case
c) Either A or B
d) None of these
Answer:
a) if..else
Question 25.
……………….. is a multiple branching statement.
a) if..else
b) switch., case
c) Either A or B
d) None of these
Answer:
b) switch., case
Question 26.
An if statement contains another if the statement is called …………….
a) Nested if
b) Compound if
c) Block if
d) Group if
Answer:
a) Nested if
Question 27.
The nested if can have ……………. forms.
a) four
b) two
c) three
d) five
Answer:
c) three
Question 28.
……………. is nested if form.
a) If nested inside if part
b) If nested inside both if part and else part
c) If nested inside else part
d) All the above
Answer:
d) All the above
Question 29.
The ………….. is a multi-path decision-making statement.
a) if-else ladder
b) Simple if
c) if… else
d) None of these
Answer:
a) if-else ladder
Question 30.
In ……………..type of statement ‘if’ is followed by one or more else if statements and finally end with an else statement.
a) if-else ladder
b) Simple if
c) if… else
d) None of these
Answer:
a) if-else ladder
Question 31.
The……………. operator is an alternative for the if-else statement.
a) Conditional
b) Ternary
c) Continue
d) Either A or B
Answer:
d) Either A or B
Question 32.
The conditional operator consists of …………….. symbols.
a) two
b) three
c) four
d) five
Answer:
a) two
Question 33.
The conditional operator takes …………….. arguments.
a) two
b) three
c) four
d) five
Answer:
b) three
Question 34.
The switch statement is a …………… statement.
a) Multi-way branch
b) Two-way branch
c) Jump
d) None of these
Answer:
a) Multi-way branch
Question 35.
………….. statement provides an easy way to dispatch execution to different parts of code based on the value of the expression.
a) if..else
b) switch
c) goto
d) None of these
Answer:
b) switch
Question 36.
The ………………statement replaces multiple if-else sequences.
a) if..else
b) switch
c) go to
d) None of these
Answer:
b) switch
Question 37.
The expression provided in the switch should result in a…………… value.
a) Constant
b) Variant
c) Null
d) None of these
Answer:
a) Constant
Question 38.
In switch statement …………… statement is optional.
a) default
b) break
c) Both A and B
d) None of these
Answer:
c) Both A and B
Question 39.
The …………… statement is used inside the switch to terminate a statement sequence.
a) default
b) break
c) continue
d) None of these
Answer:
b) break
Question 40.
In a switch statement, when a ………….. statement is reached, the flow of control jumps to the next line following the switch statement.
a) go to
b) break
c) continue
d) None of these
Answer:
b) break
Question 41.
……………. statement checks only for equality.
a) go to
b) if,.else
c) continue
d) switch
Answer:
d) switch
Question 42.
………….. statement checks for equality as well as for logical expression.
a) go to
b) if..else
c) continue
d) switch
Answer:
b) if..else
Question 43.
………….. statement uses a single expression for multiple choices.
a) go to
b) if..else
c) continue
d) switch
Answer:
d) switch
Question 44.
A(n) ………….. statement uses multiple statements for multiple choices.
a) go to
b) if..else
c) continue
d) switch
Answer:
b) if..else
Question 45.
The ……………. statement evaluates integer, character, pointer or floating-point type or Boolean type.
a) go to
b) if..else
c) continue
d) switch
Answer:
b) if..else
Question 46.
……….. statement evaluates only character or an integer data type.
a) go to
b) if..else
c) continue
d) switch
Answer:
d) switch
Question 47.
If the expression inside the switch statement turns out to be false then statements are executed.
a) default
b) break
c) else block
d) None of these
Answer:
a) default
Question 48.
If the expression inside if turns out to be false, statement inside ………….. will be executed.
a) default
b) true block
c) else block
d) None of these
Answer:
c) else block
Question 49.
The ………….. statement is more flexible.
a) switch
b) if
c) continue
d) None of these
Answer:
b) if
Question 50.
The …………… statement is more efficient than the if-else statement.
a) switch
b) go to
c) continue
d) None of these
Answer:
a) switch
Question 51.
Which is true related to the switch statement?
a) A switch statement can only work for the quality of comparisons.
b) No two case labels in the same switch can have identical values.
c) If character constants are used in the switch statement, they are automatically converted to their equivalent ASCII codes.
d) All the above
Answer:
d) All the above
Question 52.
When a switch is a part of the statement sequence of another switch, then it is called as …………….. switch statement.
a) Iterative
b) Sequential
c) Nested
d) None of these
Answer:
c) Nested
Question 53.
A(n) ………….. is a sequence of one or more statements that are repeatedly executed until a condition is satisfied.
a) Iteration
b) Looping
c) Iteration or Looping
d) None of these
Answer:
c) Iteration or Looping
Question 54.
………….. is used to reduce the length of code, to reduce time, to execute the program and takes less memory space.
a) Iteration
b) Looping
c) Iteration or Looping
d) None of these
Answer:
c) Iteration or Looping
Question 55.
C++supports ………….. types of iteration statements.
a) five
b) four
c) three
d) two
Answer:
c) three
Question 56.
C++ supports ………….. type of iteration statement.
a) for
b) while
c) do..while
d) All the above
Answer:
d) All the above
Question 57.
Every loop has …………….. elements.
a) five
b) four
c) three
d) two
Answer:
b) four
Question 58.
Every loop has ………….. element.
a) Initialization expression
b) Test expression / Update expression
c) The body of the loop
d) All the above
Answer:
d) All the above
Question 59.
The control variable(s) must be initialized …………. the control enters into a loop.
a) after
b) before
c) Either A or B
d) None of these
Answer:
b) before
Question 60.
Whose value decides whether the loop-body will be executed or not?
a) Test expression
b) Update expression
c) Initialization expression
d) None of these
Answer:
a) Test expression
Question 61.
In an ……….. loop, the test-expression is evaluated before entering into a loop,
a) Entry-controlled
b) Exit-controlled
c) Both A and B
d) None of these
Answer:
a) Entry-controlled
Question 62.
In an ………….. loop, the test-expression is evaluated before exiting from the loop.
a) Entry-controlled
b) Exit-controlled
c) Both A and B
d) None of these
Answer:
b) Exit-controlled
Question 63.
……………. is used to change the value of the loop variable.
a) Test expression
b) Update expression
c) Initialization expression
d) None of these
Answer:
b) Update expression
Question 64.
……………. statement is executed at the end of the loop after the body of the loop is executed.
a) Test expression
b) Update expression
c) Initialization expression
d) None of these
Answer:
b) Update expression
Question 65.
In an …………. loop, first, the test expression is evaluated and if it is nonzero, the body of the loop is executed otherwise the loop is terminated.
a) Entry-controlled
b) Exit-controlled
c) Both A and B
d) None of these
Answer:
a) Entry-controlled
Question 66.
In an …………. loop, the body of the loop is executed first then the test-expression is evaluated.
a) Entry-controlled
b) Exit-controlled
c) Both A and B
d) None of these
Answer:
b) Exit-controlled
Question 67.
………….. loop statement contains three different statements.
a) for
b) while
c) do..while
d) All the above
Answer:
a) for
Question 68.
The for statement contains ……………. statement
a) Test expression
b) Update expression
c) Initialization expression
d) All the above
Answer:
d) All the above
Question 69.
The for statement sections are separated by ………………
a) Semicolons
b) Colon
c) Comma
d) None of these
Answer:
a) Semicolons
Question 70.
The initialization part of the loop is used to initialize variables or declare a variable which is executed …………… time(s).
a) Loop repeated
b) Only one
c) two
d) None of these
Answer:
b) Only one
Question 71.
How many times the following loop will be executed?
for (i=0; i <= 10; i++);
a) 10
b) 9
c) 11
c) None of these
Answer:
c) 11
Question 72.
If the body of the loop contains a ……………. statement(s), need not use curly braces.
a) Single
b) More than one
c) Either A or B
d) None of these
Answer:
a) Single
Question 73.
In for loop, multiple initializations and multiple update expressions are separated by ……………
a) Semicolons
b) Colons
c) Commas
d) None of these
Answer:
c) Commas
Question 74.
In a for loop, which expression is optional?
a) Test expression
b) Update expression
c) Initialization expression
d) All the above
Answer:
d) All the above
Question 75.
What will be formed if the following loop is executed?
for( i=0; ++i);
a) Finite loop
b) Indefinite loop
c) No iteration
d) None of these
Answer:
b) Indefinite loop
Question 76.
A loop that has no statement in its body is called an ……………
a) Empty loop
b) Indefinite loop
c) Finite loop
d) None of these
Answer:
a) Empty loop
Question 77.
Identify the empty loop from the following.
a) for( i+0 ; i<=5; +=i);
b) for( i+0 ; i<=5; +=i); {cout<<“C++”;>
c) for( i+0 ; i<=5; +=i) { }
d) All the above
Answer:
d) All the above
Question 78.
A ………….. loop is useful for pausing the program for some time.
a) Time delay
b) Infinite loop
c) Finite loop
d) None of these
Answer:
a) Time delay
Question 79.
……………. is a time delay loop.
a) for(i=l; i<=100; i++);
b) int i=l;
while( ++i < 10)
{
}
c) int i=l;
do
{
} while( ++i < 10);
d) All the above
Answer:
d) All the above
Question 80.
How many times the following loop will be executed?
int i=l;
while( ++i < 10)
cout<< “The value of i is “<<i;
a) 10
b) 9
c) 8
d) Infinite times
Answer:
c) 8
Question 81.
How many times the following loop will be executed?
int i=l;
while( i++< 10)
cout<< “The value of i is “<<i;
a) 10
b) 9
c) 8
d) Infinite times
Answer:
b) 9
Question 82.
In ………… loop, the body of the loop will be executed atleast once.
a) while
b) do..while
c) for
d) All the above
Answer:
b) do..while
Question 83.
In ……………….. loop, the body of the loop is executed at least once, even when the condition evaluates false during the first iteration.
a) while
b) do..while
c) for
d) All the above
Answer:
b) do..while
Question 84.
What will be the output of the following program?
using namespace std;
int main ()
{
int n = 10;
do
{
cout<<n<<“”; n-; }while (n>0);
}
a) 109 8 7 6 5 43 2 1
b) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
c) 1 2 3 4 5 6 7 8 9 10
d) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1,0
Answer:
b) 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Question 85.
……………. statements are used to interrupt the normal flow of the program.
a) Jump
b) Loop
c) Decision making
d) None of these
Answer:
a) Jump
Question 86.
……………. is a jump statement.
a) go to statement
b) break statement
c) continue statement
d) All the above
Answer:
d) All the above
Question 87.
The ………….. statement is a control statement which is used to transfer the control from one place to another place without any condition in a program.
a) go to
b) break
c) continue
d) All the above
Answer:
a) go to
Question 88.
A …………… statement is a jump statement which terminates the execution of the loop and the control is transferred to resume normal execution after the body of the loop.
a) go to
b) break
c) continue
d) All the above
Answer:
b) break
Question 89.
…………….. statement forces the loop to continue or execute the next iteration,
a) go to
b) break
c) continue
d) All the above statement is executed in
Answer:
c) continue
Question 90.
When the …………….statement is executed in the loop, the code inside the loop following the …………… statement will be skipped and the next iteration of the loop will begin.
a) go to
b) break
c) continue
d) All the above
Answer:
c) continue
Question 91.
……………. statement breaks the iteration.
a) continue
b) break
c) Both A and B
d) None of these
Answer:
b) break
Question 92.
…………… statement skips the iteration.
a) continue
b) break
c) Both A and B
d) None of these
Answer:
a) continue
Question 93.
…………….. statement is used with loops as well as switch case.
a) continue
b) break
c) Both A and B
d) None of these
Answer:
b) break
Question 94.
………….. statement is only used in loops.
a) continue
b) break
c) Both A and B
d) None of these
Answer:
a) continue
Very Short Answers (2 Marks)
Question 1.
What is an if statement? Write its syntax.
Answer:
The if statement evaluates a condition, if the condition is true then a true – block (a statement or set of statements) is executed, otherwise the true – block is skipped. The general syntax of the if the statement is:
if (expression)
true – block;
statement – x;
Question 2.
What are the basic control structures?
Answer:
The basics of control structures are:
- Selection statement
- Iteration statement
- Jump statement
Question 3.
What is a computer program?
Answer:
A computer program is a set of statements or instructions to perform a specific task.
Question 4.
What is nested if? Mention its types.
Answer:
An if statement contains another if the statement is called nested if. The nested can have, one of the following three forms.
- If nested inside if part
- If nested inside else part
- If nested inside both if part and else part
Question 5.
What are control statements?
Answer:
Control statements are statements that alter the sequence of flow of instructions.
In a program, statements may be executed sequentially, selectively, or iteratively. Every programming language provides statements to support sequence, selection (branching), and iteration.
Question 6.
What is the exit condition of a loop?
Answer:
The condition on which the execution or exit from the loop is called exit-condition or test-condition.
Question 7.
What is an iteration or loop?
Answer:
An iteration or loop is a sequence of one or more statements that are repeatedly executed until a condition is satisfied. These statements are also called control flow statements.
Question 8.
What are the advantages of a loop or an iteration?
Answer:
It is used to reduce the length of code, to reduce time, to execute the program, and takes less memory space
Question 9.
What are loop elements?
Answer:
Every loop has four elements that are used for different purposes. These elements are
- Initialization expression
- Test expression
- Update expression
- The body of the loop
Question 10.
What are the parts of a loop?
Answer:
Every loop has four elements that are used for different purposes. These elements are:
- Initialization expression
- Test expression
- Update expression
- The body of the loop
Question 11.
Write a note on the declaration of a variable in a for a loop.
Answer:
Declaration of a variable in a for loop:
In C++, the variables can also be declared within a for loop.
For instance,
for(int i=0; i<=5; ++i)
A variable declared inside the block of main() can be accessed anywhere inside main() i.e., the scope of variable in main().
Question 12.
Write a program to display numbers from 1 to 10 except 6 using a continue statement.
Answer:
C++ program to display numbers from 1 to 10 except 6 using continue statement
#include
using namespace std;
int main()
{
if (i = 6)
continue;
else
cout << i << ” ”
}
return 0;
}
Output:
1 2 3 4 5 7 8 9 10
Question 13.
Write about the go-to statement.
Answer:
go to the statement: The go-to statement is a control statement which is used to transfer the control from one place to another place without any condition in a program.
The syntax of the go-to statement is;
In the syntax above, the label is an identifier. When go-to label; is encountered, the control of the program jumps to label: and executes the code below ¡t.
Question 14.
Write a note on the break statement.
Answer:
break statement: A break statement is a jump statement which terminates the execution of the loop and the control is transferred to resume normal execution after the body of the loop. The following figure shows the working of break statement with looping statements;
Question 15.
Write a note on the continue statement.
Answer:
continue statement:
The continue statement works quite similar to the break statement. Instead of terminating the loop, the continue statement forces the loop to continue or execute the next iteration. When the continue statement is executed in the loop, the code inside the loop following the continue statement will be skipped and the next iteration of the loop will begin.
The following figure describes the working flow of the continue statement
Short Answers 3 Marks
Question 1.
Write a note on the sequence statement.
Answer:
sequence statement:
The sequential statement is the statements, that are executed one after another only once from top to bottom. These statements do not alter the flow of execution. These statements are called sequential flow statements. They always end with a semicolon (;).
Question 2.
What is a selection statement? Mention its types.
Answer:
Selection statement: The selection statement means the statement (s) are executed depends upon a condition. If a condition is true, a true block is executed otherwise a false block is executed. This statement is also called a decision statement or selection statement because it helps in making a decision about which set of statements are to be executed.
Question 3.
Explain the Iteration statement.
Answer:
Iteration statement: The iteration statement is a set of the statement are repetitively executed depends upon conditions. If a condition evaluates to true, the set of statements (true block) is executed again and again. As soon as the condition becomes false, the repetition stops. This is also known as a looping statement or iteration statement.
Question 4.
Explain if statement with syntax and an example.
Answer:
The if statement evaluates a condition, if the condition is true then a true-block is executed, otherwise, the true-block is skipped.
Syntax: if (expression) true-block; statement-x;
In the above syntax, if is a keyword that should contain expression or condition which is enclosed within parentheses. Working of if statement: If the expression is true then the true-block is executed and followed by statement-x is also executed, otherwise, the control passes to statement-x. It is implemented in the following flowchart.
Example 1:
if ( qty> 500)
dis=10;
Example 2:
if(age>=18)
cout<< “\n You are eligible for voting ….”;
Question 5.
Explain if..else statement with syntax and an example.
Answer:
The syntax of the if..else statement:
if (expression)
{
True-block;
}
else
{
False-block;
}
Statement-x
Working of if..else statement:
In if..else statement, first the expression or condition is evaluated as either true or false. If the result is true, then the statements inside the true-block are executed and the false-block is skipped. If the result is false, then the statement inside the false-block is executed i.e., the true-block is skipped. It is implemented in the following flowchart.
Example:
if(num%2==0)
cout<< “\n The given number is Even”;
else
cout<< “\n The given number is Odd”;
Question 6.
What is a conditional or ternary operator? Explain.
Answer:
The conditional operator (or Ternary operator) is an alternative for the ‘if-else statement’. The conditional operator consists of two symbols (?:). It takes three arguments. The control flow of the conditional operator is shown below.
The syntax of the conditional operator is:
expression 1? expression 2: expression 3
In the above syntax, expression 1 is a condition which is evaluated, if the condition is true (Non-zero), then the control is transferred to expression 2, otherwise, the control passes to expression 3.
Example:
#include
using namespace std;
int main()
{
int a, b, largest;
cout << “\n Enter any two numbers:
cin >> a >> b;
largest = (a>b)? a : b;
cout << “\n Largest number: ” << largest;
return 0;
}
Output
Enter any two numbers: 12 98
Largest number: 98
Question 7.
Explain the nested switch statement.
Answer:
Nested switch:
When a switch is a part of the statement sequence of another switch, then it is called a nested switch statement. The inner switch and the outer switch constant may or may not be the same.
The syntax of the nested switch statement is: switch (expression)
{
case constant 1:
statement(s);
break;
switch(expression)
{
case constant 1:
statement(s);
break;
case constant 2:
statement(s);
break;
.
.
.
default: statement(s);
}
case constant 2:
statement(s);
break;
.
.
.
default:
statement(s);
}
Example:
#include
using namespace std;
int main()
{
int a = 8;
cout<<“The Number is : ” < switch (a)
{
case 0 :
cout<<“The number is zero” <<endl;
break;
default:
cout<<“The number is a non-zero
integer” <<endl;
int b = a % 2;
switch (b)
{
case 0:
cout<<“The number is even” <<endl;
break;
case 1:
cout<<“The number is odd” <<endl;
break;
}
}
return 0;
}
Output
The Number is: 8
The number is a non-zero integer
The number is even
Question 8.
Explain variations of for loop?
Answer:
Variations of for loop:
Variations increase the flexibility and applicability of for loop.
Multiple initializations and multiple update expressions:
Multiple statements can be used in the initialization and update expressions of for loop.
This multiple initializations and multiple update expressions are separated by commas.
For example:
Output
The value of i is 0 The value of j is 10
The value of i is 1 The value of j is 9
The value of i is 2 The value of j is 8
The value of i is 3 The value of j is 7
The value of i is 4 The value of j is 6
In the above example, the initialization part contains two variables i and j, and the update expression contains i++ and j++. These two variables are separated by commas which are executed in sequential order i.e., during initialization firstly i-0 followed by j=10. Similarly, in update expression, firstly i++ is evaluated followed by j++ is evaluated.
Question 9.
Explain for loop optional expression with suitable examples.
Answer:
Optional expressions;
The for loop contains three parts, i.e., initialization expressions, test expressions and
update expressions. These three expressions are optional in a for loop.
Case 1:
#include
using namespace std;
int main ()
{
int i, sum=0, n;
cout<<“\n Enter The value of n”;
cin>>n;
i =1;
for (; i<=10;i++)
{
sum += i;
}
cout<<“\n The sum of 1 to” <<n<<“is “<<sum;
return 0;
}
Output
Enter the value of n 5
The sum of 1 to 5 is 15
In the above example, variable i is declared and the sum is initialized at the time of variable declaration.
The variable i is assigned to 0 before the for loop but still, the semicolon is necessary before test expression.
In a for loop, if the initialization expression is absent then the control is transferred to the test expression/conditional part.
Case 2:
#include
using namespace std;
int main ()
{
int i, sum=0, n;
cout<<“\n Enter The value of n”; cin>>n;
i =1;
for (; i<=10; )
{
sum += i;
++i;
cout<<“\n The sum of 1 to” <<n<<“is “<<sum;
return 0;
}
Output
Enter the value of n 5
The sum of 1 to 5 is 15
In the above code, the update expression is hot done, but a semicolon is necessary before the update expression.
In the above code, neither the initialization nor the update expression is done in the for a loop. If both or any one of the expressions are absent then the control is transferred to the conditional part.
Case 3:
An infinite loop will be formed if a test- expression is absent in a for a loop.
For example:
Question 10.
What is an empty loop? Give a suitable example.
Answer:
Empty loop:
An empty loop means a loop that has no statement in its body is called an empty loop. Following for loop is an empty loop:
In the above code, the for loop contains a null statement, it is an empty loop.
Similarly, the following for loop also forms an empty loop.
In the above code, the body of a for loop enclosed by braces is not executed at all because a semicolon is ended after the for a loop.
Question 11.
What do you mean by the nesting of loops?
Answer:
Nesting of loops:
A loop which contains another loop is called a nested loop.
The syntax is given below:
for (initialization(s); test-expression; update expression(s))
{
for (initialization(s); test-expression; update expression(s)
{
statement(s);
}
statement(s);
{
Example:
#include
using namespace std;
int main(void)
{
cout<< “A multiplication table:”
< <<endl<< “” <<endl;
for(int c = 1; c < 10; C++)
{
cout<< c << “l”;
for(int i = 1; i< 10; i++)
{
cout< }
cout<<endl;
}
return 0;
}
Question 12.
What are the Differences between the Break and Continue statement?
Answer:
Break | Continue |
The break is used to terminate the execution of the loop. | Continue is not used to terminate the execution of the loop. |
It breaks the iteration. | It skips the iteration. |
When this statement is executed, control will come out from the loop and executes the statement immediately after the loop. | When this statement is executed, it will not come out of the loop but moves/jumps to the next iteration of the loop. |
The break is used with loops as well as switch cases. | Continue is only used in loops, it is not used in switch cases. |
Book Evaluation
Hands-On Practice
Write a C++ program to solve the following problems:
Question 1.
Temperature – conversion program that gives the user the option of converting Fahrenheit to Celsius or Celsius to Fahrenheit and depending upon the user’s choice.
Answer:
PROGRAM :
using namespace std;
#include<iostream>
int main()
{
float f,c;
int choice;
cout<<“\nl, FahrenheiT to Celsius”;
cout<<“\n2. Celsius to FahrenheiT”;
cout<<“\n3.Exit”;
cout<<“\nENTER YOUR CHOICE “;
cin>>choice;
switch(choice)
{
case 1: cout<<“\nENTER TEMPERATURE IN FAHRENHEIT “;
cin>>f;
c=5.0/9.0 *(f-32);
cout<<“\nTEMPERATURE IN FAHRENHEIT “<<f;
cout«”\nTEMPERATURE IN CELSIUS “<<c;
break;
case 2: cout<<“\nENTER TEMPERATURE IN CELSIUS “;
cin>>c;
f = 9*c/5 + 32;
cout< <“\nTEMPERATURE IN FAHRENHEIT “<<f;
cout< < “\nTEMPERATURE IN CELSIUS “<<c;
break;
default: cout< <“\nPROCESS COMPLETED”; > .
}
}
Output
Question 2.
The program requires the user to enter two numbers and an operator. It then carries out the specified arithmetical operation: addition, subtraction, multiplication, or division of the two numbers. Finally, it displays the result.
using namespace std;
#include <iostream>
int main()
{
float num1,num2,result;
char op;
coutczcz”\nl. Enter two numbers”;
cin>>num1>>num2;
cout<<”\nEnter operaotr + or – or * or / : “; cin>>op;
switch(op)
{
case’+’:
result = num1 + num2;
cout<<“\nAdded value “<<result;
break;
case’-‘:
result = num1 – num2;
cout<<“\nSubtracted value “<<result;
break;
case ‘*’:
result = num1 * num2;
cout<<“\Multiplied value “<<result;
break;
case’/’:
result = num1 / num2;
cout<<“\nDivided value “<<result;
break;
default:cout<<“\nPROCESS COMPLETED”;
}
}
Output
Question 3.
Program to input a character and to print whether a given character is an alphabet, digit or any other character.
Answer:
PROGRAM
using namespace std;
#include <iostream>
#include
int main()
{
char ch;
cout<<“\nl. Enter a character :”; cin>>ch;
if(isdigit(ch))
cout<<“\nThe given character “<<ch<<” is a digit”;
else if(isalpha(ch))
cout<<“\nThe given character “<<ch<<” is an alphabet”;
else
cout<<“\nThe given character “<<ch<<” is other character”;
}
Output
Question 4.
Program to. print whether a given character Is an uppercase or a lowercase character or a digit or any other character. Use ASCII codes for it. The ASCII codes are as given below:
Answer:
Characters -ASCII Range
‘0’ – ‘9’ – 48 – 57
‘A’ – ‘Z’ – 65 – 90
‘a’ – ‘z’ – 97 – 122
other characters 0 – 255 excluding the above-mentioned codes.
PROGRAM
using namespace std;
#include
int main()
{
char ch;
cout<<“\nl. Enter a character: cin>>ch;
int av = ch;
if(av>47 m av<58)
cout<<“\nThe given character “<<ch<<” is a digit”; else if(av>64 && av<91)
cout<<“\nThe given character “<<ch<<” is an Uppercase character”; else if(av>96 && av < 123)
cout<<“\nThe given character “<<ch<<” is n Lowercase character”;
else
cout<<“\nThe given character “<<ch<<” is an Other character”;
}
Output
Question 5.
Program to calculate the factorial of an integer.
Answer:
PROGRAM
using namespace std;
#include
int main()
{
int i,n;
long int fact=l;
cout<<“\nEnter a number… cin>>n; ‘
for(i=l; i<=n; i++)
fact = fact * i;
cout<<“\nFactorial of “<<n<<” is “<<fact;
}
Output
Question 6.
Program that print 1 2 4 8 16 32 64 128.
Answer:
PROGRAM
using namespace std;
#include
int main()
{
int i;
for(i=l; i< =128; i=i*2)
cout<<i<<“\t”;
}
Output
Question 7.
Program to generate divisors of an integer.
Answer:
PROGRAM
using namespace std;
#include
int main() {
int n,d;
cout<<“\n Enter a number…”; cin>>n;
cout<<“\nThe divisors for “< for(d=l; d<=n/2;d++)
if(n%d==0)
cout<<d<<“\t”;
cout<<n;
}
Output
Question 8.
Program to print fibonacci series i.e., 0112 3 5 8
Answer:
PROGRAM
using namespace std;
#include
int main()
{
int n,fl,f2,f3;
fl=-l; ,
f2=l;
cout«”\nHOW MANY TERMS … “; .
cin>>n;
cout<<,r\nThe FIBONACCI TERMS ARE \n”; for(int i=l; i<=h;i++)
{
f3=fl+f2;
cout<<f3<<“\n”;
fl-f2;
f2=f3;
}
}
Output
Question 9.
Programs to produces the following design using nested loops.
Answer:
9. A)
PROGRAM
using namespace std;
#include
int main()
{
int i,j;
for(i=l; i<=6; i++)
{
char c = ‘A’; for(j=l;j<=i;j++)
{
cout<<c<<“\t”;
C++;
}
cout<<“\n\n”;
}
}
Output
9. B)
PROGRAM
using namespace std;
#include
int main()
{
int i,j;
for(i=l; i<=5; i++) { for(j=5;j>=i;j–)
cout<<j<<“\t”;
cout<<“\n\n”;
}
}
Output
9.c)
PROGRAM
using namespace std;
#include
int main()
{
int i,j,k=0,m;
for(i=7; i> = l; i=i-2)
{
for(j=l;j<=i;j++)
cout<<“#\t”;
cout<<“\n\n”;
k++;
for(m=l;m<=k;m++)
cout<<“\t”;
}
}
Output
Question 10.
Program to check whether the square root of a number is a prime or not.
Answer:
PROGRAM
using namespace std;
#include
#include int main()
{
int n,srn,d,p=0;
cout<<“\nENTER A NUMBER … cin>>n;
srn=sqrt(n);
cout<<“\nTHE GIVEN NUMBER IS “<<n;
cout<<“\nTHE SQUARE ROOT OF”<<n<<” IS”<<srn;
for(d=2; d<=srn/2;d++)
{
if(srn%d==0)
{
p=1;
break;
}
}
if(p==0)
cout <<“\nTHE SQUARE ROOT OF THE GIVEN NUMBER “<<n<<” IS A PRIME”; else
cout <<“\nTHE SQUARE ROOT OF THE GIVEN NUMBER “<<n<<” IS NOT A PRIME”;
}
Output