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.
for
Iteration StatementTypically, for
statements are used for counter-controlled iteration, and while
statements for sentinel-controlled iteration.
for
Statement’s HeaderThe for
header “does it all”—it specifies each of the items needed for counter-controlled iteration with a control variable.
for
StatementThe 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
.
for
Statement’s Control VariableThe 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.
for
Statement’s HeaderThe increment of a for
statement may also be negative, in which case it’s a decrement, and the loop counts downward.
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.
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.
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.
float
or double
for Monetary AmountsFloating-point numbers of type double
(or float
) can cause trouble in monetary calculations; use type decimal
instead.
do
…while
Iteration StatementThe do
…while
statement tests the loop-continuation condition after executing the loop’s body; therefore, the body always executes at least once.
The do
…while
statement has the form:
do
{
statement
} while (condition);
switch
Multiple-Selection StatementThe switch
multiple-selection statement performs different actions based on the possible values of an expression.
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.
break
StatementThe break
statement causes immediate exit from a while
, for
, do
…while
, switch
or foreach
statement. Execution continues with the first statement after the control statement.
continue
StatementThe continue
statement, when executed in a while
, for
, do
…while
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.
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).
&&
) OperatorThe &&
(conditional AND) operator ensures that two conditions are both true
before we choose a certain path of execution.
||
) OperatorThe ||
(conditional OR) operator ensures that either or both of two conditions are true
before we choose a certain path of execution.
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.
&
) and Boolean Logical OR (|
) OperatorsThe 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).
^
)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
.
!
) OperatorThe !
(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.
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.