click below
click below
Normal Size Small Size show me how
Chapter 5
Control Statements: Part 1
Term | Definition |
---|---|
Algorithm | A procedure for solving a problem in terms of.. 1. The actions to execute and.. 2. The order in which these actions execute. Any computing problem can be solved by executing a series of actions in a specific order |
Program Control | Specifying the order in which statements(actions) execute. This chapter investigates program control using C#'s control statments |
1. Pseudocode | An informal language that helps you to develop algorithms without having to worry about the strict details of C# language syntax. Is useful for developing algorithms that will be converted to structured portions of C# apps. |
2. Pseudocode | Is not a programming language. Does not execute on computers. Helps you "think out" an app before attempting to write it in C# |
Sequential Execution | When statements execute one after another in the order in which they're written. |
Transfer of Control | Specifying that the next statement is not necessarily the next one in the sequence. Various C# statements enable you to do this |
The Three Control Statements That Can Be Used to Write Any App | Bohm and Jacopini's work demonstrated that all apps could e written in terms of only three control structures - the sequence structure, the selection structure, and the repetition structure |
Sequence Structure in C# | 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 - that is, in sequence |
UML Activity Diagrams | Help you develop and represent algorithms. Clearly show how control structures operate. Model workflow(activity) or a portion of a software system. Composed of special-purpose symbols, such as action symbols, which are connected by transition arrows |
Activity Diagram Action-State Symbols | Rectangles with their left and right sides replaced with arcs curving outward, diamonds, and small circles. |
Activity Diagram Transition Arrows | Connect action-state symbols. Which represent the flow of activity - that is, the order in which the actions should occur |
Activity Diagram Action Expression | Each action state contains an action expression which specifies an action to perform |
Activity Diagram Solid Circle | The solid circle at the top of the diagram represents the activity's initial state - the beginning of the workflow before the app performs the modeled actions |
Activity Diagram Solid Circle Surrounded by a Hollow Circle | The solid circle surrounded by a hollow circle that appears at the bottom of the diagram represents the final state - the end of the workflow after the app performs its actions |
Activity Diagram UML Notes | Rectangles with the upper-right corners folded over which describe the purpose of symbols in the diagram. These are like comments in C#. A dotted line connects each note with the element that the note desribes |
Selection Structures in C# | C# has three types of selection structures, which from this point will be referred to as selection statements. These three are the... 1. if statement.. 2. if...else statement.. 3. switch statement |
if statement | Either performs(selects) an action if a condition is true or skips the action if the condition is false |
if...else statement | Performs an action if a condition is true or performs a different action if the condition is false |
switch statement | Performs one of many different actions, depending on the value of an expression |
Single-Selection Statement | Selects or ignores a single action or group of actions. The if statement is a single-selection statement |
Double-Selection Statement | Selects between two different actions or groups of actions. The if...else statement is a double-selection statement |
Multiple-Selection Statement | Selects among many different actions or groups of actions. The switch statement is a multiple-selection statement |
1. Repetition Structures in C# | C# provides four repetition structures, which shall be referred to as repetition statements(iteration statements, or loops). These statements enable apps to perform statements repeatedly, depending on the value of a loop-continuation condition |
2. Repetition Structures in C# | The repetition statements are the... 1. while.. 2. do...while.. 3. for.. 4. foreach |
The while, for, and foreach Statements | Performs the action or group of actions in thier bodies zero or more times - if the loop continuation condition is initially false, the action or group of action will not execute |
The do...while statement | Performs the action or group of actions in its body one or more times |
Summary of Control Statements in C# | C# has only three kinds of structures control statements: the sequence statement, selection statement(three kinds), and repetition statements(four types). We combine as many of each type of statement as necessary to make programs flow and work as required |
The Two Ways in Which Control Statement Can Can be Connected | 1. Single-entry/single-exit control statements.. 2. Control-statement nesting |
Single-entry/single-exit Control Statements | Make it each to build apps. Control statements are "attached" to one another by connecting exit point of one to entry point of the next. This procedure is similar to the way in which a child stacks building blocks, so we call it control-statement stacking |
Control-Statement Nesting | A control statement appears inside another control statement |
Activity Diagram Decision Symbol | Perhaps the most important symbol in an activity diagram - the diamond, or decision symbol, which indicates that a decision is to be made |
Guard Conditions | Workflow will continue along path determined by one of the symbol's two associated guard conditions-one true and other false. Each transition arrow emerging from decision symbols have guard conditions(specified in square brackets next to transition arrow) |
1. Conditional Operator(?:) | C#'s conditional operator(?:) can be used in place of an if...else statement like the one diagrammed. This is C# only ternary operator - this means that it takes three operands. The operands and the ?: symbols form a conditional expression |
2. Conditional Operator(?:) | The first operand(to the left of ?) is a boolean expression(an expression that evaluates to a bool-type value - true or false). The second operand(between the ? and :) is the value of the conditional expression if the boolean is true. |
3. Conditional Operator(?:) | The third operand(to the right of the :) is the value of the conditional expression if the boolean expression is false. Ex: Console.WriteLine(grade >= 60? "Passed" : "Failed") |
Block | A set of statements contained within a pair of braces. A block can be placed anywhere in an app that a single statement can be placed |
Logic Errors | A logic error has its effect at execution time. A fatal logic error causes an app to fail and terminate prematurely. A nonfatal logic error allows an app to continue executing, but causes it to produce incorrect results |
Repetition Statement | Allows you to specify that an app should repeat an action while some condition remains true |
Infinite Loop | A logic error in which a loop never terminates because you do not provide in the body of the while statement an action that eventually causes the condition in the while to become false |
Merge Symbol | Shown as diamond which joins two flows of activity into one. Has two or more transition arrows pointing to diamond and only one transition arrow pointing from diamond to indicate multiple activity flows merging to continue activity. No guard conditions |
Counter - Controlled Repetition | This technique uses a variable called a counter(or control variable) to control the number of times a set of statements will execute. Often called definite repetition, because the number of repetitions is known before the loop begins executing |
A Total | A variable used to accumulate the sum of several values. Usually initialized to zero before being used in an app |
A Counter | A variable used to count |
A Definitely Assigned Variable | We say that a variable is definitely assigned when it's guaranteed to be assigned a value before the variable is used |
Integer Division | Dividing two integers results in integer division in which any fractional part of the calculation is lost(truncated, not rounded) |
Sentinel-Controlled Repetition | A sentinel value(also called signal value, a dummy value, or a flag value) indicates "end of data entry". Often called indefinite repetition because the number of repetitions is not known by the app before the loop begins executing |
1. Developing the Pseudocode Algorithm with Top-Down, Stepwise Refinement | Essential to the development of well-structured apps. We begin with a pseudocode representation of the top - a single statement that conveys the overall function of the app. |
2. Developing the Pseudocode Algorithm with Top-Down, Stepwise Refinement | The top is a complete representation of an app. The top rarely conveys sufficient detail from which to write a C# app. We divide top into a series of smaller tasks and list these in order to be performed. This result is called the First Refinement |
3. Developing the Pseudocode Algorithm with Top-Down, Stepwise Refinement | Each refinement, as well as the top itself, is a complete specification of the algorithm - only the level of detail varies. Many apps can be divided logically into three phases: an initialization phase that initializes the variables; |
4. Developing the Pseudocode Algorithm with Top-Down, Stepwise Refinement | a processing phase that inputs data values and adjusts variables(counters and totals) accordingly; and a termination phase that calculates and outputs and final results. To proceed to the next level, the second refinement, we specify individual variables. |
Error-Prevention Tip 5.2 | When performing division by an expression whose value could be zero, explicitly test for this possibility and handle it appropriately in your app rather than allowing the error to occur |
Cast Operator | Is a unary operator(takes only one operand). Formed by placing parenthese around the name of a type. Has precedence of one level higher than that of the multiplicative operators *, /, and % |
Number's Precision | The number of decimal places to the right of the decimal point. Any floating-point value output with F have two digits to the right of the decimal point |
1. Compound Assignment Operators | C# has compound assignment operators for shortening assignment expressions. Any statement of form, variable = variable operator expression;, where operators is one of the binary operators +, -, *, /, %, can be written in form variable operator= expression |
2. Compound Assignment Operators | For example, you can abbreviate the statement, C = C + 3;, with the addition compound assignment operator, +=, as, C =+ 3; |
Increment and Decrement Operators | C# provides two unary operators for adding 1 to or subtracting 1 from the value of a numeric value. These are the unary increment operator, ++, and the unary decrement operators, --. |
Prefix Increment and Decrement Operators | An increment or decrement operator that's prefixed to(placeed before) a variable. The variable's new value is used in the expression in which the variable appears |
Postfix Increment and Decrement Operators | An increment or decrement operator that's postfixed to(placed after) a variable |
Prefix Increment | Operator(++)..Sample Expression(++a)..Explanation(Increments "a" by 1 and uses the new value of "a" in the expression in which "a" resides) |
Postfix Increment | Operator(++)..Sample Expression(a++)..Explanation(Increments "a" by 1, but uses the original value of "a" in the expression in which "a" resides) |
Prefix Decrement | Operator(--)..Sample Expression(--b)..Explanation(Decrements "b" by 1 and uses the new value of "b in the expression in which "b" resides) |
Postfix Decrement | Operator(--)..Sample Expression(b--)..Explanation(Decrements "b" by 1 but uses the original value of "b" in the expression in which "b" resides) |
1. Precedence and Associativity of the Operators Discussed so far | 1. Operators(., new, ++(postfix), --(postfix))..Associativity(left to right)..Type(highest precedence)... 2. Operators(++,--, +, -, (type))..Associativity(right to left)..Type(unary prefix)... |
2. Precedence and Associativity of the Operators Discussed so far | 3. Operators(*, /, %)..Associativity(left to right)..Type(multiplicative)... 4. Operators(+, -)..Associativity(left to right)..Type(additive).. 5. Operators(<, <=, >, >=)..Associativity(left to right)..Type(relational) |
3. Precedence and Associativity of the Operators Discussed so far | 6. Operators(==, !=)..Associativity(left to right)..Type(equality)... 7. Operators(?:)..Associativity(left to right)..Type(conditional)... 8. Operators(=, +=, -=, *=, /=, %=)..Associativity(right to left)..Type(assignment) |