Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
85 views

Programming Syntax Cheat Sheet

The document provides a programming syntax cheat sheet covering common syntax structures across several languages including C++, Java, Python, PHP, and JavaScript. It includes syntax for starting code, declaring variables, functions/methods, if statements, for loops, taking user input, write operations/output, and arrays in each language.

Uploaded by

Fabio Gemesio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views

Programming Syntax Cheat Sheet

The document provides a programming syntax cheat sheet covering common syntax structures across several languages including C++, Java, Python, PHP, and JavaScript. It includes syntax for starting code, declaring variables, functions/methods, if statements, for loops, taking user input, write operations/output, and arrays in each language.

Uploaded by

Fabio Gemesio
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Programming Syntax Cheat Sheet

Version 1.0

STEP NAME

Start of the Code

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

Python
PHP
Javascript

Declaring a Variable

No specific start required


<?php
?>
<script>
</script>

C++

int var_1;
char var_2;

Java

public int var_1;


public char var_1;

Python
PHP
Javascript

Function/Method Declaration

SYNTAX

C++

var_1=2
var_2= "This is a string"
$var_1 = 2 ;
$var_2="This is a string" ;
var var_1=2;
var var_2="This is a string";
int function func_01 {
int var_1;
char var_2;
}

Java

Python

PHP

Javascript

public static func_01 {


public int var_1;
public char var_1;
}
def func_01 :
var_1=2
var_2= "This is a string"
function func_01 {
var_1=2 ;
var_2= "This is a string";
}
function func_01 {
var var_1=2;
var 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");
}
}

If Statement
Python

PHP

Javascript

a=1
if a > 5:
print "This shouldn't happen."
else:
print "This should happen."
if ($t < "20") {
echo "Have a good day!";
}
if (hour < 18) {
greeting = "Good day";
} else {
greeting = "Good evening";
}

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(",");
}

For Loop

Python

PHP

Javascript

Taking Input from User

for ($x = 0; $x <= 10; $x++) {


echo "The number is: $x <br>";
}
for (i = 0; i < cars.length; i++) {
text += cars[i] + "<br>";
}

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( );

Python

person = input('Enter your name: ')


print('Hello', person)

PHP

//In PHP taking input from user will need a PHP form
<form action="welcome.php" method="post">
Name: <input type="text" name="name">
E-mail: <input type="text" name="email">
<input type="submit">
</form>

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 );

Python

Write Operation/ Output

for item in items:


print(number, item)
number = number + 1

PHP

person = input('Enter your name: ')


print('Hello', person)
echo "Hello world!";
echo "I'm about to learn PHP!";
<h1>My First Web Page</h1>
<p>My first paragraph.</p>

Javascript

Arrays

<script>
document.write(5 + 6);
</script>

C++

int foo [5] = { 16, 2, 77, 40, 12071 };

Java

dataType[] arrayRefVar = new dataType[arraySize];

Python

PHP
Javascript

['red', 'green', 'blue']


[1, 3, 5, 7, 9, 11]
['silly', 57, 'mixed', -23, 'example']
//These are Python lists
$cars = array("Volvo", "BMW", "Toyota");
var cars = ["Saab", "Volvo", "BMW"];

You might also like