Chapter # 4 Class X
Chapter#4 Control Structure
Q#1: What is the purpose of “Default” statements in C++?
Ans: The purpose of the default in the switch case is to provide an option in case the conditions specified to trigger the execution of code within the other options does not occur so that the program will not crash.
{
default: //This body is evaluated if none of the case statement are equivalent to the expression.
}
Q#2: Can we use while loop in place of for loop? If yes, then how?
Ans: YES. But in most of the cases it’s advisable to use the loops appropriately.
for(i=1 ; i<=n ; ++i)
{
sum = sum + i;
}
2. WHILE LOOP:
i=0;
while(i<=n)
{
sum = sum + 1;
i++;
}
We can clearly see the difference in both the loops. The for loop is most suited for such sort of operations where the number of iterations is already known.
Q#3: What is the main difference between while and do while loop?
Ans:
Ans: A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax:
The syntax of a for loop in C++ is
for ( init; condition; increment )
{
statement(s);
}
Ans: Different programming languages have various ways to delineate the start and end points of a programming structure, such as a loop, method or conditional statement. But in, Java and C++ curly braces are used to define the start and end of a code block.
Q6: Which data type variable can be used in “switch” statements?
Ans: The variable used in a switch statement can only be a short, byte, int or char. The values for each case must be the same data type as the variable type.
Example code.
char choice;
switch(choice)
{
case 'Y' : cout << "Yes"; break;
case 'M' : cout << "Maybe"; break;
case 'N' : cout << "No"; break;
default: cout << "Invalid response";
}
Q#7: What is the purpose of jump statement?
Ans: Jump statements are used to manipulate the flow of the program if some conditions are met. It is used to terminate or continues the loop inside a program or to stop the execution of a function. In C++ there are four jump statements: continue, break, return, and goto.
Q#8: What are the purpose of following statements?
1) If else 2) Switch 3) Go to 4) Exit
Ans: 1) If else The if/else statement executes a block of code if a specified condition is true.
2) Switch A switch statement is a type of selection control mechanism used to allow the value of a variable or expression to change the control flow of program execution via search and map.
3) Go to The go to statement is used to alter the flow of control in a program. Although the go to statement can be used to create loops with finite repetition times.
4) Exit The exit function calls all functions registered with at exit and terminates the program.
Past Papers Question
Q#1: Write down the purpose of any one of the following. (2023)
i) if ii) goto iii) cin
Ans:
1) If: The if statement executes a block of code if a specified condition is true.
2) go to: The go to statement is used to alter the flow of control in a program. Although the go to statement can be used to create loops with finite repetition times.
3) cin: cin is a predefined object that reads data from the keyboard with the extraction operator (>>). This operator allows you to accept data from standard input devices.
Q#2: Write down the purpose of any two statements. (2022)
i) if-else ii) return iii) exit() iv) continue
Ans: 1) If else: The if/else statement executes a block of code if a specified condition is true.
2) 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. It can also return a value from the current function.
3) Exit: The exit function calls all functions registered with at exit and terminates the program.
4) continue: It causes the loop to skip the remaining statements of its body and immediately transfers control to the top of the loop.
Q#3: Explain the purpose of default in C++. (2022)
Ans: The purpose of the default in the switch case is to provide an option in case the conditions specified to trigger the execution of code within the other options does not occur so that the program will not crash.
Q#4: Write the functions of for loop. (2022)
Ans: A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax: The syntax of a for loop in C++ is
for ( initialization; condition; increment )
{
statement(s);
}
Q#5: Write down purpose and syntax for the following loops. (2023)
i) For ii) While iii) do-while
Comments
Post a Comment