Which control statement which is best used when you know how many times to repea
ID: 3843105 • Letter: W
Question
Which control statement which is best used when you know how many times to repeat the execution of a group of statements? The control statement which is best used when you need to choose which statements to execute from among any choices, and the condition for selection can be expressed as some expression is not necessarily integer? The control statement which is best used when you want to repeat a group of statements over and over based a condition and the statements in the loop body must be executed at least once is the? Use the following program SEGMENT to answer the next two questions int I, J; for (I = 2; I >= 0; I--) {for(J = 1; JExplanation / Answer
Answer:
9. A. The for loop statement
The for loop statement is the best used when you know how many times to repeat the execution of a group of statements.
A for loop is a control statement for repetition that allows to write a loop that executes a specific number of times.
Syntax
for ( init; condition; increment ) {
statement(s);
}
The init step is executed first, and only once & allows to initialize any loop control variables.
Next is the condition evaluation. If condition is true, the body of the loop is executed. If false, loop does not execute.
Then the flow control jumps back up to the increment or decrement statement.
10. D. the switch statement
The control statement which is best used when you need to choose which statements to execute from among many choices, and the condition for selection can be expressed as some expression is not necessarily integer the switch statement.
Because the expression which is used in a switch statement must be an integral or enumerated type, or a class type in which the class has to be a single conversion function to an integral or enumerated type.
11. . . C. the do/while statement
The control statement which is best used when you want to repeat a group of statements over and over based on a condition and the statements in the loop body must be executed at least once is the . the do/while statement .
Because do...while loop checks its condition at the end of the loop.
12.
D. none of these are correct
#include<iostream>
using namespace std;
int main()
{ int i, j;
for (i=2; i>=0; i--)
{ for (j=1; j<4; j++)
{
// cout << j << ", " << i << ",";
cout << "j=" << j << ", " <<"i="<< i << ",";
cout << endl;
}
}
return 0;
}
Output:
j=1, i=2,
j=2, i=2,
j=3, i=2,
j=1, i=1,
j=2, i=1,
j=3, i=1,
j=1, i=0,
j=2, i=0,
j=3, i=0,
So, the statement cout << j << ", " << i << ","; is executed 9 times. According to the output for each i value how the j value is being changed with the conditioni , is provided.
13. D. None of these answer is correct
#include<iostream>
using namespace std;
int main()
{ int i, j;
for (i=2; i>=0; i--)
{ for (j=1; j<4; j++)
{
// cout << j << ", " << i << ",";
cout << "j=" << j << ", " <<"i="<< i << ",";
cout << endl<< "*";
}
}
return 0;
}
Output:
j=1, i=2,
*j=2, i=2,
*j=3, i=2,
*j=1, i=1,
*j=2, i=1,
*j=3, i=1,
*j=1, i=0,
*j=2, i=0,
*j=3, i=0,
*
Here the “*” symbol is given to identify how many times the cout << endl ; statement is executed. So in the above output the number of * is 9. So the cout <<endl<<”*” ; or cout << endl ; statement executed 9 times.
14. B. FALSE
A do while loop repeats the loop body zero or more times is False.
Notice that the condition is tested at the end of the block instead of the beginning, so the block will be executed at least once. If the condition is true, we jump back to the beginning of the block and execute it again. A do..while loop says "Execute this block of code, and then continue to loop while the condition is true".
15. A.TRUE
eof controlled input loops require a prior knowledge of how many data are in the file: A.TRUE
End-of-file controlled loops - This type of while loop is usually used when reading from a file. The loop terminates when the end of the file is detected. This can easily be determined by checking the EOF()function after each read from the file. This function will return true when the end-of-file is reached.
In case of EOF controlled loop , whenever open the file then perform the read operation primarily, then implicitly updating the loop condition which means if the loop can not read any data from file it is impossible to reach to EOF.
16. A. TRUE
sentinal controlled input loops require a prior knowledge of how many data are in the file:
In sentinal controlled input loops open the file is in priority to prime a read the data & the update according to the condition when a input is read with each iteration.
17. D. # include <fstream>
Because # include <fstream> is signifying to the Input/output file stream class .
18. A. ifstream MyInput("exam.txt");
ifstream MyInput("exam.txt");statements correctly declares the input file object MyInput and initializes the file object to read the file exam.txt
Because ifstream is signifying to the Input file stream class .
19. . D. You open the file, and associate it with a specific file name
The next step in using a file, after the file has been declared with the following statement.
ofstream AnsFile;
. D. You open the file, and associate it with a specific file name
Because ofstream is signifying to the Output file stream (class )
20. A. AnsFile.open("myanswers.txt");
The statements AnsFile.open("myanswers.txt correctly opens the output file object AnsFile to store answers into file myanswers.txt
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.