Chapter 2 Introduction to Java Applications 85 Good (Web hosting plans)
Chapter 2 Introduction to Java Applications 85 Good Programming Practice 2.17 Indent the statement i7n the body of an ifstructure to make the body of the structure stand out and to enhance program readability. Good Programming Practice 2.18 Place only one statement per line in a program. This format enhances program readability In the preceding ifstructure, if the values of variables number1 and number2 are equal, line 35 assigns to result the value of result + number1 + “==” + number2. As discussed in Fig. 2.9, the + operator in this expression performs string concatenation. For this discussion, we assume that each of the variables number1 and number2 has the value 123. First, the expression converts number1 s value to a string and appends it to result (which currently contains the empty string) to produce the string “123″. Next, the expression appends “==”to “123″to produce the string “123==”. Finally, the expression appends number2 to “123==” to produce the string “123==123″. The Stringresult becomes longer as the program proceeds through the if structures and performs more concatenations. For example, given the value 123for both number1 and number2 in this discussion, the if conditions at lines 46 47 (<=) and 49 50 (>=) are also true. So, the program displays the result 123 == 123 123 <= 123 123 >= 123 in a message dialog. Common Programming Error 2.15 Replacing operator == in the condition of an ifstructure, such as if(x==1), with operator =, as in if(x=1), is a syntax error. Common Programming Error 2.16 Forgetting the left and right parentheses for the condition in an if structure is a syntax error. The parentheses are required. Notice that there is no semicolon (;) at the end of the first line of each if structure. Such a semicolon would result in a logic error at execution time. For example, if ( number1 == number2 ); // logic error result = result + number1 + ” == ” + number2; would actually be interpreted by Java as if ( number1 == number2 ) ; result = result + number1 + ” == ” + number2; where the semicolon on the line by itself called the empty statement is the statement to execute if the condition in the ifstructure is true. When the empty statement executes, no task is performed in the program. The program then continues with the assignment statement, which executes regardless of whether the condition is true or false. Copyright 1992 2002 by Deitel & Associates, Inc. All Rights Reserved. 7/2/01
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services