Wednesday, 15 December 2021

UNIT 04. CONTROL STRUCTURE - Text Book Exercise And Question Answers - Computer Science For Class X

GO TO INDEX

Computer Science For Class X
Unit 04: Control Structure
Question Answers

By Mrs. Ayesha Arif
Vice Principal
(Jauhar Progressive School)


DETAILED QUESTION –ANSWERS


Q No.1: What are control statements?
Ans: CONTROL STATEMENTS:
Control Statements are the statements that can control the flow of a program. They control the direction of program by repeating any block of code, making a choice or simply transfer the control to any specific block of program. They help the programmers to decide which part of code is executed at certain time.

TYPES OF CONTROL STATEMENTS
  1. C++ has three types of control statements:
  2. Selection/Decision Making Structure
  3. Iteration / Loops
  4. Jump

Q No.2: Write a detailed note on Selection / Decision making structures.
Ans: SELECTION /DECISION MAKING STRUCTURE:
These structures decide whether a certain part of code is executed or not. C++ has three decision making structures.
  1. ‘If ’ statement
  2. ‘if-else’ statement
  3. ‘switch’ statement

1. ‘if’ statement:
  • It is the basic decision statement.
  • The structure contains “if” keyword followed by a conditional expression in parenthesis and then its body of statement(s) is also called if block.
  • If there are more than one statement in “if” block or body we enclose all of them in braces.
  • It tests a particular condition. Whenever a condition is evaluated as true an action is performed.
  • But if condition is false it leaves the statements in “if” block and starts executing statements after “if” block.


 Syntax:

 if (test condition)
 {
 Statement(s);




Nested if statement:
  • An ‘if’ statement which is part of block of another ‘if’ statement is called nested ‘if’.
  • The inner ‘if’ statement will only be tested if the outer ‘if’ is true.

2. “If –else” statement:
  • The structure of if-else contains "if" keyword followed by a conditional expression in parenthesis and then its body of statement(s) then "else" keyword and its body of statement(s).
  • It checks the conditions.
  • When the condition is true the statement in if block performs an indicated action.
  • Otherwise the action is skipped, i.e. in case of false condition, it executes statements in 'else' block.
  • 'if-else' structure performs certain action when the condition is true and some different action when the condition is false, i.e. 'if' block statements or 'else' block statements must execute.


 Syntax:

 if (test condition)
 {
 Statement(s);
 }
 Else
 {
 Statement(s);
 }




'else-if' statement Or Nested 'else-if' statement:
  • This statement test for multiple conditions by placing 'if-else' statement inside 'if–else' statements.
  • When a condition is evaluated as true , the corresponding statements are executed and the rest of the structure is skipped. This structure is referred to as if–else-if-ladder.

3. Switch statement:
The 'switch' statement starts with "switch" keyword followed by a variable or expression in parenthesis, than a block of switch statement in braces.
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
The following rules apply to a switch statement:
  • The expression used in a switch statement must have an integer or character constant.
  • We can have any number of case statements within a switch. Each case is followed by the value to be compared to and then a colon.
  • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant.
  • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
  • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
  • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
  • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.


 Syntax:

 switch(expression)
 {
 case constant 1:
 statement(s)
 break;
 case constant 2:
 statement(s)
 break;
:   :   :
 default:
 statement(s)
 }




Q No.3: What do you know about Iteration /loop?
Ans: LOOP:
A loop is used for executing a block of statements repeatedly until a particular condition is satisfied.

ITERATION:
Ans: A loop repeats code until a certain condition is met. Each time the computer runs through a loop, it's referred to as an iteration. Many computer programs and programming languages use iterations to perform specific tasks, solve problems, and present solutions.

TYPES OF LOOPS IN C++:
There are three types of loops in C++
  1. for
  2. while
  3. do-while

1. "for" Loop:
  • It is used to repeat a statement or a block of statements or a sequence of statements for a specified number of times.
  • It is a pre-test loop as condition is tested at the start of loop.
  • It starts with keyword “for” followed by loop expression parenthesis.
  • Loop expression has three parts separated by semicolon.
    (i) Initialization: This is the first part which initializes loop variable. It is executed only once.
    (ii) Test Expression: This is the second part where loop variable is tested. If it is true it enters the loop otherwise transfers control to statement after loop. If it enters in loop then after executing all statements in loop block.
    (iii) Increment Or decrement: It shows decrement or increment in loop variable . It is also called counter controlled loop or definite repetition loop since the number of iterations is known before loop execution.
  • The body of the loop may have one or more statements. If there are more than one statements then they are enclosed in braces.


 Syntax:
 for(initialization; condition testing; increment/decrement)
 {
 statement(s);
 }



2. “while” loop:
  • This loop allows a programmer to specify that an action is to be repeated while condition remains true.
  • It tests the condition at the start of loop before executing the body of loop.
  • It is used when the exact number of loop repetitions before the loop execution begins are not known. Therefore it is also called indefinite repetition loop.
  • It is also a pre test loop.
  • It starts with a keyword “while” followed by a conditional expression like if statement in the parenthesis. Then comes the body of the loop which may have one or more statements in braces.
  • When loop starts, it tests a condition, if it is true it enters in the loop otherwise transfers control to statement after loop.
  • If it enters in loop then after executing all statements in loop block again condition is tested.


 Syntax:
 Initialization;
 while(condition)//condition=true
 {
 statement(s);
 Increment/decrement;
 }



3. “do while” loop:
  • This loop is similar to while loop with one major difference. It tests the condition at the end of the loop body so it is also called a post-test loop.
  • The do-while loop is executed at least once. Only then, the test expression is evaluated.
  • For second time condition is tested.
  • It starts with keyword "do" followed by a body of loop which may have one or more statement in braces. Then "while" keyword andconditional expression in parenthesis.
  • It must be terminated with semi colon.
  • When loop starts it executes statement in block once and then tests the condition, after “while” keyword. If it is true it enters the loop again otherwise transfers control to the next statement after loop. It is also indefinite repetition loop.


 Syntax:
 do
 {
 Statement 1;
 Statement 2;
 Statement 3;
 :
 }
 while (condition);   




Nested Loops:
  • We can use one or more loop inside any another ‘for’, ’while’ or ‘do while’ loop. If a loop exists in the body of another loop then it is called nested loop.
  • The inner nested loop is completely executed every time for each repetition of outer loop.

Q No. 4: Differentiate between:
a) "for", "while" and "do-while" loop structures
b) “if ”, “if-else” and “switch” statements (structures)

