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.
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.
There are three types of control structures—sequence, selection and iteration.
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.
C# has three types of selection statements: the if
statement, the if
…else
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 if
…else
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).
C# provides four iteration statements: the while
, do
…while
, for
and foreach
statements.
The while
, for
and foreach
statements perform the actions in their bodies zero or more times.
The do
…while
statement performs the actions in its body one or more times.
Control statements may be connected in two ways: control-statement stacking and control-statement nesting.
if
Single-Selection StatementThe 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.
if
…else
Double-Selection StatementThe if
…else
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.
To include several statements in the body of an if
(or the body of an else
for an if
…else
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.
?:
)C# provides the conditional operator (?:
), which can be used in place of an if
…else
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
.
Student
Class: Nested if
…else
StatementsA read-only property contains only a get
accessor.
while
Iteration StatementAn 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.
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.
C# requires local variables to be definitely assigned before their values are used.
Dividing two integers results in integer division—any fractional part of the calculation is lost.
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.
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.
In the second refinement, we commit to specific variables and logic.
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).
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.
The format specifier F
is used to format floating-point numbers—typically rounded to two digits to the right of the decimal point.
C# provides several compound assignment operators for abbreviating assignment expressions, including +=
, -=
, *=
, /=
and %=
.
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.
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
.