114 Control Structures: Part 1 Chapter 4 structure. (Windows 2003 server web)

114 Control Structures: Part 1 Chapter 4 structure. This value is used to determine if the program s flow of control should enter the body of the while structure. If the while structure condition is false (i.e., the user has entered the sentinel value), the body of the while structure does not execute (i.e., no grades were entered). If, on the other hand, the condition is true, the body begins execution, and the value input by the user is processed (added to the total). Then, the next value is input from the user before the end of the while structure s body. When program control reaches the closing right brace (}) of the body (line 39), execution continues with the next test of the while structure condition. The new value input by the user determines if the while structure s body should execute again. Notice that the next value is input from the user immediately before the while structure condition is evaluated (line 37). This allows the program to determine whether the value just input by the user is the sentinel value before the program processes that value as a valid grade. If the value is the sentinel value, the while structure terminates, and the value is not added to the total. Notice the block that composes the while loop in Fig. 4.9. Without the braces, the last three statements in the body of the loop would be outside the loop, causing the computer to interpret the code incorrectly, as follows: while ( gradeValue != -1 ) // add gradeValue to total total = total + gradeValue; // add 1 to gradeCounter gradeCounter = gradeCounter + 1; // prompt for input and read grade from user Console.Write( “Enter Integer Grade, -1 to Quit: ” ); gradeValue = Int32.Parse( Console.ReadLine() ); An infinite loop occurs in the program if the user fails to input the sentinel -1 as the input value at line 23 (before the while structure). Common Programming Error 4.6 Omitting the curly braces that delimit a block in a repetition structure can lead to logic errors, such as infinite loops. Good Programming Practice 4.6 In a sentinel-controlled loop, the prompts requesting data entry should remind the user of the sentinel value. Averages do not always evaluate to integer values. Often, an average is a value such as 3.333 or 2.7, that contains a fractional part. These values are floating-point numbers and usually are represented by the data type double. We declare the variable average as type double to capture the fractional result of our calculation. However, the result of the calculation total / gradeCounter is an integer because total and grade- Counter are both integer variables. Dividing two integers results in integer division, in which any fractional part of the calculation is truncated and the result is a whole number. The calculation is performed first, thus the fractional part is lost before the result is assigned to average. To produce a floating-point calculation with integer values, we must create temporary values that are floating-point numbers for the calculation. C# provides the unary
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision virtual web hosting services

Leave a Reply