1 How are indefinite loops used for validating data Why is a
1. How are indefinite loops used for validating data? Why is a loop structure typically the better choice for validating data than an if statement?
2. while(10 > 1){ System.out.println(\"This prints forever.\"); }
Identify the problem that exists in the above while loop.
3. Why would a programmer use curly braces in the body of a do…while loop?
Solution
1) Every loop executes for some predefined number of times, say n. If n is not defined then we call that loop as infinite loop or indefinite loop, however both are not same.
Infinite loops executes for ever.
Indefinite loop executes for unknown number of times.
An example of indefinite loop is as below:
ch=y;
while(ch==\'y\')
{
printf(\"Hello... we are testing..\");
printf(\"Do you want to print the above line again (Y/N) : \");
scanf(\"%c\",ch);
}
It is very much idefinite that how many times the string is printed. As long as user type y the sentence will be printed. This loop will terminate when the user inputs n. Thus, it clearly exihibits how data validation is happening in indefinite loops.
Since, \"if\" increases the number of instructions it is always desirable to use loops.
2.
In the given code segment, the while condition is comparing two constant values. And since that condition will always be true the loop will go for infinite number of times. Making use of constants in condition evaluation is always risky.
3.
do.. while is also a loop that can have any number of instructions. To specify the beginning and ending of the body curly braces are used.
