Whenever I start learning a new programming language, I focus on defining variables, writing a statement, and evaluating expressions. Once I have a general understanding of those concepts, I can usually figure out the rest on my own. Most programming languages have some similarities, so once you know one programming language, learning the next one is a matter of figuring out the unique details and recognizing the differences.
To help me practice a new programming language, I like to write a few test programs. One sample program I often write is a simple "guess the number" game, where the computer picks a number between one and 100 and asks me to guess it. The program loops until I guess correctly. This is a very simple program, as you can see using pseudocode like this:
- The computer picks a random number between 1 and 100
- Loop until I guess the random number
- The computer reads my guess
- It tells me if my guess is too low or too high
Recently, Opensource.com ran an article series that wrote this program in different languages. This was an interesting opportunity to compare how to do the same thing in each language. I also found that most programming languages do things similarly, so learning the next programming language is mostly about learning its differences.
C is an early general-purpose programming language, created in 1972 at Bell Labs by Dennis Ritchie. C proved popular and quickly became a standard programming language on Unix systems. Because of its popularity, many other programming languages adopted a similar programming syntax. That's why learning C++, Rust, Java, Groovy, JavaScript, awk, or Lua is easier if you already know how to program in C.
For example, look at how these different programming languages implement the major steps in the "guess the number" game. I'll skip some of the surrounding code, such as assigning temporary variables, to focus on how the basics are similar or different.
The computer picks a random number between one and 100
You can see a lot of similarities here. Most of the programming languages generate a random number with a function like rand()
that you can put into a range on your own. Other languages use a special function where you can specify the range for the random value.
C | Using the Linux getrandom system call:getrandom(&randval, sizeof(int), GRND_NONBLOCK); Using the standard C library: number = rand() % 100 + 1; |
C++ | int number = rand() % 100+1; |
Rust | let random = rng.gen_range(1..101); |
Java | private static final int NUMBER = r.nextInt(100) + 1; |
Groovy | int randomNumber = (new Random()).nextInt(100) + 1 |
JavaScript | const randomNumber = Math.floor(Math.random() * 100) + 1 |
awk | randomNumber = int(rand() * 100) + 1 |
Lua | number = math.random(1,100) |
Loop until I guess the random number
Loops are usually done with a flow-control block such as while
or do-while
. The JavaScript implementation doesn't use a loop and instead updates the HTML page "live" until the user guesses the correct number. Awk supports loops, but it doesn't make sense to loop to read input because awk is based around data pipelines, so it reads input from a file instead of directly from the user.
C | do { |
C++ | do { |
Rust | for line in std::io::stdin().lock().lines() { |
Java | while ( guess != NUMBER ) { |
Groovy | while ( … ) { |
Lua | while ( player.guess ~= number ) do |
The computer reads my guess
Different programming languages handle input differently. So there's some variation here. For example, JavaScript reads values directly from an HTML form, and awk reads data from its data pipeline.
C | scanf("%d", &guess); |
C++ | cin >> guess; |
Rust | let parsed = line.ok().as_deref().map(str::parse::<i64>); |
Java | guess = player.nextInt(); |
Groovy | response = reader.readLine() |
JavaScript | let myGuess = guess.value |
awk | guess = int($0) |
Lua | player.answer = io.read() |
Tell me if my guess is too low or too high
Comparisons are fairly consistent across these C-like programming languages, usually through an if
statement. There's some variation in how each programming language prints output, but the print statement remains recognizable across each sample.
C | if (guess < number) { |
C++ | if ( guess > number) { cout << "Too high.\n" << endl; } |
Rust | _ if guess < random => println!("Too low"), |
Java | if ( guess > NUMBER ) { |
Groovy | if (guess < randomNumber) |
JavaScript | if (myGuess === randomNumber) { |
awk | if (guess < randomNumber) { |
Lua | if ( player.guess > number ) then |
What about non-C-based languages?
Programming languages that are not based on C can be quite different and require learning specific syntax to do each step. Racket derives from Lisp and Scheme, so it uses Lisp's prefix notation and lots of parentheses. Python uses whitespace rather than brackets to indicate blocks like loops. Elixir is a functional programming language with its own syntax. Bash is based on the Bourne shell from Unix systems, which itself borrows from Algol68—and supports additional shorthand notation such as &&
as a variation of "and." Fortran was created when code was entered using punched cards, so it relies on an 80-column layout where some columns are significant.
As an example of how these other programming languages can differ, I'll compare just the "if" statement that sees if one value is less than or greater than another and prints an appropriate message to the user.
Racket | (cond [(> number guess) (displayln "Too low") (inquire-user number)] |
Python | if guess < random: |
Elixir | cond do |
Bash | [ "0$guess" -lt $number ] && echo "Too low" |
Fortran | IF (GUESS.LT.NUMBER) THEN |
Read more
This "guess the number" game is a great introductory program when learning a new programming language because it exercises several common programming concepts in a pretty straightforward way. By implementing this simple game in different programming languages, you can demonstrate some core concepts and compare each language's details.
Learn how to write the "guess the number" game in C and C-like languages:
- C, by Jim Hall
- C++, by Seth Kenlon
- Rust, by Moshe Zadka
- Java, by Seth Kenlon
- Groovy, by Chris Hermansen
- JavaScript, by Mandy Kendall
- awk, by Chris Hermansen
- Lua, by Seth Kenlon
And in non-C-based languages:
8 Comments