Link Search Menu Expand Document

Programming Structures

There are a few basic programming structures that help us write more compact, efficient programs that are easier to write and maintain. Think of these structures as a way to organize blocks of code that perform sub-tasks in a larger sequence of tasks. The basic structures provide a way to repeat blocks of code (loops), decide which blocks of code to skip or not skip (if constructs), and reuse blocks with possible different inputs (functions).

Table of contents

  1. Loops
  2. if constructs
  3. functions

Loops

A programming loop is a block of code that repeats. The two common types of loops are for loops and while loops. Typically, a for loop is used when the number of repetitions is known in advance and a while loop is used when an unknown number of repetitions continue until a condition is met.

Read more about for and while loops on this page.

if constructs

Programs that respond to changing conditions need to have ways of making decisions. When one set of conditions is true, a predefined block of code is executed. If alternative conditions are present, a different block of code is executed. The general concept of responding to changes in data is called branching. This logical decision-making is implemented with if constructs and is described on this page.

functions

In programming, functions are modules of code. In a complex code that performs several distinct tasks, it is usually a good idea to write a function for each major task (or set of tasks). Functions can have inputs that control how they work. Functions can also return values.

Programming languages have a set of built-in functions for performing common tasks. Examples are mathematical functions like cos(x) and sin(x) and functions that print values to an external device.

Read more about functions on this page.