Tabular Comparison Examples
HelloWorld
Ruby
puts 'Hello, World!'
C
#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
Java
package Comparisons;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Python
print('Hello, World!')
Variables
Ruby
_var1 = "Ben" #Local Variables
@var2 = "Jake" #Instance Variables
@@var3 = "Josh" #Class Variables
$var4 = "Braeden" #Global Variables
C
#include <stdio.h>
int main() {
int num = 15;
float floatNum = 5.99;
char letter = 'J';
char word[] = "word";
return 0;
}
Java
package Comparisons;
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Python
x = 5 #Integer
y = "Ben" #String
z = 3.0 #Float
Methods
Ruby
def example(j)
j = 200
end
example(i)
C
#include <stdio.h>
int main() {
int foo = 3;
int bar = 5;
int sum;
sum = addNums(foo, bar);
return 0;
}
int addNums(int a, int b) {
int result;
result = a+b;
return result;
}
Java
package Comparisons;
public class methods {
static void method() {
System.out.println("This is an example!");
}
public static void main(String[] args) {
method();
}
}
Python
def example_function
print("This is an example")
example_function()
Loops
Ruby
#while loop
x = gets.chomp.to_i
while x >= 0
puts x
x = x -1
end
#until loop
x = gets.chomp.to_i
until x < 0
puts x
x -= 1
end
#for loop
x = gets.chomp.to_i
for i in 1..x do
puts x - i
end
C
#include <stdio.h>
int main() {
int i;
for (i = 1; i < 11; ++i) //for loop
{
printf("%d ", i);
}
int j = 5; //while loop
while(j >= 0) {
printf("%d ", j);
j--;
}
int num=1;
do { //do-while loop
printf("%d\n",2*num);
num++;
}
while(num<=10);
return 0;
}
Java
package Comparisons;
public class loops {
public static void main(String[] args) {
int n = 5;
for (int i = 0; i <= n; i++) { //for loop
System.out.println(i);
}
int[] numbers = {3,4,5,6}; //for each loop
for(int number: numbers) {
System.out.println(number);
}
int j = 5; //while loop
while(j >= 0) {
System.out.println(j);
j--;
}
}
}
Python
#for loop
fruits = ["apple", "banana", "cherry"]
for x in fruits:
print(x)
#else in for loop
for x in range(6):
print(x)
else:
print("Finally finished!")
#range loop function
for x in range(6):
print(x)
Operators
Ruby
x = 1
y = 2
x, y = 1, 2
x, y = y, x
x + y
x - y
x * y
x / y
x < y
x > y
x <= y
x >= y
x == y
C
int main() {
int x = 1;
int y = 2;
int z;
z = x + y;
z = x - y;
z = x * y;
z = x / y;
if(x < y)
return;
if(x > y)
return;
if(x <= y)
return;
if(x >= y)
return;
if(x == y)
return;
}
Java
package Comparisons;
public class Operators {
//Have to create a variable to hold total without modifying x and y
int x = 1;
int y = 2;
int z;
z = x + y;
z = x - y;
z = x * y;
z = x / y;
if (x < y)
break;
if (x > y)
break;
if (x <= y)
break;
if (x >= y)
break;
if (x == y)
break;
}
Python
x = 1
y = 2
x, y = 1, 2
x, y = y, x
x + y
x - y
x * y
x / y
x < y
x > y
x <= y
x >= y
x == y