aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
aster.cloud aster.cloud
  • /
  • Platforms
    • Public Cloud
    • On-Premise
    • Hybrid Cloud
    • Data
  • Architecture
    • Design
    • Solutions
    • Enterprise
  • Engineering
    • Automation
    • Software Engineering
    • Project Management
    • DevOps
  • Programming
    • Learning
  • Tools
  • About
  • Practices
  • Programming

How Different Programming Languages Do The Same Thing

  • root
  • April 5, 2021
  • 5 minute read

Compare 13 different programming languages by writing a simple game.

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:


Partner with aster.cloud
for your next big idea.
Let us know here.



From our partners:

CITI.IO :: Business. Institutions. Society. Global Political Economy.
CYBERPOGO.COM :: For the Arts, Sciences, and Technology.
DADAHACKS.COM :: Parenting For The Rest Of Us.
ZEDISTA.COM :: Entertainment. Sports. Culture. Escape.
TAKUMAKU.COM :: For The Hearth And Home.
ASTER.CLOUD :: From The Cloud And Beyond.
LIWAIWAI.COM :: Intelligence, Inside and Outside.
GLOBALCLOUDPLATFORMS.COM :: For The World's Computing Needs.
FIREGULAMAN.COM :: For The Fire In The Belly Of The Coder.
ASTERCASTER.COM :: Supra Astra. Beyond The Stars.
BARTDAY.COM :: Prosperity For Everyone.

  1. The computer picks a random number between 1 and 100
  2. Loop until I guess the random number
    1. The computer reads my guess
    2. 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.

Read More  How Imposter Syndrome Affects Developers
C Using the Linux getrandom system call:
getrandom(&randval, sizeof(int), GRND_NONBLOCK);
number = randval % maxval + 1;
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 {
…
} while (guess != number); 
C++ do {
…
} while ( number != guess ); 
Rust for line in std::io::stdin().lock().lines() {
…
break;
} 
Java while ( guess != NUMBER ) {
…
} 
Groovy while ( … ) {
…
break;
} 
Lua  while ( player.guess ~= number ) do
…
end

 

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>);
if let Some(Ok(guess)) = parsed {
…
} 
Java guess = player.nextInt(); 
Groovy response = reader.readLine()
int guess = response as Integer 
JavaScript let myGuess = guess.value 
awk guess = int($0) 
Lua player.answer = io.read()
player.guess = tonumber(player.answer) 

 

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.

Read More  Measure And Improve Performance With Macrobenchmark
C     if (guess < number) {
puts("Too low");
}
else if (guess > number) {
puts("Too high");
}
…
puts("That's right!");
  
C++   if ( guess > number) { cout << "Too high.\n" << endl; }
else if ( guess < number ) { cout << "Too low.\n" << endl; }
else {
cout << "That's right!\n" << endl;
exit(0);
}
  
Rust                 _ if guess < random => println!("Too low"),
_ if guess > random => println!("Too high"),
_ => {
println!("That's right");
break;
} 
Java             if ( guess > NUMBER ) {
System.out.println("Too high");
} else if ( guess < NUMBER ) {
System.out.println("Too low");
} else {
System.out.println("That's right!");
System.exit(0);
} 
Groovy                   if (guess < randomNumber)
print 'too low, try again: '
else if (guess > randomNumber)
print 'too high, try again: '
else {
println "that's right"
break
} 
JavaScript       if (myGuess === randomNumber) {
feedback.textContent = "You got it right!"
} else if (myGuess > randomNumber) {
feedback.textContent = "Your guess was " + myGuess + ". That's too high. Try Again!"
} else if (myGuess < randomNumber) {
feedback.textContent = "Your guess was " + myGuess + ". That's too low. Try Again!"
} 
awk             if (guess < randomNumber) {
printf "too low, try again:"
} else if (guess > randomNumber) {
printf "too high, try again:"
} else {
printf "that's right\n"
exit
} 
Lua   if ( player.guess > number ) then
print("Too high")
elseif ( player.guess < number) then
print("Too low")
else
print("That's right!")
os.exit()
end 

 

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.

