CS C++ Control Structures
KAMSC Computer Science: Concepts from the Control Stuctures Chapter 2 of Deitel Packet
Terms in this set (53)
A procedure for solving a problem in terms of the actions to be executed and the order in which these actions are to be executed
Specifying the order in which statements are to be executed in a computer program.
An artificial and informal language that helps programmers develop algorithms. Similar to everyday English, is convenient and friendly although it is not an actual computer programming language. Helps programmers "think out" a program. Consists only of executable statements.
those that are executed when the program has been converted from pseudocode to C++ and is run. Declarations are not these.
Statements in a program are executed one after the other in the order in which they are written.
Transfer of Control
Valid C++ statements enable the programmer to specify that the next statement to be executed may be other than the next one in sequence.
Allows the programmer to specify a transfer of control to one of a very wide rang of possible destinations in a program.
Three types of control structures
Sequence structure, selection structure and repetition structure
Graphical representation of an algorithm or of a portion of an algorithm. Drawn using certain special-purpose symbols such as rectangles, diamonds, ovals, and small circles.
Arrows in flowcharts
Pseudocode or Flowcharts
Pseudocode is strongly preferred by most programmers.
Flowchart: Action symbol
Rectangle symbol indicates any type of action including a calculation or an input/output operation.
Flowchart: Oval symbol
In complete algorithm, this symbol containing the word "Begin" is the first symbol and containing the word "End" is the last symbol. When drawing only a portion of an algorithm, these are omitted for small circle symbols.
Flowchart: Connector symbols
Small circle symbols for drawing only a portion of an algorithm.
Flowchart: Decision symbol
Diamond symbol that indicates a decision is to be made.
Three selection structures
If, If/else, Switch
If selection structure
Either performs (selects) an action if a condition is true or skips the action if the condition is false.
If/else selection structure
Performs an action if a condition is true and performs a different action if the condition is false.
Switch selection structure
Performs one of many different actions depending on the value of the expression.
Selects or ignores a single action. (If)
Selects between two different actions. (If/else)
Selects the action to perform from many different actions. (Switch)
Three Repetition Structures
While, do/while, for.
Words reserved by the language to implement various features such as C++'s control structures. May not be used as identifiers such as for variable names.
Examples of C++ keywords
if, else, switch, while, do, for, break, continue, try, delete, operator, class, false, namespace, bool, new, true
C++ seven types of control structures
sequence, three types of selection and three types of repetition
Single-entry/single-exit control structures
control structures are attached to one another by connecting the exit point of one control structure to the entry point of the next. Control-structure stacking.
Control structure nesting
Only other way control structures may be connected besides stacking.
C++ ignores these
Whitespace characters like blanks, tabs and newlines used for indentation and vertical spacing.
Data type to represent true and false. The keywords true and false are used to represent values of this.
Action/decision model of programming
Flowcharts only contain rectangle symbols to indicate actions to be performed and diamond symbols to indicate decisions to be made.
( ? : ) C++'s only ternary operator, it takes the three operands. The operands together with the conditional operator form a conditional expression. The first operand is the condition, the second operand is the value for the entire conditional expression if the condition is true and the third operand is the value for the entire conditional expression if the condition is false. For example: cout << ( grade >= 60 ? "Passed" : "Failed" ); this evaluates to the string "Passed" if grade >= 60 and "Failed" if condition is false.
More popular version of nested if/else structures
else if (condition)
else if (condition)
etc. This avoids deep indentation
Caught by the compiler, error in the computer language.
Has its effect at execution time.
Fatal logic error
Causes a program to fail and terminate prematurely
Nonfatal Logic error
Allows a program to continue executing but the program produces incorrect results.
While repetition structure
Action will be performed repeatedly while the condition remains true.
Uses a variable called a counter to control the number of times a set of statements will execute. Often called definite repetition because number of repetitions is known before the loop begins executing.
A variable used to accumulate the sum of a series of values. Should be initialized to zero before start of program.
Contains a "garbage" value (also called an undefined value) - the value last stored in the memory location reserved for that value.
Also called a signal value, dummy value, flag value. Indicates end of data entry. User types in values until all have been entered, then type in this value to indicate last value has been entered. Also called indefinite repetition.
Top down stepwise refinement
A technique that is essential to the development of well-structured programs.
A single statement in top down stepwise refinement that conveys the overall function of the program. Rarely contains enough information to code the entire C++ program.
Divide the top into a series of smaller tasks and list these in the order in which they need to be performed. There are multiple versions of these that go into further detail each time.
Three phases of programs.
Initialization phase, processing phase (inputs data and adjusts program variables accordingly) and termination phase (calculates and prints final results)
Lists tasks in order they need to be completed.
Commits to specific variables.
Called "bombing" or "crashing"
Integer data types
Data type to handle numbers with decimal points.
When fractional part of value is lost.
Unary cast operator
Creates temporary values that are floating point values.