Archive for May, 2007

140 Control Structures: Part 2 Chapter 5 Outline (Web host 4 life)

Tuesday, May 8th, 2007

140 Control Structures: Part 2 Chapter 5 Outline 5.1 Introduction 5.2 Essentials of Counter-Controlled Repetition 5.3 for Repetition Structure 5.4 Examples Using the for Structure 5.5 switch Multiple-Selection Structure 5.6 do/while Repetition Structure 5.7 Statements break and continue 5.8 Logical and Conditional Operators 5.9 Structured-Programming Summary Summary Terminology Self-Review Exercises Answers to Self-Review Exercises Exercises 5.1 Introduction Chapter 4 began our introduction to the types of building blocks that are available for problem solving and used those building blocks to implement proven program-construction principles. In this chapter, we continue our presentation of the theory and principles of structured programming by introducing C# s remaining control structures. As in Chapter 4, the C# techniques you learn here are applicable to most high-level languages. When we begin our formal treatment of object-based programming in C# in Chapter 8, we will see that the control structures we study in this chapter and in Chapter 4 are helpful in building and manipulating objects. 5.2 Essentials of Counter-Controlled Repetition In the last chapter, we introduced the concept of counter-controlled repetition. In this section, we formalize the elements needed in counter-controlled repetition, namely: 1. The name of a control variable (or loop counter), used to determine whether the loop continues. 2. The initial value of the control variable. 3. The increment (or decrement) by which the control variable is modified each time through the loop (also known as each iteration of the loop). 4. The condition that tests for the final value of the control variable (i.e., whether looping should continue). To see the four elements of counter-controlled repetition, consider the simple program in Fig. 5.1, which displays the digits 1 5. The declaration (line 10) int counter = 1; names the control variable (counter), declares it to be an integer, reserves space for it in memory and sets it to an initial value of 1. This statement is a declaration that includes an
Note: If you are looking for cheap and reliable webhost to host and run your mysql application check Vision mysql hosting services

5 Control Structures: Part 2 Objectives To

Monday, May 7th, 2007

5 Control Structures: Part 2 Objectives To be able to use the forand do/while repetition structures to execute statements in a program repeatedly. To understand multiple selection that uses the switch selection structure. To be able to use the breakand continue program-control statements. To be able to use the logical operators. Who can control his fate? William Shakespeare, Othello The used key is always bright. Benjamin Franklin Man is a tool-making animal. Benjamin Franklin Intelligence is the faculty of making artificial objects, especially tools to make tools. Henri Bergson
Note: If you are looking for reliable webhost to maintain and run your java application check Vision java hosting services

138 Control (Zeus web server) Structures: Part 1 Chapter 4 by

Sunday, May 6th, 2007

