An endless source of amusement for computer scientists is the observation that the directions on shampoo, Lather, rinse, repeat, are an infinite loop. In the case of countdown , we can prove that the loop terminates because we know that the value of n is finite, and we can see that the value of n gets smaller each time through the loop, so eventually we have to get to 0. In other cases, it is not so easy to tell.
Look at the following function, defined for all positive integers n :. The condition for this loop is n! Each time through the loop, the program outputs the value of n and then checks whether it is even or odd. If it is even, the value of n is divided by 2. For example, if the starting value the argument passed to sequence is 3, the resulting sequence is 3, 10, 5, 16, 8, 4, 2, 1.
Since n sometimes increases and sometimes decreases, there is no obvious proof that n will ever reach 1, or that the program terminates. For some particular values of n , we can prove termination. For example, if the starting value is a power of two, then the value of n will be even each time through the loop until it reaches 1. The previous example ends with such a sequence, starting with Particular values aside, the interesting question is whether we can prove that this program terminates for all values of n.
So far, no one has been able to prove it or disprove it! To write effective computer programs a programmer needs to develop the ability to trace the execution of a computer program. Tracing involves becoming the computer and following the flow of execution through a sample program run, recording the state of all variables and any output the program generates after each instruction is executed.
At the start of the trace, we have a local variable, n the parameter , with an initial value of 3. Since 3 is not equal to 1, the while loop body is executed. To keep track of all this as you hand trace a program, make a column heading on a piece of paper for each variable created as the program runs and another one for output. Our trace so far would look something like this:.
Since 10! By the end of the trace we have:. From this trace we can learn a lot about the way our code works. We can observe that as soon as n becomes a power of 2, for example, the program will require log 2 n executions of the loop body to complete. We can also see that the final 1 will not be printed as output. The following function counts the number of decimal digits in a positive integer expressed in decimal format:. Trace the execution of this function call to convince yourself that it works.
This function demonstrates another pattern of computation called a counter. The variable count is initialized to 0 and then incremented each time the loop body is executed. When the loop exits, count contains the result — the total number of times the loop body was executed, which is the same as the number of digits. If we wanted to only count digits that are either 0 or 5, adding a conditional before incrementing the counter will do the trick:. The increment value does not have to be One of the things loops are good for is generating tabular data.
Before computers were readily available, people had to calculate logarithms, sines and cosines, and other mathematical functions by hand. To make that easier, mathematics books contained long tables listing the values of these functions. Creating the tables was slow and boring, and they tended to be full of errors. When computers appeared on the scene, one of the initial reactions was, This is great! We can use the computers to generate the tables, so there will be no errors. That turned out to be true mostly but shortsighted.
Typically used to initialize a counter variable. This expression may optionally declare new variables with var or let keywords. Variables declared with var are not local to the loop, i.
Variables declared with let are local to the statement. An expression to be evaluated before each loop iteration.
If this expression evaluates to true , statement is executed. This conditional test is optional. Cloud Computing. Data Science. Angular 7. Machine Learning. Data Structures. Operating System. Computer Network. Compiler Design. Computer Organization. Discrete Mathematics. Ethical Hacking. To do this, lets consider the following program, which declares two variables, count which is an integer, and letter which is a character.
Neither variable is pre-initialized. The value of each variable is printed out using a printf statement.
0コメント