Procedural Programming

Saving the world, one line of code at a time.

  • Course Length: 3 weeks
  • Course Type: Short Course
  • Category:
    • Technology and Computer Science
    • High School
    • Middle School

Schools and Districts: We offer customized programs that won't break the bank. Get a quote.

Get a Quote

So you want to learn to program. Maybe...

  • some number-based cop dramas showed you how interesting and high stakes coding can be.
  • someone said, "If you love your computer so much, why don't you marry it?" and you decided to do the next best thing.
  • you want to build a robot to take over the world and enslave humanity.

Whatever your reason, you've come to the right place. (Except maybe that last one. We like our freedom, thanks.) We'll get you up and running code-wise in no time.

There are plenty of tutorials out there willing to teach you coding's variables and loops, but so many of them forget to tell you what you're learning in those first print statements. Programming isn't just about throwing together a bunch of variables and numbers. It's about learning how to break problems into concrete steps. You'll learn how to break down a problem and work through a solution one line at a time. Here, you're going to get it all:

  • algorithms
  • print statements
  • conditionals
  • loops
  • functions (also known as subroutines)

along with all the information about why we use each one. That way, when you start looking at other programming paradigms (like object-oriented, functional, and aspect-oriented), you'll have more grounding in why you'd choose one over another. And you won't even need to marry a computer to do it.

(Is that even legal?)

You'll also be applying all your knowledge to some programming projects. Think of them like proof of all the knowledge you've gained from the course. The Shakespeare-style insults you'll make in the final project? Those are just a bonus.

Next step: proposing to the computer.


Unit Breakdown

1 Procedural Programming - Procedural Programming

Procedural programming is the building block of computer science, taking coding one line at a time. With 15 lessons of procedure-filled goodness, we'll get you up and running with all the important info about ifs, ands, and whiles. (That's a coding keyword joke, BTW—you'll get it once you've taken this course.)


Sample Lesson - Introduction

Lesson 1.07: All that and a Loop

Multicolored roller coaster filled with loops.
Ride it enough times and you'll forget you even had a top hat.
(Source)

Rollercoasters have been around since the 1700's. Think about it for a second: men in top hats and women in hoop skirts being rocked around a rickety track on a bench.

That wasn't enough, though. When the initial thrill of the downhill roller coaster wore off, someone decided to add loops to the death traps. Passengers lost consciousness because of the g-forces. Worse yet: they lost their top hats.

Once engineers figured out how to make ones that didn't knock people out, loops became standard. Too standard.

Just like adding loops to rollercoasters, adding loops to our programs makes the code more interesting, minus the puking and loss of consciousness (most of the time). Loops allow statements to execute multiple times, instead of just once. By adding loops to our programming toolkit, we can write programs that do more.

No smelling salts required.


Sample Lesson - Reading

Reading 1.1.07a: Thrown for a Loop

Loops all have two parts, no matter how they're written:

  • A condition
  • Statements

In Python, your standard loop will look something like this.

loop condition:
	statement

The condition is a boolean—just like in conditionals—but this code runs slightly differently.

When a program encounters a loop:

  1. The test condition is evaluated. If the condition's true, the program runs the indented statements below the condition. 
  2. It then returns to the test condition to re-evaluate its truthfulness. If it's still true, then the program runs the statements again.
  3. When the condition turns false, the program heads to the next line of code outside the loop.

We're going to look at a few different types of loops (hence the name of the lesson), but that's always the basic outline.


Sample Lesson - Reading

Reading 1.1.07b: For Loops

For loops are—wait for it—loops. Not just any loops, though: these ones tell us just how many times we're going to be going through the loop. Say you want to find every integer between two numbers (and conveniently forgot how number lines work). With for loops you can easily see all the numbers…between two numbers. It's a lot more helpful than it sounds.

Let's use that example and see how it looks in Python:

smallest = 5
largest = 10

for i in range(smallest, largest):
	print(i)

This code is going to print:

5
6
7
8
9

