Programming Syntax Cheat Sheet
Version 2.2
STEP NAME
LANGUAGE
C++
#include <iostream>
using namespace std;
int Main() {
}
Java
public static void main(String args[])
//All the code should be enclosed inside a class as well
Javascript
Start of the Code
SYNTAX
Python
PHP
C#
Ruby
<script>
</script>
No specific start required
<?php
?>
using System;
namespace *Project_Name*
{
class *Class_Name*
//Just like Java, in C# also all code is enclosed within a class
static void Main() {
{
No specific start required
C++
int var_1;
char var_2;
float var_3 = 1.0;
Java
public int var_1;
public char var_1;
public float var_3 = 1.0;
Javascript
Declaring a
Variable
Python
var_1=2
var_2= "This is a string"
PHP
$var_1 = 2;
$var_2="This is a string";
C#
int myInt = 1;
float myFloat = 1f;
bool myBoolean = true;
bool anotherBoolean;
string myName = "John";
char myChar = 'a';
Ruby
$var_1=23 //Ruby Global Variable
@var_2=23 //Ruby Instance Variable
@@var_3=23 //Ruby Class Variable
_var_4=23
//Ruby local variable
var_5 = 23
//Ruby local variable
C++
int function func_01 {
int var_1;
char var_2;
}
Java
public static func_01 {
public int var_1;
public char var_2;
}
Javascript
Function/Method
Declaration
var var_1=2;
var var_2="This is a string";
function func_01 {
var var_1=2;
var var_2="This is a string";
}
Python
def func_01:
var_1=2
var_2= "This is a string"
PHP
function func_01 {
var_1=2;
var_2= "This is a string";
}
C#
Ruby
public int func_01 {
int var_1;
char var_2;
}
def func_01
_var_1=2
_var_2= "This is a string
C++
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
Java
if( x < 20 ) {
System.out.print("This is if statement");
} else {
System.out.print("This is else statement");
}
Javascript
If Statement
Python
PHP
C#
Ruby
if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}
a=1
if a > 5:
print "This shouldn't happen."
else:
print "This should happen."
if ($t < "20") {
echo "Have a good day!";
}
a=1
if (a > 5)
Console.WriteLine("Hey! I am Trapped");
else
Console.WriteLine("Now I am free");
x=1
if x > 2
puts "x is greater than 2"
else
puts "I can't guess the number"
end
C++
for (int n=10; n>0; n--) {
cout << n << ", ";
}
Java
System.out.print("\n");
String [] names = {"James", "Larry", "Tom", "Lacy"};
for( String name : names ) {
System.out.print( name );
System.out.print(",");
}
Javascript
For Loop
Python
PHP
C#
Ruby
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}
for item in items:
print(number, item)
number = number + 1
print( loop ended, iteration count = , number)
for ($x = 0; $x <= 10; $x++) {
echo "The number is: $x <br>";
}
for (n=10; n>0; n--) {
Console.WriteLine("Hey! I am Trapped");
}
for i in 0..5
puts "Value of local variable is #{i}"
end
C++
cin >> x;
//Takes the value from user and stores in x
Java
Scanner user_input = new Scanner( System.in );
String first_name;
System.out.print("Enter your first name: ");
first_name = user_input.next( );
Javascript
Python
Taking Input from
User
PHP
C#
Ruby
var customerName = prompt("Please enter your name", "<name goes here>");
person = input('Enter your name: ')
print('Hello', person)
//In PHP taking input from user will need a PHP form
# This is also a one line comment
/* PHP also supports C style comments
*/
<form action="welcome.php" method="post">
Name: <input type="text" name="name">
E-mail: <input type="text" name="email">
<input type="submit">
</form>
String var_1= Console.ReadLine();
$var1 = gets //Takes user input passed as argument
$var2=gets.chomp //Same as above with no line breaks
$var3= STDIN.gets //Reads from console even if ARGV is empty
C++
cout << "Output sentence"; // prints Output sentence on screen
cout << 120;
// prints number 120 on screen
cout << x;
// prints the value of x on screen
Java
System.out.print( name );
<h1>My First Web Page</h1>
<p>My first paragraph.</p>
Javascript
Write Operation/
Output
Python
PHP
C#
Ruby
C++
Java
Javascript
Python
Arrays
PHP
C#
Ruby
<script>
document.write(5 + 6);
</script>
person = input('Enter your name: ')
print('Hello', person)
echo "Hello world!";
echo "I'm about to learn PHP!";
Console.WriteLine("Hey! I am Trapped");
puts "This is awesome
int marks [] = {3,4,3,2,1};
dataType[] arrayRefVar = new dataType[arraySize];
var cars = ["Saab", "Volvo", "BMW"];
['red', 'green', 'blue']
[1, 3, 5, 7, 9, 11]
['silly', 57, 'mixed', -23, 'example']
//These are Python lists
$cars = array("Volvo", "BMW", "Toyota");
int [] marks = new int[] { 99, 98, 92, 97, 95};
nums = Array[1, 2, 3, 4,5]