Tech

Control Statements–STRUCTURED PROGRAMMING Course Notes

CONTROL STATEMENTS

  • Theory & Principles of Structured Programming.
    • These concepts presented here are crucial to building effective classes & manipulating objects.

BEFORE writing a program to solve a problem, we must have a thorough understanding of the problem & a carefully planned approach to solving it.

WHEN writing a program, we must also understand the types of building blocks that are available, & employ proven program construction techniques.

An ALGORITHM is a procedure for solving a problem in terms of the actions to execute and the order in which to execute them.

Specifying the order in which statements execute in a program is called PROGRAM CONTROL.

Note: In addition to solving problems, we also need to know & understand solution evaluation techniques.

In the end, there are often many ways to solve a problem! Finding a solution & then deciding whether it is a good one are tasks we will do over and over again.

Pseudocode helps us think out a program before writing it in a programming language.

Top-down, stepwise refinement is a process for refining pseudocode by maintaining a complete representation of the program during each refinement.

  • There are 3 TYPES of CONTROL STRUCTURES:
    1. Sequence
    2. Selection
    3. Repetition
  • 1). The Sequence Structure is built-in-by default, statements execute in the order they appear.
  • 2). A Selection Structure chooses among alternative courses of action.
  • 3). A Repetition statement repeats an action while some condition remains true.

Structured Programming

  • Any form of control can be expressed in terms of sequence, selection, & repetition statements, and these can be combined in only two ways–stacking and nesting.
  • Any algorithm can be developed using:
    • combinations of the sequence structure,
    • the 3 types of selection statements, (‘if’, ‘if…else’, and ‘switch’ statements),
    • and the 3 types of repetition statements (‘while’, ‘do…while’, and ‘for’ statements).
    • You can combine these building block to utilize proven program construction & problem-solving techniques.
  • Again, only 3 types of control structures–sequence, selection, and repetition–are needed to develop any algorithm!!!
  • Activity diagrams are part of the UML (Unified Modeling Language)–an industry standard for modeling software systems.
    • An activity diagram models the workflow (aka–the activity) of a software system. Like pseudocode, activity diagrams help you develop & represent algorithms.
  • Structured programming produces programs that are easier than unstructured programs to understand, test, debug, modify, and even prove correct in a mathematical sense.
  • Structured programming promotes simplicity.

Software Engineering Observation 4.3–Experience has shown that the most difficult part of solving a problem on a computer is developing the algorithm for the solution! The process of producing a working C++ program from the algorithm is typically straight-forward.

Rules for Forming Structured Programs:

  1. Begin with the “simplest activity diagram”.
  2. Any action state can be replaced by two action states in sequence.
  3. Any action state can be replaced by any control statement (sequence, ‘if’, ‘if…else’, ‘switch’, ‘while’, ‘do…while’, or ‘for’).
  4. Rules 2 and 3 can be applied as often as you like and in any order.

Structured Programming

Again, Structured Programming promotes simplicity. Bohm and Jacopini have given us the result that only three forms of control are needed:

  • Sequence
  • Selection
  • Repetition

The sequence structure is trivial. Simply list the statements to execute in the order in which they should execute.

  • Selection is implemented in one of three ways:
    • if‘ statement (single selection)
    • if…else‘ statement (double selection)
    • switch‘ statement (multiple selection)
    • Note: The simple ‘if‘ statement is sufficient to provide any form of selection–everything that can be done with the ‘if…else‘ statement and the ‘switch‘ statement can be implemented (although perhaps not as clearly and efficiently) by combining ‘if’ statements.
  • Repetition is implemented in one of three ways:
    • ‘while’ statement
    • ‘do…while’ statement
    • ‘for’ statement
    • Note: Repetition statements are also called “looping statements” or “loops”.
    • Note: It’s straightforward to also prove that the ‘while’ statement is sufficient to provide any form of repetition. Everything that can be done with the ‘do…while’ statement and the ‘for’ statement can be done (although perhaps not as smoothly) with the ‘while’ statement.
    • Combining these results illustrates that any form of control every needed in a C++ program can be expressed in terms of: sequence, ‘if’ statement (selection), and ‘while’ statement (repetition), and these control statements can only be combined in two ways–stacking & nesting. Indeed, structured programming promotes simplicity.

