Previous page Next page Navigation bar

Calculator Programming Tutorial

Programming Building Blocks I

Control Flow I

Introduction

Consider again our friend the flowchart of the factorial function that we first met in the Programming as Process Design page.

Flowchart of factorial algorithm; click for description

This flowchart describes a procedure which, when performed, results in the computation of the factorial function:

n! = n × n-1 × ... × 1

However, the flowchart describes a procedure, rather than is a procedure. The flowchart is static, a picture on a page, while the procedure is dynamic, a set of actions. The procedure — the computation of the factorial function — has attributes that the flowchart does not. In particular, the computation has state, a set of current data.

Some aspects of this state are alluded to in the flowchart. The variables a, b, and n are all components of the computation’s state. The expressions a > n, ab, and a + 1 are all computations whose values are, for a time, part of the computation’s state. But another, more subtle, aspect of the computation’s state is staring us right in the face, but the flowchart carries no indication of this component: the computation’s location or current position in the flowchart.

The flowchart is a sequence of computations and steps which must be carried out in precisely the right order. The test a > n cannot be made before the initial assignment of 1 to a has been carried out. The assignment of ab to b, the assignment of a + 1 to a, cannot be carried out before the test a > n has been made. The return b statement must happen after the a > n test has first succeeded, not before; and the assignments of ab to b and a + 1 to a must not occur after the a > n test has first succeeded.

The flowchart combines computation with what is called control flow to yield the answers of interest. Control flow, or flow of control, is how the current position in the flowchart changes over time.

There are three necessary and sufficient types of control flow that permit all programs to be written: sequence, alternative, and pre-tested loop. However, there are a number of variations on these three types that serve as useful idioms or abbreviations of commonly used control flows. This section, Control Flow I, describes the three basic types of control flow.

[ Previous page | Top of page | Next page ]

Previous page Top of page Next page Navigation bar

Copyright © 2001 Brian Hetrick
Page last updated 30 December 2001.

Brian’s Casio Calculator Corner

Home

Programs

Tutorial

Preface

Introduction

Fundamentals

Building Blocks I

Introduction

Comments

Data Types

Numbers

Variables

Expressions

Control Flow I

Introduction

Sequences

Alternatives

Loops

Composition

Control Flow II

Subprograms

Basic I/O

Algorithms

A First Program

Examples

Exercises

Answers

Modularization

Data Structures I

Recursion

Program Attributes

Building Blocks II

Algorithm Analysis

Structuring

Data Structures II

Abstract Types

Objects

Problem Analysis

Reference Card

References

Puzzles

Site Information

Your Privacy

Site Map

E-mail

Site Technical Data