# How different programming languages ​​accomplish the same thing

Introduction: Compare 13 programming languages ​​through a simple little game.

When I start learning a new programming language, I focus on defining variables, writing declarations, and evaluating expressions, and once I have a general understanding of these concepts, I can usually figure out the rest on my own. Most programming languages ​​have similarities, so if you have mastered one programming language, the point of learning the next language is to figure out the unique concepts and distinguish the differences.

I like to write test programs to help practice new programming languages. Among them, what I often write is a small game called “guess the number”. The computer chooses any number from 1 to 100, and then I guess. The program loops until the correct number is guessed. As you can see from the pseudocode, this is a very simple program:

◈ The computer picks a random number between 1 and 100

◈ Loop until the random number is guessed

◈ The computer reads my guess

◈ Tell me if my guess is too high or too low

We published some articles writing this program in different languages. This is an interesting opportunity to compare different languages ​​doing the same thing. Most programming languages ​​share similarities, so when you’re learning the next new programming language, it’s mostly about its uniqueness.

Created by Dennis Ritchie at Bell Labs in 1972, C is an early general-purpose programming language. The C language was very popular and quickly became the standard programming language on Unix systems. It is because of its popularity that many other programming languages ​​have adopted a similar programming syntax. That’s why it’s easier to learn C++, Rust, Java, Groovy, JavaScript, awk, or Lua if you already know how to program in C.

Next we look at how these different programming languages ​​implement the main steps of the “number guessing” game. I’ll focus on the similarity or difference of the basic elements, skipping some peripheral code like assigning temporary variables.

The computer picks a random number between 1 and 100

You can see many similarities here. Most programming languages ​​use functions like `rand()` where you can set a range to generate random numbers. And some other languages ​​use a special function to set the range to generate random numbers.

C:

1. `// Using the Linux `getrandom` system call`
2. `getrandom(&randval, sizeof(int), GRND_NONBLOCK);`
3. `number = randval % maxval + 1;`
4. `// Using the standard C library`
5. `number = rand() % 100 + 1;`

C++:

1. `int number = rand() % 100+1;`

Rust:

1. `let random = rng.gen_range(1..101);`

Java:

1. `private static final int NUMBER = r.nextInt(100) + 1;`

Groovy:

1. `int randomNumber = (new Random()).nextInt(100) + 1`

JavaScript:

1. `const randomNumber = Math.floor(Math.random() * 100) + 1`

awk:

1. `randomNumber = int(rand() * 100) + 1`

Lua:

1. `number = math.random(1,100)`

loop until I guess that random number

Loops are usually implemented with control flow, such as `while` or `do-while` . The implementation in JavaScript does not use a loop, but updates the HTML page “in real time” until the user guesses the correct number. Although Awk supports loops, it makes no sense to read input information through loops, because Awk is based on data pipes, so it reads input information from a file instead of directly from the user.

C:

1. `do {`
2. `…`
3. `} while (guess != number);`

C++:

1. `do {`
2. `…`
3. `} while ( number != guess );`

Rust:

1. `for line in std::io::stdin().lock().lines() {`
2. `…`
3. `break;`
4. `}`

Java:

1. `while ( guess != NUMBER ) {`
2. `…`
3. `}`

Groovy:

Lua:

1. `while ( player.guess ~= number ) do`
2. `…`
3. `end`

Different programming languages ​​handle input differently. For example, JavaScript reads values ​​directly from HTML forms, while Awk reads data from data pipes.

C:

C++:

Rust:

1. `let parsed = line.ok().as_deref().map(str::parse::<i64>);`
2. `if let Some(Ok(guess)) = parsed {`
3. `…`
4. `}`

Java:

1. `guess = player.nextInt();`

Groovy:

1. `response = reader.readLine()`
2. `int guess = response as Integer`

JavaScript:

1. `let myGuess = guess.value`

Awk:

Lua:

1. `player.answer = io.read()`
2. `player.guess = tonumber(player.answer)`

Tell me to guess too high or too low

In these C-like languages, the comparison is usually done through an `if` statement. There are some variations in the way each programming language prints output, but the print statements are recognizable in each sample.

C:

1. `if (guess < number) {`
2. `puts("Too low");`
3. `}`
4. `else if (guess > number) {`
5. `puts("Too high");`
6. `}`
7. `…`
8. `puts("That's right!");`

C++:

1. `if ( guess > number) { cout << "Too high.\n" << endl; }`
2. `else if ( guess < number ) { cout << "Too low.\n" << endl; }`
3. `else {`
4. `cout << "That's right!\n" << endl;`
5. `exit(0);`
6. `}`

