Archive for April, 2007

106 Control Structures: Part 1 Chapter 4 product (Web hosting company)

Saturday, April 21st, 2007

106 Control Structures: Part 1 Chapter 4 product <= 1000 product = 2 * product true false Fig. 4.5 Flowcharting the while repetition structure. Testing and Debugging Tip 4.2 Visual Studio .NET will not color a keyword properly unless that keyword is spelled correctly and with the correct case. Imagine, again, a deep bin of empty while structures that may be stacked and nested with other control structures to form a structured implementation of an algorithm s flow of control. The empty rectangles and diamonds are filled with appropriate actions and decisions. The flowchart clearly shows the repetition. The flowline emerging from the rectangle indicates that program control continues with the decision, which is tested during each iteration of the loop until the decision eventually becomes false. At this point, the while structure terminates, and control passes to the next statement following the while structure in the program. When the while structure begins executing, product is 2. Variable product is repeatedly multiplied by 2, taking on the values 4, 8, 16, 32, 64, 128, 256, 512 and 1024, successively. When product becomes 1024, the condition product <= 1000 in the while structure becomes false. This terminates the repetition with 1024 as product s final value. Execution continues with the next statement after the while. [Note: If a while structure s condition is initially false, the body statement(s) will never be executed.] 4.8 Formulating Algorithms: Case Study 1 (Counter-ControlledRepetition) To illustrate how algorithms are developed, we solve several variations of a class-averaging problem. Consider the following problem statement: A class of ten students took a quiz. The grades (integers in the range 0 to 100) for this quiz are available to you. Determine the class average on the quiz. The class average is equal to the sum of the grades divided by the number of students. The algorithm for solving this problem on a computer must input each of the grades, perform the averaging calculation and display the result. Let us use pseudocode to list the actions to execute and to specify the order of execution. We use counter-controlled repetition to input the grades one at a time. This technique uses a variable called a counter to control the number of times a set of statements will execute. In this example, repetition terminates when the counter exceeds 10. This section presents a pseudocode algorithm (Fig. 4.6) and the corresponding program (Fig. 4.7). In
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision mysql5 web hosting services

Chapter 4 Control Structures: Part 1 105 Common (Web site management)

Friday, April 20th, 2007

Chapter 4 Control Structures: Part 1 105 Common Programming Error 4.2 Placing a semicolon after the condition in an if structure leads to a logic error in single- selection if structures and a syntax error in double-selection if structures (if the if clause contains a nonempty body statement). Good Programming Practice 4.3 Some programmers prefer to type the beginning and ending braces of blocks before typing the individual statements within the braces. This practice helps avoid omitting one or both of the braces. In this section, we introduced the notion of a block. A block may contain declarations. The declarations in a block commonly are placed first in the block before any action statements, but declarations may be intermixed with action statements. 4.7 while Repetition Structure A repetition structure allows the programmer to specify that an action is to be repeated while a condition remains true. The pseudocode statement While there are more items on my shopping list Purchase next item and cross it off my list describes the repetition that occurs during a shopping trip. The condition, there are more items on my shopping list may be true or false. If it is true, then the action, Purchase next item and cross it off my list is performed. This action executes repeatedly while the condition remains true. The statement(s) contained in the while repetition structure constitute the body of the while. The while structure body may be a single statement or a block. Eventually, the condition becomes false (when the last item on the shopping list has been purchased and crossed off the list). At this point, the repetition terminates, and the first statement after the repetition structure executes. As an example of a while structure, consider a program segment designed to find the first power of 2 larger than 1000. Suppose int variable product contains the value 2. When the following while structure finishes executing, product contains the result: int product = 2; while ( product <= 1000 ) product = 2 * product; The flowchart in Fig. 4.5 illustrates the flow of control of the preceding while repetition structure. Once again, note that (besides small circles and arrows) the flowchart contains only a rectangle symbol and a diamond symbol. Common Programming Error 4.3 Not providing in the body of a while structure an action that eventually causes the condition to become false is a logic error. Normally, such a repetition structure will never terminate, which is an error called an infinite loop. Common Programming Error 4.4 Beginning the keyword while with an uppercase W, as in While, is a syntax error. Remember that C# is a case-sensitive language. All of C# s keywords while, if, else, etc. contain only lowercase letters.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