Read More  Python 3.12.0 Alpha 6 Released

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)] [(< number guess) (displayln "Too high") (inquire-user number)] [else (displayln "Correct!")])) 
Python     if guess < random:
print("Too low")
elif guess > random:
print("Too high")
else:
print("That's right!") 
Elixir     cond do
guess < num ->
IO.puts "Too low!"
guess_loop(num)
guess > num ->
IO.puts "Too high!"
guess_loop(num)
true ->
IO.puts "That's right!"
end 
Bash         [ "0$guess" -lt $number ] && echo "Too low"
[ "0$guess" -gt $number ] && echo "Too high" 
Fortran       IF (GUESS.LT.NUMBER) THEN
PRINT *, 'TOO LOW'
ELSE IF (GUESS.GT.NUMBER) THEN
PRINT *, 'TOO HIGH'
ENDIF 

 

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:

  • Racket, by Cristiano L. Fontana
  • Python, by Moshe Zadka
  • Elixir, by Moshe Zadka
  • Bash, by Jim Hall
  • Fortran, by Jim Hall

This article is republished from opensource.com


For enquiries, product placements, sponsorships, and collaborations, connect with us at [email protected]. We'd love to hear from you!

Our humans need coffee too! Your support is highly appreciated, thank you!

root

Related Topics
  • Bash
  • C++
  • Code
  • Fortran
  • Groovy
  • Java
  • javascript
  • Programming
  • programming languages
  • Python
  • Rust
  • Variables
You May Also Like
Laptop | Tools
View Post
  • Computing
  • Engineering
  • Practices

Best Practices For Securing Kubernetes Deployments

  • December 22, 2023
Frontier Model Security
View Post
  • Platforms
  • Practices
  • Technology

Frontier Model Security

  • August 12, 2023
working-business-women-cowomen-hz-6prUpVss-unsplash
View Post
  • Data
  • Enterprise
  • Practices
  • Technology

5 Effective Tactics For Ecommerce Link Building

  • June 9, 2023
View Post
  • Architecture
  • Data
  • Engineering
  • People
  • Programming
  • Software Engineering
  • Technology
  • Work & Jobs

Predictions: Top 25 Careers Likely In High Demand In The Future

  • June 6, 2023
View Post
  • Architecture
  • Insights
  • Practices

The Agile Mindset: A Path to Personal Fulfillment and Growth

  • May 30, 2023
View Post
  • Engineering
  • Practices
  • Tools

Tricentis Launches Quality Engineering Community ShiftSync

  • May 23, 2023
View Post
  • Programming
  • Software Engineering
  • Technology

Build a Python App to Alert You When Asteroids Are Close to Earth

  • May 22, 2023
View Post
  • Programming

Illuminating Interactions: Visual State In Jetpack Compose

  • May 20, 2023

Stay Connected!
LATEST
  • college-of-cardinals-2025 1
    The Definitive Who’s Who of the 2025 Papal Conclave
    • May 7, 2025
  • conclave-poster-black-smoke 2
    The World Is Revalidating Itself
    • May 6, 2025
  • 3
    Conclave: How A New Pope Is Chosen
    • April 25, 2025
  • Getting things done makes her feel amazing 4
    Nurturing Minds in the Digital Revolution
    • April 25, 2025
  • 5
    AI is automating our jobs – but values need to change if we are to be liberated by it
    • April 17, 2025
  • 6
    Canonical Releases Ubuntu 25.04 Plucky Puffin
    • April 17, 2025
  • 7
    United States Army Enterprise Cloud Management Agency Expands its Oracle Defense Cloud Services
    • April 15, 2025
  • 8
    Tokyo Electron and IBM Renew Collaboration for Advanced Semiconductor Technology
    • April 2, 2025
  • 9
    IBM Accelerates Momentum in the as a Service Space with Growing Portfolio of Tools Simplifying Infrastructure Management
    • March 27, 2025
  • 10
    Tariffs, Trump, and Other Things That Start With T – They’re Not The Problem, It’s How We Use Them
    • March 25, 2025
about
Hello World!

We are aster.cloud. We’re created by programmers for programmers.

Our site aims to provide guides, programming tips, reviews, and interesting materials for tech people and those who want to learn in general.

We would like to hear from you.

If you have any feedback, enquiries, or sponsorship request, kindly reach out to us at:

[email protected]
Most Popular
  • 1
    IBM contributes key open-source projects to Linux Foundation to advance AI community participation
    • March 22, 2025
  • 2
    Co-op mode: New partners driving the future of gaming with AI
    • March 22, 2025
  • 3
    Mitsubishi Motors Canada Launches AI-Powered “Intelligent Companion” to Transform the 2025 Outlander Buying Experience
    • March 10, 2025
  • PiPiPi 4
    The Unexpected Pi-Fect Deals This March 14
    • March 13, 2025
  • Nintendo Switch Deals on Amazon 5
    10 Physical Nintendo Switch Game Deals on MAR10 Day!
    • March 9, 2025
  • /
  • Technology
  • Tools
  • About
  • Contact Us

Input your search keywords and press Enter.