Counter-Controlled Repetition

Counter-Controlled Repetition requires:

  1. the name of a control variable (or loop counter)
  2. the initial value of the control variable
  3. the loop-continuation condition, that tests for the final value of the control variable (i.e.–whether looping should continue)
  4. the increment (or decrement) by which the control variable is modified each time through the loop.

Counter-controlled repetition uses a variable called a counter to control the number of times a group of statements will execute (aka–the number of iterations of the loop).

Counter-controlled repetition is often called definite repetition, because the number of repetitions is known before the loop begins executing.

Note: Control counting loops with whole integer values, not floating-point values. Floating-point values are approximate, so controlling counter loops with floating-point variables can result in impressive counter values & inaccurate tests for termination.

A total is a variable used to accumulate the sum of several values.

A counter is a variable used to count.

Initialize each counter & total, either in its declaration or in an assignment statement. Totals are normally initialized to zero. Counters are normally initialized to zero or one, depending on how they’re used.

Sentinel-Controlled Repetition uses a special value called a “sentinel value” (also called a signal value, a dummy value, or a flag value) to indicate “end of data entry”.

Sentinel-Controlled Repetition is often called indefinite repetition because the number of repetitions is not know before the loop begins executing.

  • Many programs can be divided logically into three phases:
    • an initialization phase that initializes the program variables
    • a processing phase that inputs data values and adjusts programs variables (such as counters and totals) accordingly;
    • and a termination phase that calculates & outputs the final results.

Assignment Operators

C++ provides several assignment operators for abbreviating assignment expressions.

Assignment operatorSample expressionExplanationAssigns
Assume: int c=3, d=5, e=4, f=6, g=12;
+=c += 7c = c + 710 to c
-=d -= 4d = d – 41 to d
*=e *= 5e = e * 520 to e
/=f /= 3f = f/32 to f
%=g %= 9g = g % 93 to g
Arithmetic assignment operators

Increment & Decrement Operators

In addition to the arithmetic assignment operators, C++ also provides two unary operators for adding 1 to or subtracting 1 from the value of a numeric variable.

These are the unary increment operator, ‘++‘, and the unary decrement operator, ‘‘, summarized below. A program can increment by 1 the value of a variable called ‘c’ using the increment operator, ++, rather than the expression c=c+1 or c+=1.

An increment or decrement operator that is prefixed to (placed before) a variable is referred to as the ‘prefix increment‘ or ‘prefix decrement’ operator, respectively.

An increment or decrement operator that is postfixed to (placed after) a variable is referred to as the postfix increment or post-fix decrement operator, respectively.

OperatorCalled Sample expressionExplanation
++pre-increment++aIncrement ‘a’ by 1, then use the new value of ‘a’ in the expression in which ‘a’ resides.
++post-incrementa++Use the current value of ‘a’ in the expression in which ‘a’ resides, then increment ‘a’ by 1.
pre-decrement–bDecrement ‘b’ by 1, then use the new value of ‘b’ in the expression in which ‘b’ resides.
post-decrementb–Use the current value of ‘b’ in the expression in which ‘b’ resides, then decrement ‘b’ by 1.
Increment and decrement operators.

‘for’ Statement Header Components

for ( int counter = 1; counter <= 10; counter++ )

  • In the above line of code:
    • for‘ is the ‘for’ keyword
    • counter‘ is the Control variable name
    • 1‘ is the Initial value of control variable
    • ;‘ are the required semicolon separators
    • counter <= 10;‘ is the loop-continuation condition
    • counter++‘ is the increment of control variable

The general form of the ‘for’ statement is:

  • for (initialization; loopContinuationCondition; increment )
    • statement

where the ‘initialization‘ expression initializes the loop’s control variable, ‘loopContinuationConditiondetermines whether the loop should continue executing, and ‘incrementincrements the control variable.

In most cases, the ‘for‘ statement can be represented by an equivalent ‘while‘ statement, as follows:

  • initialization;
  • while ( loopContinuationCondition )
    • {
      • statement
      • increment;
    • }