A for loop repeats for a specific number of times—following that value of i.

Here's where Python's upper-level nature comes out to bite us. When we write for i in range(smallest, largest) we're creating a new variable (i), setting it to the value of smallest, and running the indented lines of code until i equals largest. In the for loop syntax, Python's actually hiding a line like this:

i += 1

After every run-through of the loop, called an iteration, i increases by one.

The general pseudocode for a loop will look like this:

for variable in range(start, end):
	statement
	statement
	statement

Or, if visuals are more your thing:

Flowchart of a for loop.

This chart might look complicated, but it does some pretty intuitive stuff. "Change" means that we set a variable—and change that variable at each iteration of the loop.

Each time the statements in the loop run is called one iteration. When we counted from 5 to 10, we went through—guess how many—five iterations.

The condition is a boolean that evaluates whether or not the loop should continue. It judges i before the statements run.

Change expression executes after each trip through the loop. This change expression either increases or decreases the value of i by an increment of one. It's hidden by Python, so you won't see it in this language, but in a language like Java or C, you'll need to pay attention to that.

For Loop Example

If that explanation was a jumble of words, let's look at another example.

for x in range(1, 20):
	print(x)

In this example, 1 to 19 will print to screen. Let's look at this example step by step to see what's happening:

  1. A loop variable, x, is created as equaling 1.
  2. Python checks to make sure x is still in the range of numbers between 1 and 20. Big surprise: it is.
  3. The statement inside the loop, print(x) prints x to the screen.
  4. x is incremented by one, so its value is now 2.
  5. Python checks the new version of x to make sure it's in range and finds that is still is.
  6. print(x) runs again.
  7. x is incremented by one, so its value is now 3.
  8. And on and on until x == 20. Once x evaluates to 20, x will no longer be in range because Python doesn't include the last number, so the program leaves the loop.

Up until now, if we wanted to create a program with repeated print statements, we'd need to write them all out. That isn't a huge deal if we're working with, say 14 print commands. But remember this, Shmooper: coding like that is really brittle. Take this code:

for i in range(0, 15):
	print(i)

What if we needed to print more than 14 print lines? What if we wanted to go from 1 to 100? Or even up to a million? Instead of writing print lines until our fingers bleed, now all we need to do is change the condition in the for loop.

for i in range(0, 100):
	print(i)

And now we have a hundred print lines. Think about how much easier it is to go from 100 to 56. Of course, if you really want to cut and paste 34 print lines instead of using a for loop, we guess you can do that. It's just way easier to make changes when you're using for loops.

Loops are also a better coding practice.

One more example before we go. Although it's common to increment or decrement for loops by one at each iteration, you can use any integer you want.

for x in range(7, 101, 7):
print(x)

In this program that final number in the range function tells Python to add seven at each iteration until x is greater than 100. Using a for loop makes stepping through a series of values as easy as…counting by sevens.

Did you notice something about all the numbers we used in that range function? They're all integers—not a float to be seen. That's because Python doesn't let you use floats (numbers with decimal values) in ranges; just whole numbers. You can get around it, though, if you…

You know what? You'll be able to work through it in the activity.


Sample Lesson - Activity

Activity 1.07: Just the Five of Us

While you're still here, let's write a program that does some counting—this time without the excessive print statements. Remember: a good loop needs a line something like this:

for i in range(start, end, increment):

For example, if Shmoop were writing a for loop counting from one to ten, we'd write:

for counter in range(1, 11):
	print(counter)

Now it's your turn.

  1. Write a loop that counts by 5's from 1 to 100. The answer should be 5, 10, 15, 20, 25, etc.

  2. Write a loop that displays numbers from 30 to 80, inclusive. That means both 30 and 80 should be part of the loop. Your output should be 30, 35, 40…75, 80.

  3. Challenge problem time? Challenge problem time. Write a loop that prints out every tenth of a digit numbers between 0 and -1. Your output should be 0, -0.1, -0.2, -0.3…-0.9, -1.0.