Joseph Haugh
University of New Mexico
Python
# This is a comment
x = 5 # This is an inline comment
Java
// This is a comment
int x = 5; // This is an inline comment
Python
x = 5 # x is 5
y = 6 + 3 # y is 9
z = y * 2 # z is 18
Java
int x = 5; // x is 5
int y = 6 + 3; // y is 9
int z = y * 2; // z is 18
Python
x = 5
if x > 5:
# If x is greater than 5
# Then do these statements
print("Greater than 5")
elif x == 5:
# Else if x is equal to 5
# Then do these statements
print("Equal to 5")
else:
# Else do these statements
print("Less than 5")
Java
int x = 5;
if (x > 5) {
// If x is greater than 5
// Then do these statements
IO.println("Greater than 5");
} else if (x == 5) {
// Else if x is equal to 5
// Then do these statements
IO.println("Equal to 5");
} else {
// Else do these statements
IO.println("Less than 5");
}
Python
x = 5
if x > 5:
# If x is greater than 5
# Then do these statements
print("Greater than 5")
elif x == 5:
# Else if x is equal to 5
# Then do these statements
print("Equal to 5")
else:
# Else do these statements
print("Less than 5")
Java
int x = 5;
if (x > 5) {
// If x is greater than 5
// Then do these statements
IO.println("Greater than 5");
} else if (x == 5) {
// Else if x is equal to 5
// Then do these statements
IO.println("Equal to 5");
} else {
// Else do these statements
IO.println("Less than 5");
}
Python
i = 0
while i < 5:
# Loop until i is NOT less than 5
# While the condition holds do
# these statements
print(i)
i = i + 1
# Then check the condition again
# and repeat if necessary
Java
int i = 0;
while (i < 5) {
// Loop until i is NOT less than 5
// While the condition holds do
// these statements
IO.println(i);
i = i + 1;
// Then check the condition again
// and repeat if necessary
}
Python
x = 5
y = 6
if x < 5:
x = 7 # Inside
y = 8 # Outside
print(x) # Prints 5
print(y) # Prints 8
Java
int x = 5;
int y = 6;
if (x < 5) {
x = 7; // Inside
}
y = 8; // Outside
IO.println(x); // Prints 5
IO.println(y); // Prints 8
Python
x = 0
y = 0
while x < 10:
x += 1 # Inside
print(x) # Inside
print(y) # Outside
Java
int x = 0;
int y = 0;
while (x < 10) {
x += 1; // Inside
IO.println(x); // Inside
}
IO.println(y); // Outside
Python
# Read an int from user input
x = int(input())
# Now you can use the number
if x >= 0:
print("Positive")
else:
print("Negative")
Java
// Read an int from user input
int x = Integer.parseInt(IO.readln());
// Now you can use the number
if (x >= 0) {
IO.println("Positive");
}
else {
IO.println("Negative");
}
Comments
Python
Java