Rust:

1. `_ if guess < random => println!("Too low"),`
2. `_ if guess > random => println!("Too high"),`
3. `_ => {`
4. `println!("That's right");`
5. `break;`
6. `}`

Java:

1. `if ( guess > NUMBER ) {`
2. `System.out.println("Too high");`
3. `} else if ( guess < NUMBER ) {`
4. `System.out.println("Too low");`
5. `} else {`
6. `System.out.println("That's right!");`
7. `System.exit(0);`
8. `}`

Groovy:

1. `if (guess < randomNumber)`
2. `print 'too low, try again: '`
3. `else if (guess > randomNumber)`
4. `print 'too high, try again: '`
5. `else {`
6. `println "that's right"`
7. `break`
8. `}`

JavaScript:

1. `if (myGuess === randomNumber) {`
2. `feedback.textContent = "You got it right!"`
3. `} else if (myGuess > randomNumber) {`
4. `feedback.textContent = "Your guess was " + myGuess + ". That's too high. Try Again!"`
5. `} else if (myGuess < randomNumber) {`
6. `feedback.textContent = "Your guess was " + myGuess + ". That's too low. Try Again!"`
7. `}`

Awk:

1. `if (guess < randomNumber) {`
2. `printf "too low, try again:"`
3. `} else if (guess > randomNumber) {`
4. `printf "too high, try again:"`
5. `} else {`
6. `printf "that's right\n"`
7. `exit`
8. `}`

Lua:

1. `if ( player.guess > number ) then`
2. `print("Too high")`
3. `elseif ( player.guess < number) then`
4. `print("Too low")`
5. `else`
6. `print("That's right!")`
7. `os.exit()`
8. `end`

Non-C-like programming languages ​​will be very different and require learning specific syntax to complete each step. Racket comes from Lisp and Scheme, so it uses Lisp prefixes and lots of parentheses. Python uses spaces instead of parentheses to denote blocks like loops. Elixir is a functional programming language with its own syntax. Bash is based on the Bourne shell in Unix systems, which itself borrows from Algol68 and supports additional shorthand characters such as `&&` as a variant of `and` . Fortran was created at a time when punched cards were used to enter code, so it relied on an 80-column layout for some important columns.

I will illustrate the differences between these programming languages ​​by comparing `if` statements. `if` determines whether one value is less than or greater than another value and prints the appropriate information to the user.

Racket:

1. `(cond [(> number guess) (displayln "Too low") (inquire-user number)]`
2. `[(< number guess) (displayln "Too high") (inquire-user number)]`
3. `[else (displayln "Correct!")]))`

Python:

1. `if guess < random:`
2. `print("Too low")`
3. `elif guess > random:`
4. `print("Too high")`
5. `else:`
6. `print("That's right!")`

Elixir:

1. `cond do`
2. `guess < num ->`
3. `IO.puts "Too low!"`
4. `guess_loop(num)`
5. `guess > num ->`
6. `IO.puts "Too high!"`
7. `guess_loop(num)`
8. `true ->`
9. `IO.puts "That's right!"`
10. `end`

Bash:

1. `[ "0\$guess" -lt \$number ] && echo "Too low"`
2. `[ "0\$guess" -gt \$number ] && echo "Too high"`

Fortran:

1. `IF (GUESS.LT.NUMBER) THEN`
2. `PRINT *, 'TOO LOW'`
3. `ELSE IF (GUESS.GT.NUMBER) THEN`
4. `PRINT *, 'TOO HIGH'`
5. `ENDIF`

More

The “Guess the Number” game is a friendly introductory program when you’re learning a new programming language, practicing several common programming concepts in an easy way. Implementing this simple game in different programming languages ​​allows you to understand some of the core concepts and details of each language.

Learn how to write a “number guessing” game in C and C-like languages:

C

opensource.com

, Jim Hall

C++

opensource.com

, Seth Kenlon

Rust

opensource.com

Java

opensource.com

, Seth Kenlon

opensource.com

, Chris Hermansen

opensource.com

, Mandy Kendall

awk

opensource.com

, Chris Hermansen

Lua

opensource.com

, Seth Kenlon

other languages:

opensource.com

, Cristiano L. Fontana

opensource.com

opensource.com

Bash

opensource.com

, Jim Hall

opensource.com

, Jim Hall

Author: Jim Hall Topic: lujun9972 Translator: VeryZZJ Proofreading: wxy