Summary

Section 5.2 Algorithms

  • An algorithm is a procedure for solving a problem in terms of the actions to execute and the order in which these actions execute.

  • Specifying the order in which statements (actions) execute in an app is called program control.

Section 5.3 Pseudocode

  • Pseudocode is an informal language that helps you develop algorithms without having to worry about the strict details of C# language syntax.

  • Carefully prepared pseudocode can easily be converted to a corresponding C# app.

Section 5.4 Control Structures

  • There are three types of control structures—sequence, selection and iteration.

Section 5.4.1 Sequence 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’re written.

  • Activity diagrams are part of the UML. An activity diagram models the workflow of a portion of a software system.

  • Activity diagrams are composed of special-purpose symbols, such as action-state symbols, diamonds and small circles. These symbols are connected by transition arrows, which represent the flow of the activity.

  • Like pseudocode, activity diagrams help you develop and represent algorithms. Activity diagrams clearly show how control structures operate.

  • Action-state symbols (rectangles with their left and right sides replaced with arcs curving outward) represent actions to perform.

  • The arrows in an activity diagram represent transitions, which indicate the order in which the actions represented by the action states occur.

  • The solid circle in an activity diagram represents the activity’s initial state. The solid circle surrounded by a hollow circle represents the final state.

  • Rectangles with the upper-right corners folded over are UML notes (like comments in C#)— explanatory remarks that describe the purpose of symbols in the diagram.

Section 5.4.2 Selection Statements

  • C# has three types of selection statements: the if statement, the ifelse statement and the switch statement.

  • The if statement is called a single-selection statement because it selects or ignores a single action (or group of actions).

  • The ifelse statement is called a double-selection statement because it selects between two different actions (or groups of actions).

  • The switch statement is called a multiple-selection statement because it selects among many different actions (or groups of actions).

Section 5.4.3 Iteration Statements

  • C# provides four iteration statements: the while, dowhile, for and foreach statements.

  • The while, for and foreach statements perform the actions in their bodies zero or more times.

  • The dowhile statement performs the actions in its body one or more times.

Section 5.4.4 Summary of Control Statements

  • Control statements may be connected in two ways: control-statement stacking and control-statement nesting.

Section 5.5 if Single-Selection Statement

  • The if single-selection statement performs an indicated action (or group of actions) only when the condition is true; otherwise, the action is skipped.

  • In an activity diagram, the diamond symbol indicates that a decision is to be made. The workflow will continue along a path determined by the symbol’s associated guard conditions.

  • When modelled by a UML activity diagram, all control statements contain initial states, transition arrows, action states and decision symbols.

Section 5.6 ifelse Double-Selection Statement

  • The ifelse statement allows you to specify an action (or group of actions) to perform when the condition is true and a different action (or group of actions) when the condition is false.

Section 5.6.3 Blocks

  • To include several statements in the body of an if (or the body of an else for an ifelse statement), enclose the statements in braces ({ and }).

  • A set of statements contained within a pair of braces is called a block. A block can be placed anywhere in an app that a single statement can be placed.

Section 5.6.4 Conditional Operator (?:)

  • C# provides the conditional operator (?:), which can be used in place of an ifelse statement. The conditional expression evaluates to the second operand if the first operand evaluates to true, and evaluates to the third operand if the first operand evaluates to false.

Section 5.7 Student Class: Nested ifelse Statements

  • A read-only property contains only a get accessor.

Section 5.8 while Iteration Statement

  • An iteration statement allows you to specify that an app should repeat an action while some condition remains true.

  • The format for the while iteration statement is

    
    while (condition)
    {
       statement
    }
    
  • The UML represents both the merge symbol and the decision symbol as diamonds. The merge symbol joins two flows of activity into one.

Section 5.9.1 Pseudocode Algorithm with Counter-Controlled Iteration

  • Counter-controlled iteration is a technique that uses a variable called a counter to control the number of times a set of statements will execute.

Section 5.9.2 Implementing Counter-Controlled Iteration

  • C# requires local variables to be definitely assigned before their values are used.

Section 5.9.3 Integer Division and Truncation

  • Dividing two integers results in integer division—any fractional part of the calculation is lost.

Section 5.10 Formulating Algorithms: Sentinel-Controlled Iteration

  • Sentinel-controlled iteration is a technique that uses a special value called a sentinel value to indicate “end of data entry.”

  • Sentinel-controlled iteration is often called indefinite iteration, because the number of iterations is not known in advance.

Section 5.10.1 Top-Down, Stepwise Refinement: The Top and First Refinement

  • Top-down, stepwise refinement is essential to the development of well-structured apps.

  • Begin with a pseudocode representation of the top—a single statement that, in effect, is a complete representation of an app.

  • The top rarely conveys sufficient detail from which to write an app. So in the first refinement, we refine the top into a series of smaller tasks and list these in the order in which they’ll be performed. This refinement uses only tasks in sequence.

Section 5.10.2 Second Refinement

  • In the second refinement, we commit to specific variables and logic.

Section 5.10.3 Implementing Sentinel-Controlled Iteration

  • A number with a decimal point is called a real number or floating-point number (e.g., 7.33, 0.0975 or 1000.12345).

  • Types float and double represent floating-point numbers.

  • double variables can typically store numbers with larger magnitude and finer detail (i.e., more precision).

Section 5.10.6 Converting Between Simple Types Explicitly and Implicitly

  • The unary cast operator (double) creates a temporary floating-point copy of its operand. Using a cast operator in this manner is called explicit conversion.

  • To ensure that both operands of a binary operator are of the same type, C# performs promotion on selected operands.

Section 5.10.7 Formatting Floating-Point Numbers

  • The format specifier F is used to format floating-point numbers—typically rounded to two digits to the right of the decimal point.

Section 5.12 Compound Assignment Operators

  • C# provides several compound assignment operators for abbreviating assignment expressions, including +=, -=, *=, /= and %=.

Section 5.13 Increment and Decrement Operators

  • C# provides the unary increment operator, ++, and the unary decrement operator, --, for adding 1 to or subtracting 1 from the value of a numeric variable.

  • Incrementing (or decrementing) a variable with the prefix increment (or prefix decrement) operator increments (or decrements) the variable by 1 and uses the new variable value in the expression in which the variable appears. Incrementing (or decrementing) a variable with the postfix increment (or postfix decrement) operator increments (or decrements) the variable by 1 but uses the original variable value in the expression in which the variable appears.

Section 5.14 Simple Types

  • C# requires all variables to have a type.

  • Variables of simple types declared outside a method as fields of a class are automatically assigned default values. Instance variables of types char, byte, sbyte, short, ushort, int, uint, long, ulong, float, double, and decimal are all given the value 0 by default. Instance variables of type bool are given the value false by default. Reference-type instance variables are initialized by default to the value null.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset