Exam 6: Looping
Use a(n) ____ loop to execute a body of statements continually as long as the Boolean expression that controls entry into the loop continues to be true .
B
How are indefinite loops used for validating data? Why is a loop structure typically the better choice for validating data than an if statement?
Programmers commonly use indefinite loops when validating input data. Validating data is the process of ensuring that a value falls within a specified range. For example, suppose you require a user to enter a value no greater than 3. If the user enters 3 or less at the first prompt, the loop never executes. However, if the user enters a number greater than 3, the shaded loop executes, providing the user with another chance to enter a correct value. While the user continues to enter incorrect data, the loop repeats. Novice programmers often make the mistake of checking for invalid data using a decision instead of a loop. That is, they ask whether the data is invalid using an if statement; if the data is invalid, they reprompt the user. However, they forget that a user might enter incorrect data multiple times. Usually, a loop is the best structure to use when validating input data.
When creating a for loop, which statement will correctly initialize more than one variable?
You can use virtually any number of nested loops; however, at some point, your machine will no longer be able to store all the necessary looping information.
In the expressions b = 8 and c = --b , what value will be assigned to the variable c ?
When nesting loops, the variable in the outer loop changes more frequently.
You can initialize one or more variables in the first section of a for statement.
Making a comparison to 0 is slower than making a comparison to any other value.
The statements that make up a loop body will continue to execute as long as the expression value remains false.
What are the three sections inside the parentheses of a for loop typically used for?
In a do…while loop, the loop will continue to execute until ____.
It is important that the loop control ____ be altered within the body of the loop.
How could you rewrite the following code to have the arithmetic performed only once, even if the loop executes 1,000 times?
while (x < a + b)
// loop body
Filters
- Essay(0)
- Multiple Choice(0)
- Short Answer(0)
- True False(0)
- Matching(0)