138 Control Structures: Part 1 Chapter 4 by the placement of braces ({}). On first glance, the programmer may not be sure which if and elsematch; this is referred to as the dangling-else problem. We have eliminated the indentation from the following code to make the problem more challenging. (Hint: Apply indentation conventions that you have learned.) a) if ( x < 10 ) if ( y > 10 ) Console.WriteLine( “*****” ); else Console.WriteLine( “#####” ); Console.WriteLine( “$$$$$” ); b) if ( x < 10 ) { if ( y > 10 ) Console.WriteLine( “*****” ); } else { Console.WriteLine( “#####” ); Console.WriteLine( “$$$$$” ); } 4.14 A palindrome is a number or a text phrase that reads the same backwards as forwards. For example, each of the following five-digit integers are palindromes: 12321, 55555, 45554 and 11611. Write an application that reads in a five-digit integer and determines whether it is a palindrome. If the number is not five digits, display an error message dialog indicating the problem to the user. When the user dismisses the error dialog, allow the user to enter a new value. 4.15 A company wants to transmit data over the telephone, but they are concerned that their phones may be tapped. All their data are transmitted as four-digit integers. They have asked you to write a program that will encrypt their data so that it may be transmitted more securely. Your application should read a four-digit integer entered by the user in an input dialog and encrypt it as follows: Replace each digit by (the sum of that digit plus 7) modulus 10. Then swap the first digit with the third, and swap the second digit with the fourth. Print the encrypted integer. Write a separate application that inputs an encrypted four-digit integer and decrypts it to form the original number. 4.16 The factorial of a nonnegative integer n is written n! (pronounced n factorial ) and is defined as follows: n! = n (n - 1) (n -2) … 1 (for values of n greater than or equal to 1) and n! = 1 (for n = 0). For example, 5! =5 4 3 2 1, which is 120. a) Write an application that reads a nonnegative integer from an input dialog and computes and prints its factorial. b) Write an application that estimates the value of the mathematical constant e by using the formula 111 e = 1+—- –+—- –+—–+ 1! 2! 3! c) Write an application that computes the value of ex by using the formula 23 x xxx e = 1+—–+—- —+—- –+ 1! 2! 3!
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision j2ee hosting services

Chapter 4 Control Structures: Part (Web design templates) 1 137 18

Sunday, May 6th, 2007

Chapter 4 Control Structures: Part 1 137 18 19 Console.WriteLine( “The sum is: ” + sum ); 20 } 21 } 4.7 a) product = 25, x = 6; b) quotient = 0, x = 6; 4.8 a) Error: Missing the closing right brace of the whilebody. Correction: Add closing right brace after the statement ++c;. b) Error: Semicolon after else results in a logic error. The second output statement will always be executed. Correction: Remove the semicolon after else. 4.9 The value of the variable z is never changed in the while structure. Therefore, if the loop- continuation condition (z>=0) is true, an infinite loop is created. To prevent the infinite loop, z must be decremented so that it eventually becomes less than 0. EXERCISES 4.10 Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a C# program that will input the miles driven and gallons used (both as doubles) for each tankful. The program should calculate and display the miles per gallon obtained for each tankful and print the combined miles per gallon obtained for all tankfuls up to this point. All average calculations should produce floating-point results. 4.11 Develop a C# application that will determine if a department store customer has exceeded the credit limit on a charge account. For each customer, the following facts are available: a) Account number b) Balance at the beginning of the month c) Total of all items charged by this customer this month d) Total of all credits applied to this customer’s account this month e) Allowed credit limit The program should input as integers each of these facts, calculate the new balance (= beginning balance + charges credits), display the new balance and determine if the new balance exceeds the customer’s credit limit. For those customers whose credit limit is exceeded, the program should display the message, Credit limit exceeded. 4.12 Write a C# application that uses looping to print the following table of values: N 10*N 100*N 1000*N 1 10 100 1000 2 20 200 2000 3 30 300 3000 4 40 400 4000 5 50 500 5000 4.13 (Dangling-Else Problem) Determine the output for each of the following, when xis 9and y is 11 and when xis 11 and yis 9. Note that the compiler ignores the indentation in a C# program. Also, the C# compiler always associates an else with the previous if unless told to do otherwise
Note: If you are looking for reliable webhost to maintain and run your java application check Vision java hosting services

136 Control Structures: Part 1 (Business web hosting) Chapter 4 4.9

Sunday, May 6th, 2007