Ans: DIFFERENCE BETWEEN FOR,WHILE AND DO WHILE STRUCTURES

DIFFERENCE BETWEEN If , if else and switch structures
S.NO. “if ” “if-else” loop “switch” loop
1. "If" statement is used to select among two alternatives. "If-else" statement is used to select among two alternatives  The "switch" statement is used to select among multiple alternatives.
2. It can have values based on constraints. It can have values based on constraints. Switch can have values based on user choice.
3. It implements Linear search. It implements Linear search. Switch implements Binary search.
4. "if" statement checks a condition using rational and other operators. if-else" statement checks a condition using rational and other operators. "switch" statement checks 'switch' variable value equal to constant that follows case statement.
5. If the condition is true the statement in if block is executed. If condition is false, it leaves the statement in "if" block and start executing statements after "if" block.  If the condition is true the statement in if block is executed and in case of false condition it executes statement in 'else' block. If the switch expression matches any value of constant the control is transferred to that case and all statements after colon are executed. If it doesn’t match any of the case constants control goes to keyword default.


Q No.5: What is Jump statement?
Ans: JUMP STATEMENT:
Jump statements change execution of program from its normal sequence. There are five jump statements used in C++.
  1. break
  2. continue
  3. return
  4. exit
  5. goto

i. BREAK:
  • It can be used in both switch and loop statements.
  • A break causes the switch or loop statements to terminate the moment it is executed.
  • When a break statement is encountered , it terminates the block and gets the control out of the switch or loop.
  • A break causes the innermost enclosing loop or switch to be exited immediately.

ii. CONTINUE:
  • It can appear in only loop (for, while ,do) statements.
  • This statement is used to skip the remaining statements of its body in loop that appear after continue.
  • It immediately transfer control to the top of the loop i.e. first statement.
  • When a continue statement is encountered , it gets the control to the next iteration of the loop.
  • A continue inside a loop nested within a switch causes the next loop iteration.

iii. GOTO:
  • This is used to jump or transfer control unconditionally from "goto" to a labelled statement in the same function.
  • A labelled statement is any identifier followed by a colon.
  • It is not advised to use "goto" statements in programs.
e.g.

iv. RETURN:
  • It terminates the execution of a function and transfers program control to the statement just after the function call statement in the calling function (or to the operating system if we transfer control to the main function).
  • It can also return a value from the current function, if the return type is not "void".


 Syntax:
 return [expression/value];

The value of the expression clause returned to the calling function.

v. EXIT:
  • The exit() is used to terminate C++ program and closes program whenever executed.
  • It is defined under “stdlib.h” header file.


 Syntax:
 void exit (int);


Source: Special Thanks To Sir Syed Arif Ali

B. RESPOND THE FOLLOWING:

Answer the following questions:
Q.1: What is the Purpose of Default Statement in C++?
Ans: Purpose Of Default Statement:
The purpose of the default in the switch case in C++ is to provide an alternative option, in case the conditions specified to trigger the execution of code within the other options do not occur so that the program will not crash.
OR
A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.

