Summary

Section 6.2 Essentials of Counter-Controlled Iteration

  • Counter-controlled iteration requires a control variable, the initial value of the control variable, the increment (or decrement) by which the control variable is modified each time through the loop and the loop-continuation condition that determines whether looping should continue.

Section 6.3 for Iteration Statement

  • Typically, for statements are used for counter-controlled iteration, and while statements for sentinel-controlled iteration.

Section 6.3.1 A Closer Look at the for Statement’s Header

  • The for header “does it all”—it specifies each of the items needed for counter-controlled iteration with a control variable.

Section 6.3.2 General Format of a for Statement

  • The general format of the for statement is

    
    for (initialization; loopContinuationCondition; increment)
    {
      statement
    }
    

    where the initialization expression names the loop’s control variable and provides its initial value, the loopContinuationCondition is the condition that determines whether looping should continue and the increment modifies the control variable’s value so that the loop-continuation condition eventually becomes false.

Section 6.3.3 Scope of a for Statement’s Control Variable

  • The scope of a variable defines where it can be used in an app. A local variable can be used only in the method that declares the variable and only from the point of declaration through the end of the block in which the variable is declared. A control variable declared in the initialization expression of a for statement is only that statement.

Section 6.3.5 Placing Arithmetic Expressions in a for Statement’s Header

  • The increment of a for statement may also be negative, in which case it’s a decrement, and the loop counts downward.

Section 6.6 App: Compound-Interest Calculations

  • When a variable of type decimal is initialized to an int value, the value of type int is promoted to a decimal type implicitly—no cast is required.

Section 6.6.1 Performing the Interest Calculations with Math Method pow

  • Methods that must be called using a class name are called static methods.

  • C# does not include an exponentiation operator. Instead, Math.Pow(x, y) calculates the value of x raised to the yth power. The method receives two double arguments and returns a double value.

  • C# will not implicitly convert a double to a decimal type, or vice versa, because of the possible loss of information in either conversion. To perform this conversion, a cast operator is required.

Section 6.6.2 Formatting with Field Widths and Alignment

  • Values are right-aligned in a field by default. To indicate that values should be output left-aligned, simply use a negative field width.

  • In a format item, an integer n after a comma indicates that the value output should be displayed with a field width of n—that is, with at least n character positions, using spaces as fill characters.

Section 6.6.3 Caution: Do Not Use float or double for Monetary Amounts

  • Floating-point numbers of type double (or float) can cause trouble in monetary calculations; use type decimal instead.

Section 6.7 dowhile Iteration Statement

  • The dowhile statement tests the loop-continuation condition after executing the loop’s body; therefore, the body always executes at least once.

  • The dowhile statement has the form:

    
    do
    {
       statement
    } while (condition);
    

Section 6.8 switch Multiple-Selection Statement

  • The switch multiple-selection statement performs different actions based on the possible values of an expression.

Section 6.8.1 Using a switch Statement to Count A, B, C, D and F Grades.

  • Method Console.ReadLine returns null when the end-of-file key sequence is encountered.

  • The switch statement’s block contains a sequence of case labels and an optional default label.

  • The expression in parentheses following keyword switch is the switch expression. The app attempts to match the value of the switch expression to a case label. If a match occurs, the app executes the statements for that case.

  • The switch statement does not provide a mechanism for testing ranges of values, so every value to be tested must be listed in a separate case label.

  • After the statements in a case execute, you’re required to include a statement that terminates the case, such as a break or a return.

  • If no match occurs between the switch expression’s value and a case label, the statements after the default label execute. If no match occurs and the switch does not contain a default label, program control continues with the first statement after the switch statement.

Section 6.10.1 break Statement

  • The break statement causes immediate exit from a while, for, dowhile, switch or foreach statement. Execution continues with the first statement after the control statement.

Section 6.10.2 continue Statement

  • The continue statement, when executed in a while, for, dowhile or foreach, skips the remaining statements in the loop body and proceeds with the next iteration of the loop. In a for statement, the increment is performed before the loop-continuation condition is tested.

Section 6.11 Logical Operators

  • Logical operators enable you to form more complex conditions by combining simple conditions. The logical operators are && (conditional AND), || (conditional OR), & (boolean logical AND), | (boolean logical inclusive OR), ^ (boolean logical exclusive OR) and ! (logical negation).

Section 6.11.1 Conditional AND (&&) Operator

  • The && (conditional AND) operator ensures that two conditions are both true before we choose a certain path of execution.

Section 6.11.2 Conditional OR (||) Operator

  • The || (conditional OR) operator ensures that either or both of two conditions are true before we choose a certain path of execution.

Section 6.11.3 Short-Circuit Evaluation of Complex Conditions

  • The parts of an expression containing && or || operators are evaluated only until it’s known whether the condition is true or false. This feature of conditional AND and conditional OR expressions is called short-circuit evaluation.

Section 6.11.4 Boolean Logical AND (&) and Boolean Logical OR (|) Operators

  • The boolean logical AND (&) and boolean logical inclusive OR (|) operators work identically to the && (conditional AND) and || (conditional OR) operators, but the boolean logical operators always evaluate both of their operands (i.e., they do not perform short-circuit evaluation).

Section 6.11.5 Boolean Logical Exclusive OR (^)

  • A complex condition containing the boolean logical exclusive OR (^) operator is true if and only if one of its operands is true and the other is false. If both operands are true or both are false, the entire condition is false.

Section 6.11.6 Logical Negation (!) Operator

  • The ! (logical negation) operator enables you to “reverse” the meaning of a condition. The logical negation operator is placed before a condition to choose a path of execution if the original condition is false. In most cases, you can avoid using logical negation by expressing the condition differently with an appropriate relational or equality operator.

Section 6.12 Structured-Programming Summary

  • Any form of control ever needed in a C# app can be expressed in terms of sequence, the if statement (selection) and the while statement (iteration). These can be combined in only two ways— stacking and nesting.

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

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