136 Control Structures: Part 1 Chapter 4 4.9 What is wrong with the following while repetition structure? while ( z >= 0 ) sum += z; ANSWERS TO SELF-REVIEW EXERCISES 4.1 a) sequence, selection, repetition. b) if/else. c) counter-controlled or definite. d) sentinel, signal, flag or dummy. e) program control. f) Pseudocode. g) Keywords. h) empty. i) one. j) cast. 4.2 a) False. Pseudocode should convert easily into C# code. b) True. c) False. Some programmers argue that goto statements violate structured programming and cause considerable problems. d) True. e) True. f) False. The sequence structure is built into C#; lines of code execute in the order in which they are written, unless explicitly directed to do otherwise. g) True. h) False. Placing a semicolon after the condition in an if structure is usually a logic error. i) True. 4.3 x = x + 1; x += 1; ++x; x++; 4.4 a) z = x++ + y; b) if ( count > 10 ) Console.WriteLine( “Count is greater than 10″ ); c) total -= –x; d) q %= divisor; q = q % divisor; 4.5 a) int sum, x; b) x = 1; c) sum = 0; d) sum += x;or sum = sum + x; e) Console.WriteLine( “The sum is: ” + sum );or Console.WriteLine( “The sum is: {0}”, sum ); 4.6 1 // Calculate the sum of the integers from 1 to 10 2 3 using System; 4 5 class Calculate 6 { 7 static void Main( string[] args ) 8 { 9 int sum, x; 10 11 x = 1; 12 sum = 0; 13 14 while ( x <= 10 ) 15 { 16 sum += x++; 17 }
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision servlet hosting services

Chapter 4 Control Structures: Part 1 135 i) (Vps web hosting)

Saturday, May 5th, 2007

Chapter 4 Control Structures: Part 1 135 i) The increment operator (++) and decrement operator (–) increment and decrement a variable s value by . j) Explicit conversion makes use of the operator. 4.2 State whether each of the following is true or false. If false, explain why. a) It is difficult to convert pseudocode into a working C# program. b) Sequential execution refers to statements in a program that execute one after another. c) It is recommended for C# programmers to use goto statements. d) The ifstructure is called a single-selection structure. e) Structured programs are clear, easy to debug and modify and more likely than unstruc tured programs to be bug-free in the first place. f) The sequence structure is not built into C#. g) Pseudocode usually resembles actual C# code. h) Placing a semicolon after the condition in an if structure is a syntax error. i) The whilestructure body may be a single or a block. 4.3 Write four different C# statements that each add 1 to integer variable x and store the result in x. 4.4 Write C# statements to accomplish each of the following: a) Assign the sum of xand yto z then increment x by 1 after the calculation. Use only one statement. b) Test if the value of the variable count is greater than 10. If it is, print “Count is greaterthan10″. c) Decrement the variable x by 1, then subtract it from the variable total. Use only one statement. d) Calculate the remainder after qis divided by divisorand assign the result to q. Write this statement two different ways. 4.5 Write a C# statement to accomplish each of the following tasks: a) Declare variables sumand xto be of type int. b) Assign 1 to variable x. c) Assign 0 to variable sum. d) Add variable x to variable sum and assign the result to variable sum. e) Print “Thesumis:” followed by the value of variable sum. 4.6 Combine the statements that you wrote in Exercise 4.5 into a C# application that calculates and prints the sum of the integers from 1 to 10. Use the while structure to loop through the calculation and increment statements. The loop should terminate when the value of x becomes 11. 4.7 Determine the values of each variable after the calculation is performed. Assume that when each statement begins executing, all variables have the integer value 5. a) product *= x++; b) quotient /= ++x; 4.8 Identify and correct the errors in each of the following: a) while ( c <= 5 ) { product *= c; ++c; b) if ( gender == 1 ) Console.WriteLine( "Woman" ); else; Console.WriteLine( "Man" );
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision make web site services

134 Control Structures: Part 1 Chapter 4 keyword (Web server info)

Saturday, May 5th, 2007

