Categories
General

If Else Statement in 10 Different Programming Languages

I have written code in many programming languages. Some languages have strange syntax while others invite you to write more code. In this post, I want to write if else statements in 10 different languages so you can share what you like or dislike about each language.

Python

amount = 0
if amount < 0:
print('Negative amount')
elif amount > 0:
print('Positive amount')
else:
print('Zero')

Java

int amount = 0;
if (amount < 0) {
  System.out.println("Negative amount");
} else if (amount > 0) {
  System.out.println("Positive amount");
} else {
  System.out.println("Zero");
}

Ruby


amount = 0
if amount < 0
puts "Negative amount"
elsif amount > 0
puts "Positive amount"
else
puts "Zero"

C#

int amount = 0;
if (amount < 0) {
  Console.WriteLine("Negative amount");
} else if (amount > 0) {
  Console.WriteLine("Positive amount");
} else {
  Console.WriteLine("Zero");
}

Swift

let amount = 0
if amount < 0 {
print("Negative amount")
}
else if amount > 0 {
print("Positive amount")
}
else {
print("Zero")
}

Objective-C

int amount = 0;
if (amount < 0) {
  NSLog("Negative amount");
} else if (amount > 0) {
  NSLog("Positive amount");
} else {
  NSLog("Zero");
}

Go


amount := 0
if amount < 0 {
fmt.Println("Negative amount")
}
else if amount > 0 {
fmt.Println("Positive amount")
}
else {
fmt.Println("Zero")
}

Javascript

const amount = 0;
if (amount < 0) {
Console.Log("Negative amount");
}
else if (amount > 0) {
Console.Log("Positive amount");
}
else {
Console.Log("Zero");
}

Rust

let amount = 0;
if amount < 0 {
print!("Negative amount");
}
else if amount > 0 {
print!("Positive amount");
}
else {
print!("Zero");
}

C++

int amount = 0; 
if (amount < 0) {
cout << "Negative amount";
}
else if (amount > 0) {
cout << "Positive amount";
}
else {
cout << "Zero";
}

There you have it. If else statement in 10 different programming languages. Leave me a comment with your favorite language.