How are indefinite loops used for validating data? Why is a loop structure typically the better choice for validating data than an if statement?

What will be an ideal response?


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 anifstatement; 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.

Computer Science & Information Technology

You might also like to view...

The insertion point is the same as the I-bar.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology

Answer the following statements true (T) or false (F)

a) When String objects are compared using ==, the result is true if the Strings contain the same values. b) A String can be modified after it’s created.

Computer Science & Information Technology

What would the following code snippet display?

``` var x = 5; if(x == 4) document.write("Math is fun!"); else document.write("Wrong!
"); document.write("Math is not fun!"); ``` a. Math is fun! b. Math is fun! Math is not fun! c. Wrong! d. Wrong! Math is not fun!

Computer Science & Information Technology

Which of the following will occur on a network with redundant links that does NOT use STP?

A) A frame will loop forever on the network. B) The frame will transmit until a countdown timer is hit. C) The frame will transmit once and may not reach its destination. D) The frame will connect correctly as long as the redundancy is limited.

Computer Science & Information Technology