134 Control Structures: Part 1 Chapter 4 keyword repetition structure level of refinement second refinement logic error selection structure loop sentinel-controlled repetition main form sentinel value multiple-selection structure sequence structure multiplicative operators: *, / and % sequential execution nonfatal logic error signal value oval symbol single-entry/single-exit control structure postdecrement operator single-selection structure postdecrementing small circle symbol postincrement operator stringprimitive data type postincrementing strongly typed language precedence of operators structured programming predecrement operator switch selection structure predecrementing syntax error preincrement operator System.Windows.Forms.Form class preincrementing temporary value preprocessor directives termination phase primitive (or built-in) data type ternary operator (?:) procedure for solving a problem top-down, stepwise refinement processing phase transfer of control program control true program development tool truncate promotion unary operator pseudocode Unicode pseudocode algorithm variable reference real number vertical spacing rectangle symbol while repetition structure refinement process white-space characters #region directive SELF-REVIEW EXERCISES 4.1 Fill in the blanks in each of the following statements: a) All programs can be written in terms of three types of control structures: , and . b) The selection structure executes one action when a condition is true and another action when a condition is false. c) Repetition of a set of instructions a specific number of times is called repetition. d) When it is not known in advance how many times a set of statements will be repeated, a value can be used to terminate the repetition. e) Specifying the order in which statements are to be executed in a computer program is called . f) is an artificial and informal language that helps programmers develop algorithms. g) are reserved by C# to implement various features, such as the language s control structures. h) A(n) statement specifying that no action is to be taken is indicated by placing a semicolon where a statement normally would be.
Note: In case you are looking for affordable and reliable webhost to host and run your j2ee application check Vision web and email hosting services

Freelance web design - Chapter 4 Control Structures: Part 1 133

Friday, May 4th, 2007

Chapter 4 Control Structures: Part 1 133 Visual Studio .NET generates code that builds the GUI for an application. The primitive types are the building blocks for more complicated types. TERMINOLOGY –, unary decrement operator %=, modulus assignment operator (type), cast operator *=, multiplication assignment operator ++, unary increment operator +=, addition assignment operator /=, division assignment operator ;, empty statement =, assignment operator -=, subtraction assignment operator ?:, ternary conditional operator {, open brace }, close brace abbreviating an assignment expression action symbol action/decision model of programming algorithm application class definition assignment operator (=) associate left to right associate right to left associativity of operators binary arithmetic operator blockbody of the whileboolprimitive data type boolean expression braces that delimit a block building block case-sensitive language cast operator collapsed code complete representation of a program conditional expression conditional operator (?:) connector symbol control structure control-structure nesting control-structure stacking counter counter-controlled repetition dangling-else problem decision symbol declaration definite repetition design phase diamond symbol Dispose method division by zero do/while repetition structure doubleprimitive data type double-selection structure else statement empty statement (;) end of data entry #endregion directive entry point of control structure examination-results problem exit point of control structure expanded code explicit conversion false fatal logic error first refinement flag value floating-point data type floating-point division floating-point number flow of control flowchart flowline for repetition structure fractional result goto elimination goto-less programming graphical representation of an algorithm if selection structure if/else selection structure implicit conversion indefinite repetition indentation indentation convention infinite loop inheriting from System.Windows.Forms.Form class initialization phase initialize InitializeComponent method IntelliSense input/output operation integer division integral data type
Note: If you are looking for cheap and reliable webhost to host and run your web application check Vision coldfusion web hosting services

132 Control Structures: Part 1 Chapter (Graphic web design) 4

Thursday, May 3rd, 2007