Affordable web design - 104 Control Structures: Part 1 Chapter 4 if

Friday, April 20th, 2007

104 Control Structures: Part 1 Chapter 4 if ( x > 5 ) { if ( y > 5 ) Console.WriteLine( “x and y are > 5″ ); } else Console.WriteLine( “x is <= 5" ); The braces ({}) indicate to the compiler that the second ifstructure is in the body of the first if structure and that the else is matched with the first if structure. The if selection structure normally expects only one statement in its body. To include several statements in the body of an if, enclose these statements in braces ({ and }). A set of statements contained in a pair of braces is called a block. Software Engineering Observation 4.2 A block can be placed anywhere in a program at which a single statement can be placed. The following example includes a block in the else part of an if/else structure: if ( studentGrade >= 60 ) Console.WriteLine( “Passed” ); else { Console.WriteLine( “Failed” ); Console.WriteLine( “You must take this course again.” ); } In this case, if studentGrade is less than 60, the program executes both statements in the body of the else and prints Failed You must take this course again. Notice the braces surrounding the two statements in the elseclause. These braces are important. Without the braces, the statement Console.WriteLine( “You must take this course again.” ); would be outside the body of the else and would execute regardless of whether the grade is less than 60. Common Programming Error 4.1 Forgetting one of the braces that delimit a block can lead to syntax errors. Forgetting both of the braces that delimit a block can lead to syntax and/or logic errors. Syntax errors, such as when one brace in a block is left out of the program, are caught by the compiler. A logic error, such as the error caused when both braces in a block are left out of the program, has its effect at execution time. A fatal logic error causes a program to fail and terminate prematurely. A nonfatal logic error allows a program to continue executing, but the program produces incorrect results. Software Engineering Observation 4.3 Just as a block can be placed anywhere a single statement can be placed, it is also possible to have an empty statement, which is represented by placing a semicolon (;) where a statement normally would be.
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

Chapter 4 Control Structures: (Web site designers) Part 1 103 Most

Thursday, April 19th, 2007

Chapter 4 Control Structures: Part 1 103 Most C# programmers prefer to write the preceding if structure as if ( studentGrade >= 90 ) Console.WriteLine( “A” ); else if ( studentGrade >= 80 ) Console.WriteLine( “B” ); else if ( studentGrade >= 70 ) Console.WriteLine( “C” ); else if ( studentGrade >= 60 ) Console.WriteLine( “D” ); else Console.WriteLine( “F” ); Both forms are equivalent. The latter form is popular because it avoids the deep indentation of the code. Such indentation often leaves little room on a line, forcing lines to be split and decreasing program readability. The C# compiler always associates an else with the previous if, unless told to do otherwise by the placement of braces ({}). This is referred to as the dangling-else problem. For example, if ( x > 5 ) if ( y > 5 ) Console.WriteLine( “x and y are > 5″ ); else Console.WriteLine( “x is <= 5" ); appears to indicate that if x is greater than 5, the if structure in its body determines if y is also greater than 5. If so, the string "xandyare>5″ is output. Otherwise, it appears that if x is not greater than 5, the else part of the if/else structure outputs the string “xis<=5". Testing and Debugging Tip 4.1 The reader can use Visual Studio to indent code properly. In order to check indentation, the reader should highlight the relevant code and press Ctrl-K followed immediately by Ctrl-F. However, the preceding nested if structure does not execute as its indentation implies. The compiler actually interprets the structure as if ( x > 5 ) if ( y > 5 ) Console.WriteLine( “x and y are > 5″ ); else Console.WriteLine( “x is <= 5" ); in which the body of the first if structure is an if/else structure. This structure tests if x is greater than 5. If so, execution continues by testing if y is also greater than 5. If the second condition is true, the proper string "xandyare>5″ is displayed. However, if the second condition is false, the string “xis<=5"is displayed, even though we know x is greater than 5. To force the preceding nested if structure to execute as it was originally intended, the structure must be written as follows:
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision professional web hosting services

102 Control Structures: Part 1 Chapter 4 The (Web site template)

Thursday, April 19th, 2007

102 Control Structures: Part 1 Chapter 4 The statement with the conditional operator performs in the same manner as the preceding if/elsestatement. The precedence of the conditional operator is low, so the entire conditional expression normally is placed in parentheses. Conditional operators can be used in some situations where if/else statements cannot, such as the argument to the WriteLine method shown earlier. Nested if/else structures can test for multiple cases by placing if/else structures inside other if/else structures. For example, the following pseudocode statement will print Afor exam grades greater than or equal to 90, B for grades in the range 80 89, C for grades in the range 70 79, D for grades in the range 60 69 and F for all other grades: If student s grade is greater than or equal to 90 Print A Else If student s grade is greater than or equal to 80 Print B Else If student s grade is greater than or equal to 70 Print C Else If student s grade is greater than or equal to 60 Print D Else Print F This pseudocode may be written in C# as if ( studentGrade >= 90 ) Console.WriteLine( “A” ); else if ( studentGrade >= 80 ) Console.WriteLine( “B” ); else if ( studentGrade >= 70 ) Console.WriteLine( “C” ); else if ( studentGrade >= 60 ) Console.WriteLine( “D” ); else Console.WriteLine( “F” ); If studentGrade is greater than or equal to 90, the first four conditions are true, but only the Console.WriteLine statement after the first test executes. After that particular Console.WriteLine executes, the program skips the else part of the outer if/ else structure. Good Programming Practice 4.2 If there are several levels of indentation, each level should be indented the same additional amount of space.
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services

Christian web host - Chapter 4 Control Structures: Part 1 101 prints

Wednesday, April 18th, 2007

Chapter 4 Control Structures: Part 1 101 prints Passed if the student s grade is greater than or equal to 60, and prints Failed if the student s grade is less than 60. In either case, after printing occurs, the next pseudocode statement in sequence is performed. The preceding pseudocode If/Else structure may be written in C# as if ( studentGrade >= 60 ) Console.WriteLine( “Passed” ); else Console.WriteLine( “Failed” ); Good Programming Practice 4.1 Indent both body statements of an if/else structure. Note that the body of the else statement also is indented. The indentation convention you choose should be applied carefully throughout your programs. It is difficult to read programs that do not use uniform spacing conventions. The flowchart in Fig. 4.4 illustrates the flow of control in the if/else structure. Note that (besides small circles and arrows) the only symbols in the flowchart are rectangles (for actions) and a diamond (for a decision). We continue to emphasize this action/decision model of computing. The conditional operator (?:) is related closely to the if/else structure. The ?:is C# s only ternary operator it takes three operands. The operands and the ?: form a conditional expression. The first operand is a condition (i.e., an expression that evaluates to a bool value), the second is the value for the conditional expression if the condition evaluates to true and the third is the value for the conditional expression if the condition evaluates to false. For example, the output statement Console.WriteLine( studentGrade >= 60 ? “Passed” : “Failed” ); contains a conditional expression that evaluates to the string “Passed” if the condition studentGrade >=60 is true and evaluates to the string “Failed” if the condition is false. studentGrade >= 60 print Failed print Passed true false Fig. 4.4 Flowcharting a double-selection if/else structure.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision jsp web hosting services

100 Control Structures: Part 1 Chapter 4 Notice (Most popular web site)

Wednesday, April 18th, 2007

100 Control Structures: Part 1 Chapter 4 Notice that the C# code corresponds closely to the pseudocode, demonstrating how pseudocode can be useful as a program development tool. The statement in the body of the if structure outputs the character string “Passed” in the console window. The flowchart in Fig. 4.3 illustrates the single-selection if structure. This flowchart contains the most important flowcharting symbol the decision (or diamond) symbol, which indicates that a decision is to be made. The decision symbol contains a condition, that can be either trueor false. The decision symbol has two flowlines emerging from it. One indicates the direction to be taken when the condition in the symbol is true; the other indicates the direction to be taken when the condition is false. A decision can be made on any expression that evaluates to a value of C# s bool type (i.e., any expression that evaluates to trueor false). Note that the if structure, too, is a single-entry/single-exit structure. The flowcharts for the remaining control structures also contain (aside from small circle symbols and flowlines) only rectangle symbols, to indicate the actions to be performed, and diamond symbols, to indicate decisions to be made. This is the action/decision model of programming we have been emphasizing. We can envision eight bins, each containing control structures for only one of the eight types. The control structures in each bin are empty; nothing is written in the rectangles or diamonds. The programmer s task is to assemble a program using as many control structures as the algorithm demands, combining those control structures in only two possible ways (stacking or nesting), then filling in the actions and decisions in a manner appropriate for the algorithm. We will discuss the variety of ways in which actions and decisions may be written. 4.6 if/else Selection Structure The if selection structure performs an indicated action only when the condition evaluates to true; otherwise, the action is skipped. The if/else selection structure allows the programmer to specify different actions to perform when the condition is true and when the condition is false. For example, the pseudocode statement If student s grade is greater than or equal to 60 Print Passed Else Print Failed studentGrade >= 60 print Passed true false Fig. 4.3 Flowcharting a single-selection if structure.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision florida web design services

Chapter 4 Control Structures: Part 1 99 C# (Web server extensions)

Tuesday, April 17th, 2007

Chapter 4 Control Structures: Part 1 99 C# Keywords internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using virtual void volatile while Fig. 4.2 C# keywords. (Part 2 of 2.) C# has only eight control structures sequence, three types of selection and four types of repetition. Each program is formed by combining as many of each type of control structure as is necessary. As with the sequence structure in Fig. 4.1, each control structure is flowcharted with two small circle symbols, one at the entry point to the control structure and one at the exit point. Single-entry/single-exit control structures make it easy to build programs the control structures are attached to one another by connecting the exit point of one control structure to the entry point of the next. This is similar to the stacking of building blocks; thus, we call it control-structure stacking. There is only one other way control structures may be connected, and that is through control-structure nesting, where one control structure can be placed inside another. Thus, algorithms in C# programs are constructed from only eight different types of control structures combined in only two ways. 4.5 if Selection Structure In a program, a selection structure chooses among alternative courses of action. For example, suppose that the passing grade on an examination is 60 (out of 100). Then the pseudocode statement If student s grade is greater than or equal to 60 Print Passed determines if the condition student s grade is greater than or equal to 60 is true or false. If the condition is true, then Passed is printed, and the next pseudocode statement in order is performed. (Remember that pseudocode is not a real programming language.) If the condition is false, the print statement is ignored, and the next pseudocode statement in order is performed. Note that the second line of this selection structure is indented. Such indentation is optional, but it is highly recommended because it emphasizes the inherent structure of structured programs. The preceding pseudocode If statement may be written in C# as if ( studentGrade >= 60 ) Console.WriteLine( “Passed” );
Note: If you are looking for cheap webhost to host and run your apache application check Vision apache web hosting services

98 Control Structures: Part 1 Chapter 4 Consider (Web hosting company)

Tuesday, April 17th, 2007

98 Control Structures: Part 1 Chapter 4 Consider the flowchart segment for the sequence structure in Fig. 4.1. We use the rectangle symbol, also called the action symbol, to indicate any type of action, including a calculation or an input/output operation. The flowlines in the figure indicate the order in which the actions are to be performed first, studentGrade is to be added to total, then 1 is to be added to counter. We can have as many actions as we want in a sequence structure. Anywhere in a sequence that a single action may be placed, several actions may also be placed. When drawing a flowchart that represents a complete algorithm, an oval symbol containing the word Begin is the first symbol used; an oval symbol containing the word End indicates where the algorithm ends. When drawing only a portion of an algorithm, as in Fig. 4.1, the oval symbols are omitted in favor of using small circle symbols, also called connector symbols. Perhaps the most important flowcharting symbol is the diamond symbol, also called the decision symbol, which indicates that a decision is to be made. We discuss the diamond symbol in Section 4.5. C# provides three types of selection structures, which we discuss in this chapter and the next. The if selection structure performs (selects) an action if a condition is true or skips the action if the condition is false. The if/else selection structure performs an action if a condition is true and performs a different action if the condition is false. The switch selection structure, discussed in Chapter 5, Control Structures: Part 2, performs one of many actions, depending on the value of an expression. The if structure is called a single-selection structure because it selects or ignores a single action (or a single group of actions). The if/else structure is called a double-selection structure because it selects between two different actions (or groups of actions). The switch structure is called a multiple-selection structure because it selects among many different actions (or groups of actions). C# provides four repetition structures while, do/while, for and foreach (while is covered in this chapter, do/while and for are covered in Chapter 5, Control Structures: Part 2, and foreach is covered in Chapter 8, Object-Based Programming). Each of the words if, else, switch, while, do, for and foreach are C# keywords. Figure 4.2 lists the complete set of C# keywords. We discuss the vast majority of C# s keywords throughout this book. C# Keywords abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface Fig. 4.2 C# keywords. (Part 1 of 2.)
Note: In case you are looking for affordable webhost to host and run your web application check Vision http web server services

Chapter 4 Control Structures: Part 1 97 During (Web design tools)

Monday, April 16th, 2007

Chapter 4 Control Structures: Part 1 97 During the 1960s, it became clear that the indiscriminate use of transfers of control was causing difficulty for software development groups. The problem was the goto statement, which, in some programming languages, allows the programmer to specify a transfer of control to one of a wide range of possible destinations in a program. This caused programs to become quite unstructured and hard to follow. The notion of structured programming became almost synonymous with goto elimination. The research of Bohm and Jacopini1 demonstrated that all programs with goto statements could be written without them. The challenge of the era for programmers was to shift their styles to goto-less programming. It was not until the 1970s that programmers started taking structured programming seriously. The results were impressive, as software development groups reported reduced development times, more frequent on-time delivery of systems and more frequent within-budget completion of software projects. The key to these successes was that structured programs were clearer, easier to debug and modify and more likely to be bug-free in the first place. Bohm and Jacopini s work demonstrated that all programs could be written in terms of only three control structures, namely, the sequence structure, the selection structure and the repetition structure. The sequence structure is built into C#. Unless directed otherwise, the computer executes C# statements one after the other in the order in which they appear in a program. The flowchart segment of Fig. 4.1 illustrates a typical sequence structure in which two calculations are performed in order. A flowchart is a graphical representation of an algorithm or of a portion of an algorithm. Flowcharts contain certain special-purpose symbols, such as rectangles, diamonds, ovals and small circles. These symbols are connected by arrows called flowlines, which indicate the order in which the actions of the algorithm execute. This order is known as the flow of control. Like pseudocode, flowcharts often are useful for developing and representing algorithms, although pseudocode is preferred by many programmers. Flowcharts show clearly how control structures operate; that is all we use them for in this text. The reader should compare carefully the pseudocode and flowchart representations of each control structure. add studentGrade to total add 1 to counter total = total + studentGrade; counter = counter + 1; Fig. 4.1 Flowcharting C# s sequence structure. 1. Bohm, C., and G. Jacopini, Flow Diagrams, Turing Machines, and Languages with Only Two Formation Rules, Communications of the ACM, Vol. 9, No. 5, May 1966, pp. 336 371.
Note: If you are looking for best quality webspace to host and run your tomcat application check Vision personal web hosting services