Q.2. Can we use "while" loop in place of "for" loop, if yes, then how?
Ans: Yes, we can use while loop in place of for loop in C++. Initialization of loop can be done outside of while body, testing condition can be placed in while condition and increment or decrement usually done within the while body. Here is an example of for and while loop.


Q.3: What Is the main difference between "while" and "do while" loops?
Ans: Difference Between "While loop" and "Do While Loop":
S.NO. While Loop Do While Loop
1. It is pre-test loop because condition is tested at the start of loop. It is post-test loop because condition is tested at the end of loop which executes loop at least once.
2. It is known as entry controlled loop. It is known as exit controlled loop.
3.  If the condition is not true first time then control will never enter in a loop. Even if the condition is not true for the first time the control will enter in a loop.
4. There is no semicolon; after the condition in the syntax of the while loop. There is a semicolon (;) after the condition in the syntax of the do while loop.
5. Initialization and updating is not the part of the syntax. Initialization and updating is not the part of the syntax.

OR
Ans: Difference Between "While loop" and "Do While Loop":
S.NO. While Loop Do While Loop
1. The controlling condition is declared first in this loop. The controlling condition is declared at last in this loop.
2. If the condition is false it will not run. If the condition is false it will run once..
3. Semicolon is not used in this loop Semicolon is used at last in this loop
4. While loop is entry controlled loop. Do while loop is exit controlled loop.
5. | Such that


Q.4: Write the function of "for loop"?
Ans: Function Of for Loop:
A for loop is a repetition or iteration control structure that repeats a statement or block of statements for a specified number of times. The for-loop statement includes the initialization of the counter, the condition, and the increment. The for loop is commonly used when the number of iterations is known.

Q.5. Why we make block of statements using braces?
Ans: We make block of statements by using braces around all statements, even single statements, when they are part of a control structure, such as an if-else or for statement. This makes it easier to add statements without accidentally introducing bugs due to forgetting to add braces.

Q.6. Which data type variables can be used in "switch" statement?
Ans: Only "int" and "char" data types are used in switch statement.

Q.7: What is the purpose of Jump statements?
Ans: Jump Statements:
Jump statements change the normal execution of a program from its normal sequence, and jump over the specific part of program.
Following are the jump statements in C++
  1. Break
  2. Continue
  3. Goto
  4. Exit () oR Return

Q.8: What is the Purpose of the Following?
1) Else if
2) Switch
3) Goto
4) Exit Or Return

Ans:
1) else-if:
The else if statement is used when we have multiple conditions in the if-else structure. We can say if one condition is not met then we have an else-if condition to check other conditions.
Or
Nested if-else statements test for multiple conditions by placing if-else statements inside if-else statements. When a condition is evaluated as true, the corresponding statements are executed and rest of the structure is skipped. This structure is also referred as the else-if ladder.

2) Switch:
The switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed.
OR
Switch statement is a control statement that allows to select only one choice among the many given choices. The expression in switch evaluates to return an integer or character value, which is then compared to the values present in different cases. It executes that block of codes which matches the case value. If there is no match, then default block is executed (if present).

3) Go to:
A go to statement jumps or transfers control unconditionally from the “go to” to a labeled statement in the same function. A labeled statement is an identifier with a colon(:).
OR
In C++ programming, the goto statement is used for altering the normal sequence of program execution by transferring control to some other parts of the program.

4) Exit:
The exit() is used to terminate a C++ program and closes the program whenever executed.
OR
return: The exit function, declared in <stdlib .h>, terminates a C++ program. The value supplied as an argument to exit is returned to the operating system as the program's return code or exit code.

Q.3: Match the column?
S.NO. A S.NO. B S.NO.
(a) if(i) relational eperator.(a) (vi)
(b) loop(ii) break(b) (v)
(c) Conditional expression(iii) Switch(c) (i)
(d) Loops and switch(iv) operator(d) (ii)
(e) do(v) iteration(e) (vii)
(f) >>(vi) else(f) (iv)
(g) case(vii) while(g) (iii)



13 comments:

  1. I don't see the Exercise B questions from book please update

    ReplyDelete
  2. Plzz uploaded respond the following

    ReplyDelete
    Replies
    1. Education is the key to success6 April 2022 at 08:42

      Ok, I will fwd your req.

      Delete
  3. Upload respond the following fast as you can!

    ReplyDelete
  4. Plzz upload respond the following

    ReplyDelete
  5. PLEASE UPLOAD THE EXERCISE B QUESTIONS FROM BOOK I REALLY NEED THEM PLEASE !

    ReplyDelete
  6. Notes achay ha

    ReplyDelete
  7. Respond the following?

    ReplyDelete
  8. Ads are irritating and the data isn't sequenced. I don't understand any thing.

    ReplyDelete