132 Control Structures: Part 1 Chapter 4 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. A repetition structure repeats an action (or set of actions) while some condition remains true. Eventually, the condition in a while structure will become false. At this point, the repetition terminates, and the first statement after the repetition structure executes. It is a logic error to fail to provide in the body of a while structure an action that eventually causes the condition to become false. Normally, such a repetition structure will never terminate, which is an error called an infinite loop. We use counter-controlled repetition to input data values one at a time, a specified number of times. This technique uses a variable called a counter to control the number of times a set of statements will execute. Counter-controlled repetition often is called definite repetition because the number of repetitions is known before the loop begins executing. Sentinel-controlled repetition is often called indefinite repetition because the number of repetitions is not known before the loop begins executing. The sentinel value (also called the signal value, dummy value or flag value) determines when to terminate a repetition structure. We approach programming problems with top-down, stepwise refinement a technique that is essential to the development of well-structured algorithms. The top is a single statement that conveys the overall function of the program. As such, the top is a complete representation of a program. We divide the top into a series of smaller tasks and list these in the order in which they must be performed. Each refinement, including the top itself, is a complete specification of the algorithm; only the level of detail in each refinement varies. Many algorithms can be divided logically into three phases an initialization phase that initializes the program variables, a processing phase that inputs data values and adjusts program variables accordingly and a termination phase that calculates and prints the results. The programmer terminates the top-down, stepwise refinement process when the pseudocode algorithm is specified in sufficient detail for the programmer to convert the pseudocode to a C# program. Omitting the curly braces that delineate a block in the body of a repetition structure can lead to logic errors, such as infinite loops. Dividing two integers results in integer division, in which any fractional part of the calculation is truncated. To ensure that the operands in an expression are of the same type, C# performs implicit conversion on selected operands and promotes them to the same type. C# provides the unary increment operator, ++, and the unary decrement operator, –. These operators add 1 to or subtract 1 from their operand, respectively. If an increment or decrement operator is placed before a variable, it is referred to as the preincrement or predecrement operator, respectively. If an increment or decrement operator is placed after a variable, it is referred to as the postincrement or postdecrement operator, respectively. A key benefit of extending classes using inheritance is that all the general capabilities are provided by the original class programmers do not need to define these capabilities on their own. Method InitializeComponent contains the code to configure component properties in a GUI. The value in parentheses after the type in a newoperation initializes the new object.
Note: If you are looking for high quality webhost to host and run your jsp application check Vision christian web host services

Make web site - Chapter 4 Control Structures: Part 1 131

Wednesday, May 2nd, 2007

Chapter 4 Control Structures: Part 1 131 Various C# statements enable the programmer to specify that the next statement to execute may be other than the next one in sequence. This is called transfer of control. Many programming complications in the 1960 s were a result of misusing the goto statement, which allows the programmer to specify a transfer of control to one of a wide range of possible destinations in a program. The notion of structured programming became almost synonymous with goto elimination. Bohm and Jacopini s work demonstrated that all programs could be written in terms of only three control structures namely, sequence, selection and repetition. 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. A flowchart is a graphical representation of an algorithm or of a portion of an algorithm. Flowcharts are drawn using symbols, such as rectangles, diamonds, ovals and small circles; these symbols are connected by arrows called flowlines, which indicate the order in which the algorithm s actions execute. The if selection structure performs (selects) an action if a condition is true or skips the action if the condition is false. The if/elseselection structure performs an action if a condition is true and performs a different action if the condition is false. A single-selection structure is one that selects or ignores a single action. A double-selection structure is one that selects between two actions. A multiple-selection structure is one that selects among many actions. Keywords are reserved by the language to implement various features, such as C# s control structures. Keywords cannot be used as identifiers. Each program is formed by combining as many of each type of C# s eight control structures as is appropriate for the algorithm the program implements. 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 called control-structure stacking. Algorithms in C# programs are constructed from only eight different types of control structures combined in only two ways. The decision symbol has two flowlines emerging from it. One indicates the direction to be taken when the expression in the symbol is true; the other indicates the direction to be taken when the expression is false. Control structure flowcharts contain (besides 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. The ternary conditional operator (?:) is closely related to the if/else structure. The operands and the ?: form a conditional expression. The first operand is a condition that evaluates to a bool value, the second is the value for the conditional expression if the condition evaluates to trueand the third is the value for the conditional expression if the condition evaluates to false. Nested if/else structures test for multiple cases by placing if/else structures inside other if/ else structures. A set of statements in a pair of braces is called a block. A block can be placed anywhere in a program that a single statement can be placed. A syntax error is caught by the compiler at compile time, while a logic error has its effect during execution.
Note: In case you are looking for affordable webhost to host and run your servlet application check Vision ecommerce web hosting services