28
$\begingroup$

You've just demoed your first bit of code in whatever language you're teaching. The requisite "hello, world" has been said. The program has been compiled (or interpreted) and run. Now what?

There has to be a program that follows "hello, world" that builds upon the basics of printing one line on the screen.

What is the logical next step in an introduction to a new language and possibly to programming as a discipline in general? We all know program #0; what's program #1 and why?

Note: I know this is subjective, but I'm hoping it leans the good way. I'm trying to ascertain the scaffolding at an introductory level. Once the cliched example is complete (not that it doesn't have a lot to teach in and of itself), what makes sense as the next curricular step? I think the need for justification based on experience, expertise, and facts helps it from being too broad, but I'm happy to revise if necessary.

$\endgroup$
5
  • 4
    $\begingroup$ FYI: The traditional meaning of "hello world," has nothing to do with learning a new language (or a first language). It always was about learning a new toolchain. It was not a program that you wrote for yourself; It was a program that somebody gave to you. As in, "Here, when you can get this known-good source code to compile and run, then you will be ready to compile and run something of your own." $\endgroup$ Mar 31, 2019 at 19:51
  • 3
    $\begingroup$ P.S., I am not an educator, I'm a software developer. Been working almost forty years, and still go through the "Hello World" experience--proving that I can install, configure, and use the build tools for a new project--on a pretty regular basis. $\endgroup$ Mar 31, 2019 at 20:02
  • $\begingroup$ Same goes for hardware. But we do blinking LEDs instead of Hello World. $\endgroup$
    – Max
    Nov 9, 2021 at 9:13
  • $\begingroup$ A program that says: "Buckle your seatbelt Dorothy, 'cause Kansas is going bye bye" $\endgroup$
    – Scott Rowe
    Jan 7, 2022 at 22:13
  • $\begingroup$ If you're really going that far back, why are you advocating "hello, world" with no capitals? When you think the need for justification based on experience, expertise and facts "helps it from being too broad" what does that mean? When you're happy to revise, why not run two or three suggestions up the SE flagpole? $\endgroup$ Jan 8, 2022 at 23:41

17 Answers 17

23
$\begingroup$

When I was teaching and mentoring first-time students, what got them really excited for the first time was seeing their name in lights. Write a program that allows a student to enter their name, then print it on the screen 10 times (or 10,000).

  • It introduces the concept of variables and variable types
  • It shows how computers interact with the outside word by accepting input from the students
  • It introduces basic looping and conditional statements in a really simple example
  • It shows how math and computation works in a computer without simply spitting out basic algebra.

The best part is the students don't have to understand 100% of the program to start picking out the individual parts. The basic program flow is somewhat intuitive. You can start without the loop and add it in later as you introduce the concepts. This simple exercise is straightforward enough to teach the underlying concepts of how programming works in a somewhat entertaining, interactive application.

$\endgroup$
1
  • 1
    $\begingroup$ This is potentially #1, #2, #3, #4... $\endgroup$ Jun 2, 2017 at 15:48
13
$\begingroup$

I did a (rather unscientific) study of a few popular tutorials for Python to see what they did after "Hello, world":

  • Learn Python the Hard Way introduces expressions (25 + 30, 5 + 7 > 3 + 1), and implicitly hints at types (although never mentions them by name in the chapter after "Hello, world").

  • Codecademy introduces variables (assignment), then types, then reassigning variables.

  • Automate the Boring Stuff with Python introduces expressions and types.

  • Google's Python introduction introduces assignment, expressions, and very quickly moves on to functions. The pace of their tutorial seems a lot faster than the others I checked.

So, purely from what other tutorials and resources do, it seems that expressions are the most important next step. Notably, none of the resources I tested introduce input() in the next step—clearly, it's more important to learn about variables and how to store values from functions first.

If these resources are to be trusted, the best second program would look a little like this:

print('5 + 2 is', 5 + 2)
print('5 is less than 4?', 5 < 4)
eight = 2 * 4
print("2 * 4 is eight?", eight == 8) 

Note that in this example, you subtly introduce the fact that comparing values is done with ==, not =, and there's a link to bool values, int types, etc. Whether you explicitly mention them at first is up to you—some resources do recommend it, and some don't.

$\endgroup$
1
  • $\begingroup$ Yeah, when a student asked how X can equal X plus one I was a bit started. I mean, we get a year older each year, right? How else would it happen? $\endgroup$
    – Scott Rowe
    Jan 8, 2022 at 14:57
9
$\begingroup$

After "Hello World" it becomes "Hello \$name" where \$name is provided by user. After basic I/O and "it compiles and runs", then it is time for loops and logic. I typically like a variation on fizzbuzz:

Print the numbers 1 through 100, if the number is evenly divisible by 3 print "fizz" next to the number, if evenly divisible by 5 print "buzz" next to the number, if evenly divisible by both 3 and 5 then print "wahoo!" next to the number.

$\endgroup$
0
7
$\begingroup$

I was taught that there were three main aspects of programming:

  1. input-output,
  2. computation, and
  3. logic/control

"Hello world" is an "input-output" function, specifically a "print" command. The next thing I would teach is a simple computation, even something like 1+1=2.

For example:

X=1
Y=X+X
Print Y
$\endgroup$
1
  • $\begingroup$ TBH logic/control should come first - but in order to see it happen and make sure it works, you need the I/O too (well, at least the O) $\endgroup$
    – ivanivan
    Jun 22, 2017 at 23:43
7
$\begingroup$

I've always used Hello name" as part of Hello World. It's easy enough for most students and makes the program more fun. After Hello World I use a conversion program. For example Fahrenheit to Celsius. That makes a lot of sense in the US but might not in other countries. But there are many conversions that make local sense in other parts of the world. Pounds Sterling to Euros perhaps. Students often like money problems.

Conversion programs involve both input and output but add the introduction of assignment statements and calculations. More involved calculations allow for a review of the order of operations (one reason I use Fahrenheit to Celsius). Students get interesting results if they don't set up the formula correctly.

Conversion programs also let one introduce testing early in the game. Sure the program compiled, ran, and gave you an answer but how to you know it is the right answer?

$\endgroup$
6
$\begingroup$

There's absolutely no reason your first program needs to be "hello world" in the first place.

You can start with values of various kinds, and show how to combine them to create more interesting values. For instance, in How to Design Programs, we start out with numbers, strings, and images.

Pretty quickly students can start to create interesting pictures: e.g., flags of various countries, or a scene (imagine a beach with a tree and a sun). Now they can start to change the size of that flag or parts of that scene (e.g., the sun is at different positions).

Next they can make a function that given a parameter, produces the flag or scene at that time (by using the parameter to calculate the position of the sun). Now they supply this to a function called animate and they have their first animation. From there they can go on to much more sophisticated animations.

In short, we think a rocket launching off a pad — a good metaphor for their education — is a pretty nice follow-up, and not that hard to do. The book's prologue (http://www.ccs.neu.edu/home/matthias/HtDP2e/part_prologue.html) shows this progression.

$\endgroup$
5
$\begingroup$

You program with images, create the flags of several countries, then lay them over backgrounds, then animate them, and build up to a game. You can do all that in the first week or so of your course if you have the right curriculum. See, for instance, the prologue of How to Design Programs (second edition).

$\endgroup$
3
  • 2
    $\begingroup$ +1 for DrRacket which I love as an environment and would love to teach with some day. $\endgroup$
    – Peter
    Jun 3, 2017 at 1:19
  • $\begingroup$ If you're at the middle-/high-school levels, take a look at the Bootstrap family of curricula. There's something in there for everyone, and cut from the same cloth. $\endgroup$ Jun 3, 2017 at 11:52
  • 1
    $\begingroup$ This does not seem like a program that would come right after "Hello World", it seems more complex than that. $\endgroup$
    – thesecretmaster
    Jun 29, 2017 at 12:12
4
$\begingroup$

The next step is to introduce the type system. Students need to know what kinds of data are "understood" by a language. The "Hello World" program gets the ball rolling because you can ask, "How is text stored? Are there other types of data that can be stored as well?"

$\endgroup$
4
$\begingroup$

I think that a reasonable next step is showing them variables with an I/O basis. Things like:

  • using "Hello, world!" or something similar as a string and printing it

  • using integers and showing students the different operators for the language (add/subtract/multiply/divide/modulus division/integer division)

$\endgroup$
4
$\begingroup$

I very much like "The Devil and Daniel Webster", as introduced to me by Devika Subramanian. It connects programming to mathematics, and introduces lists, loops, and if conditions in a fairly simple wait, with an interesting story.

The lecture notes I've found for them are below, but the problem is:

The devil made a proposition to Daniel Webster. The devil proposed paying Daniel for services in the following way:"On the first day, I will pay you \$1,000 early in the morning. At the end of the day, you must pay me a commission of $100. At the end of the day, we will both determine your next day's salary and my commission. I will double what you have earned at the end of the day, but you must double the amount that you pay me. Will you work for me for a month?""

https://sites.google.com/site/publiccomp140site/week-1/the-devil-and-daniel-webster-lecture-notes

$\endgroup$
4
  • $\begingroup$ I don't entirely understand the problem. Do we have some way of determining what D.W. will earn? I'm any case, welcome to Computer Science Educators! $\endgroup$
    – Ben I.
    Jun 30, 2017 at 12:25
  • $\begingroup$ It's just the payout minus the commission: so 900 for the first day. $\endgroup$
    – sfogarty
    Jul 1, 2017 at 21:23
  • $\begingroup$ So, on the second day, he would earn \$1,800, but have to pay \$200? $\endgroup$
    – Ben I.
    Jul 1, 2017 at 21:25
  • $\begingroup$ Exactly. Have them turn it into a pair of formulas for each day with subscripted variables. Then we start writing code, and see that we'd end up with 60-odd variables. Introduce lists as a way of having variables with subscripts. Then build up lists manually a few times, and introduce them to for loops. Then at the end you can use an if, outside the loop, to check the final values. Lists, for loops, and if statements. For my non-majors class it takes three 70-minute classes to get through, but it presents a coherent (if fanciful) problem to motivate them. $\endgroup$
    – sfogarty
    Jul 2, 2017 at 22:06
4
$\begingroup$

Consider also going the other way.

We often think of teaching from small to big. However it is often possible and beneficial to go from big to small. Bertrand Meyer does this in his course “Touch of Class”.

Provide the students with a working program, have them read the program, make some predictions on what it does, then run it, determine what some of the key grammar/vocabulary does, make predictions on what changes will do, then make changes and re-run.

$\endgroup$
2
  • 1
    $\begingroup$ I think this is an important answer. $\endgroup$
    – Ben I.
    Aug 23, 2017 at 14:11
  • $\begingroup$ But there are quite some preconditions before this can happen. $\endgroup$ Jan 10, 2022 at 3:12
4
$\begingroup$

I found 'FizzBuzz' really helpful as the next exercise, it has conditions, loops and it also allows you to explain some coding practices, especially DRY (Don't Repeat Yourself).

"Write a short program that will simulate playing FizzBuzz. The rules are simple:

  • Count from 1 to 100 and print the numbers on screen
  • If the number is divisible by three, print Fizz instead of the number
  • If the number is divisible by five, print Buzz
  • If the number is divisible by both three and five, print FizzBuzz

Write as few lines of code as possible."

This is also an exercise that was used a lot on job interviews to determine if someone could actually code and apply good practices.

And for an in-depth explanation on what constitutes a good result: Tom Scott: FizzBuzz

$\endgroup$
1
3
$\begingroup$

I agree with the answer proposing Hello name, and the answer that proposes students inputting their name and then repeating it x times. Then a conditional can be introduced that does or does not print their name depending on some boolean expression or similar. At this point, we would have introduced: types, input (and output), repetition, conditionals, and possibly some mathematics.

$\endgroup$
2
$\begingroup$

You might find some more ideas here:

My approach is designed for Processing, but hopefully it generalizes to other languages.

Hello World is really a lesson in how to call functions. After they understand the basics, I'd show them how to lookup other functions in either the language reference or in a beginner-friendly list that you give them. In Processing, I'd have them draw a circle or a rectangle, or an entire scene using basic function calls. If your hello world is a console application, then maybe have them display a popup box, or display a message in a window. The specifics of this depend on the language you're using, but the idea is to get them looking stuff up in a reference and playing around with their own code.

From there I'd go to using variables. This is different than creating variables. Instead of having students create variables, have them look up whatever predefined variables your language offers. Processing has width and height variables, so I'd have them make their scene scale to the size of the window. But you would use whatever variables your language exposes. Again, have them look stuff up in the reference or in a list you provide them.

From there I'd have them create their own variables and their own functions, which I've explained in more detail in the above questions. But if you're just looking for a follow-up to Hello World, I'd use the above approach of revealing more of the reference and asking them to do their own projects based on what they just learned.

$\endgroup$
1
$\begingroup$

Existing answers have great suggestions...

There are many wonderful answers here, and I agree that, in general, "hello ${name}" to introduce dynamic input/output and string concatenation (a natural lead-in to if/else and something like a small quiz app), currency/temperature convertors to introduce mathematical operations and types are great second tasks. A temperature converter with hardcoded values is a refactor away from collecting input, introducing functions, conditions (prompt for Fahrenheit to Celsius or vice-versa) and loops (prompt for multiple temperature conversions).

I also like the idea of prematurely showing students code from a larger project they aren't expected to understand, with the idea of making a few small modifications and exposing them to the look and feel of the sort of applications they'll soon be creating. There's a risk of intimidation, so care would need to be taken, and eventually you'll have to pull back to a "next hello world"-sized step anyway.

... but what about audience, context and goals?

However, these answers seem to assume an audience of entry-level programming students. As pointed out in the comments, plenty of advanced tutorials and courses might begin with a "hello world"-style task.

For example, in back end web development, any first-time Ruby on Rails tutorial will begin with getting the "Congratulations! You're on Rails!" screen up on localhost. For machine learning libraries, a classifier using the iris dataset is a canonical first order of business.

The point here is that audience, language, framework and context matters in choosing a second task, and "hello world" isn't exclusive to beginning programmers or console-based contexts.

With that in mind, a few answers recommend FizzBuzz after "hello world". Initially, this struck me as rushing in too many fresh concepts that can take time for beginners to grasp: loops, booleans, branching and string manipulation. But if the audience has prior programming experience, FizzBuzz might be a reasonable difficulty for them. And if not, you can always build up, adding the conditions slowly, then the loop.

I recently began learning Haskell from "hello world", and found that I wrote a dozen or so smaller programs before I was able to write a FizzBuzz that accepted input, to cite an example of the language factor.

Working backward from the goals of the course, the next step.

I'll continue onward assuming beginners, though, since follow-ups to "hello world"s for experienced developers looking to learn the latest framework would be a case-by-case basis.

In any situation, working backward from the goals of the course, the next step after "hello world" might become obvious.


A "10 PRINT" approach

Answers so far mostly offer a command-line style, general-purpose task that would work in basically any language. But many languages (particularly educational ones like Racket, Scratch and BASIC) offer interesting features that make it easy to do relatively engaging and powerful things without much effort. One suggestion is to try to find and exploit these in whatever environment you're working in. In particular, I find that graphical programs, and particularly animations, are a great way to engage students, even if only ASCII.

A classic example is 10 PRINT, a simple BASIC program that produces a cool animation

10 PRINT CHR$(205.5+RND(1)); : GOTO 10

10 PRINT is not always as easy to achieve in other languages and environments (it exploits aesthetics of the environment's screen, native text wrapping, an easy-to-write infinite loop, and an easy-to-call random number generator), but there's often some other "hack" that happens to create a disproportionately cool effect of some sort in your language.

For most scripting languages, it's pretty easy to provide boilerplate to do animations. In Python:

from time import sleep

while True:
    print("hello world!")
    sleep(1)

Working with this basic framework, you can introduce numbers, math operations and string manipulation to add dynamism to the animation (and later on, explain the loop and import in more detail; it's good early exposure):

from time import sleep

i = 0
while True:
    print(i % 10 * "@")
    i += 1
    sleep(0.1)

The * operator in Ruby and Python and x in Perl shows another easy trick for one-upping "hello world". String repeat functions, when available, are fantastic: "Na" * 10 + " Batman!". This works great with the print your name suggestion elsewhere in the thread.

We can try 10 PRINT in Python (although the results won't be too impressive without the C64 monitor; consider adjusting terminal color and typeface prior to the session):

from time import sleep
from random import choice

while True:
    print(choice(r"\/"), flush=True, end="")
    sleep(0.01)

or print the "\b" character to make a little animation in-place:

from itertools import cycle
from time import sleep

for c in cycle("what's up?"):
    print(c, flush=True, end="")
    sleep(0.5)
    print("\b", end="")

or:

for s in cycle(("hello", "world")):
    print(s, flush=True, end="")
    sleep(1)
    print("\b" * len(s), end="")

Even if students won't understand the code out of the box, they can play around with the string contents and speed, building intuition for what the shape of the code looks like and how it can create something dynamic and interesting with just a few lines.

Al Sweigart has a repository of scroll art scripts for further inspiration, although most are more than a step after "hello world".

Other ideas

Many languages have turtle or another minimal graphics library that lets you draw a circle with 2-3 lines of code and might be a great next step after "hello world". This is a great choice for students coming from a Scratch background who may be prone to frustration at the relative difficulty of doing anything "fun" with text-based languages.

In HTML, I find that a <pre> tag can open a world of creative options for ASCII art without having to learn coding concepts immediately, similar to stacking a bunch of print statements or a multiline string to create an ASCII banner.

The nonstandard <marquee> tags, although technically horrible, helped turn my generation on to web development, and are a great way to jazz up "hello world" (when supported). Sure, HTML is probably not relevant for most curricula here, but concepts like this can often inspire ideas for other languages.

CSS offers powerful animation and transition techniques with just a line or two of code. For example, providing something like

<style>
body {
  transition: all 2s;
}
body:hover {
  background: black;
}
</style>

can get students interacting with their app and provides a foundation for adding CSS properties, creating new elements and designs.

For front-end JS, students can easily use the browser's development tools to manipulate content on Wikipedia or another popular website. For example, without writing code, they can change text to humorous effect while being exposed to the syntax and structure of HTML.

If you don't mind sharing code students might not understand at first but will surely get a kick out of, you can try something like:

document.querySelectorAll("*").forEach(e => {
  e.style.background = "#" + Math.random().toString(16).slice(-6);
});

which sets all element backgrounds to random colors on a page, creating a glitchy art effect and hopefully giving students an idea of what's possible in the spirit of 10 PRINT. Snippets like these can open up conversations about creative coding, computer art and history (even if those aren't the focus of the course).

It's OK to make a mess

Following the <marquee> principle, in most cases, it should be fine at this stage to let students make a mess and have fun, duplicate code everywhere, then enforce better software engineering principles later. Ugly and outside-the-box is encouraged. For web programming, grabbing snippets from w3schools is OK.

For example, I've seen students push a few of their laptops together in a row and sync up the timing of their animations to make it look like text is scrolling between monitors! Encouraging creative, emergent collaboration among students and the tools in the environment enables students to create interesting experiences even with small amounts of code, as 10 PRINT does by exploiting aesthetic qualities of typeface and monitor.

$\endgroup$
1
$\begingroup$

You say you're looking for a program

that builds upon the basics of printing one line on the screen.

I don't look at "hello world" as a basis to build upon. It's more of a sanity check: can the student use an editor, compile, run a program. The next program does not need to build on it per se.

So for my students the next step is the "Collatz conjecture". For that

  1. They need expressions: 3*n+1, n/2
  2. Conditionals: `if (n%2==0) n=n/2; else n=3*n+1;
  3. Loop: `while (n!=1) /* the above */
  4. Nested loops: find the starting point (say under 1000) with maximum length.

And those do all built on each other. Prime number testing/finding is also a good one.

$\endgroup$
-2
$\begingroup$

Historically, Hello, <name>, but I think I'm going to go with something like hangman, snake, guess the number... I'm leaning more and more to "hacking" existing programs rather than learning bit by bit.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.