The bulk of this chapter focused on the if-else and switch branching statements and their related language elements the comparison and logical operators. Together they allow you to write decision-making programs that can react in different ways to different data.

The most important points discussed in this chapter are reviewed in this section.

A branching statement is a language construct that uses a given condition (Boolean expression) to decide between two or more alternative directions (branches) to follow in a program.

A program without any branching or iteration statements is executed sequentially, in the order (from top to bottom) that the statements are written in the source code.

The if statement is the fundamental branching statement in C#. It contains a Boolean expression that controls whether a statement (single or compound) will be executed.

By combining an if statement with an else block, the program can choose between executing just one of two alternative statements (single or compound).

A comparison operator allows two expressions to be compared and generates a result of type bool (true or false). C# contains six comparison operators of which four are relational and two are equality based.

By nesting if statements inside each other, you can make a construct that can choose between executing any number of different (single or compound) statements.

Sometimes nested if else statements are difficult to construct, maintain, and comprehend. By using a standardized system, nested if-else statements can be converted to multibranch if-else statements that are simpler and easier understand.

A logical operator (also called Boolean operator) allows you to combine two Boolean expressions into one Boolean expression. As a result, any number of Boolean expressions can be combined to form one Boolean expression. C# contains the three commonly used logical operators && ("and"), || ("or"), and ! ("not") and the less frequently applied logical operators & (bitwise "and"), | (bitwise "or"), and ^ (bitwise "exclusive or"). Logical operators more or less have the same meaning as and, or, and not in our every day spoken language and allow you to construct simpler programs while maintaining their logic expressiveness.

The segment of the source code where a particular variable identifier can be used to access the variables underlying value is called the variables scope. A variables scope is outlined by the block in which the variable is declared.

A block A can be inserted inside another block B. Block A then forms an outer scope relative to block B, which forms an inner scope. Inner and outer are relative terms.

The time between the creation and destruction of a variable is called the variables lifetime. As a general rule, a variable in C# is created when execution enters its scope and destroyed when execution leaves its scope.

The goto statement transfers control (jumps) to another part of the program and, therefore, belongs to a group of statements called jump statements. It is a controversial, error-prone construct and should only be used sparingly with the switch statement.

The switch statement is tailor-made to select from multiple (single or compound) statements and is somewhat similar to a condensed multibranch if-else statement but with a narrower set of applications. If applicable, the switch statement is often clearer, simpler, and more efficient than a corresponding multibranch if-else statement.

The conditional operator is also called the tertiary operator because it is the only operator in C# that combines three expressions. It allows you to let a Boolean expression determine which of two values (expressions) this conditional operator will return.


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: