Programming can feel overwhelming for first-timers. Looking at lines of code and not knowing what’s happening, or why, creates a busy experience. As a beginner, you can’t just look at code and figure it out without some context.

This guide exists to guide beginners through basic concepts. While many programming languages do different things with distinct code formats, they all follow a set of common principles. Learn and master these basics; you’ll be well on your way to intermediate and advanced programming topics.

Statements, Declarations, and Calls

Programming code is composed of statements. Some statements are a single line, others can be multi-line.

var single_line_code;

var multi_line_code = function() {
    line_1_statement();
    line_2_statement();
}

A statement of code does something. There are two actions that a statement does:

  • “Declare”: the statement is creating a variable, function, or object.
  • “Call”: the statement is “calling” a variable’s value, or a function that performs an action.
  • Both: a statement can perform a “declare” and “call.” You can “declare” a variable by “calling” a function. The second example above declares the variable multi_line_code by calling a function.

Variables

Variables are present in every programming language. They hold values that you can reuse throughout a code. The statements below are various examples of variables in different languages:

int y = 1;
var x = "Hello world";
$circle = "round";

Many different types of variables can exist, but they usually fall into any of these three categories: dynamic, static, and constant.

Dynamic variables can be of any value. The variables shown above are dynamic because they’re instantly created, can store any value, and you can change its value anytime in the code. Static variables are similar, but it holds the same value regardless of its position in the code plus it requires special programming methods to change its value. Constant variables hold the same value and cannot be changed anytime in the code. Most programming languages use the keywords “static” and “const” to indicate static and constant.

int y = 1;
static var x = "Hello world";
const $circle = "round";

Variables can also represent a specific “data type.” A “data type” is how you’d classify a variable. For example, a variable representing a decimal might be of the data type “decimal” or “float.” A word might be of the data type “string” or “character.” The most common data types are:

  • String
  • Integer
  • Float, Decimal
  • Boolean (true or false; 1 or 0)
  • Array

Functions

Functions are another element that’s prevalent in every programming languages. They represent a set of procedures. Every function has a name and optional parameters—together called “declarations.” Below are examples of function declarations in different languages:

function doNothing(){}
public function method_best(){}
int main(float Main){}
void main(){}

You “declare” a function in most languages by using parentheses, followed by curly brackets to indicate the beginning and end of a function. If you want a function to accept value inputs, you can insert parameter variables within the parentheses (see “int main” example above). Some functions only perform output, and some functions also perform calculations or store values. In that case, a function “returns” a value; the keyword return, followed by an end curly bracket, indicates the end of a function. In the example below, the function “add_two_numbers” accepts two variable parameters and then performs addition; the return is the result of the addition:

function add_two_numbers( $number1, $number2 ) {
   return $number1 + $number2;
}

Similar to variables, functions can also be classified under specific “data types” in some programming languages. For most web development programming languages, declaring a function using a data type is unnecessary and oftentimes impossible. In a programming language such as C#, you can declare a specific data type or use void if a function doesn’t fall under a data type:

void main(){}
string get_character_name(){}

The beauty of using functions is that you can reuse programming code without having to constantly re-type it. In the example below, we want to multiply 5 and 4 and then perform the addition of those results 5 times:

var x = (5 * 4) + (5 * 4) + (5 * 4) + (5 * 4) + (5 * 4);

Typing (5 * 4) again and again, and again, can make your hands feel exhausted plus it wastes time. Instead, let’s write a function that accepts two variable number parameters and multiplies the two numbers for us:

function multiply(var num1, var num2) {
   return num1 * num2;
}

Now we replace each appearance of (5 * 4) with the function we’ve just declared:

var x = multiply(5,4) + multiply(5,4) + multiply(5,4) + multiply(5,4) + multiply(5,4);

And we can further simplify the code, while saving time and hand energy, by using the following code to return the same results:

var x = multiply(5,4) * 5;

Scopes

Now that you understand variables and functions, you should know about the concept of scope. This refers to the placement of variables inside and outside functions. Variables created and used within a function are considered local because their values are only accessible within that function. Variables created outside a function are considered global and usually accessible in any function.

In some programming languages, a variable can have the same name but a different value because the global variation of the same name differs from the local variation of the same name. Consider the example statements below, which features global and local scopes of variables with the same name but different values:

int y = 1;

void something() {
   int y = 2;
   print y;
}

print y;

The above example declares the function something(). It will show the value of the variable “y”, which will be the number 2 because the scope of that variable is local (or “within” a function). The statement that appears after the function, “print y”, will show the number 1 because that variable “y” is global (or “outside” a function) and thus the statement falls under that global scope. Makes sense?

You can bring a global variable into a function. Some programming languages will allow you to declare and reference global variables. Take PHP as an example:

$y = 1;

function something() {
   global $y;
   echo $y;
}

The above example references the global variable $y by using the keyword “global.” The function will display the number 1 when it’s called.

If a programming language doesn’t allow you to reference global variables from within a function, then you will need to declare a function with a parameter that can accept a global variable:

$y = 1;

function calculate($input1) {
   return $input1;
}

calculate($y);

The above example will output the number 1, which equals the value of the global variable.

Conditionals

Conditionals refer to programming logic that executes under specified scenarios. In most programming languages, “if” statements are the most common conditionals. “If” statements check values using equal, greater than, and less than symbols (aka “operators”). The statements are formatted differently per programming language but the structure usually follows the patterns below:

if ($× == something)
   dosomething();
else if ($x == something else)
   dosomethingelse();
else if ($x != some other value)
   dosomecodestatement();
else if ($x > $w)
   dosomethingelsecode();
else if ( ($x < $y) && ($x > $z) )
   dosomethingelseheretoo();
else if ($x === something else)
   dosomethingelseheretoo();
else
   donothing();

There are several things to note about the above statement:

  • If…else if…else chain: this means that if scenario 1 is true, execute that code. If the alternate scenario 2 is true instead, then execute that code instead. If neither scenario is true, then do nothing or execute another set of code. This chain can extend as long as you want, but it’s best to keep it short. Of you were to insert an “if” statement within the chain, it creates a new chain and your code may behave differently. However, you can insert “if” statements and chains within another “if” statement to create nested statements.
  • Double equal sign: when using “if” statements to check exact values, you must use double equal signs when checking exact matching conditions. Whenever you don’t, you risk assigning a value to the variable instead of checking its value.
  • Triple equal sign: when using 3x equal signs, this not only checks the value but also the data type of a variable. It’s a mainstay in web development and useful for comparing boolean and numeric variables.
  • Operators: “if” statements depend on equal signs, greater than/less than signs, and exclamation/equal signs to check for particular conditions matching. The “==” sign checks for a match; “!=” checks for a non-match; “<” or “<=” checks for less than and less than or equal to; “>” or “>=” checks for greater than and greater than or equal to.
  • Grouped conditions: the 5th example above displays how you can group conditions and compare them. You can utilize AND (“&&”), which activates code when all the conditions match, or you can utilize OR (“||”) to activate code when at least one of the conditions match.

There’s another conditional you should know about: “switch” statements. They’re often used to activate programming code based on string and number variable values. “If” statements rely on operators, whereas “switch” statements check the value of a variable without using operators. Below shows the typical pattern of a “switch” statement:

$x = (5 + 4);

switch ($x) {
  case 9:
    echo $x;
    break;
  case 10:
    echo "Not correct";
    break;
  default:
    echo "Did you even add this?";
    break;
}

First, the variable $x equals the number 9 (when adding 5 + 4). The “switch” statement starts the conditional and we’re inserting the variable $x within parentheses. “Case” is the condition we’re testing—if $x equals 9, then it will output the number 9; if it equals 10, then the words “Not correct” will output; if $x doesn’t equal 9 or 10, then it will activate “default” condition and output “Did you even add this?”. Each condition starts with a colon and ends with “break;”.

The “switch” statement is used for testing specific values and not used for comparisons. In some situations, “switch” statements are easier to manage and makes your code simpler to read. It’s possible to immediately know the conditions being tested. Unlike “if” statements, “switch” statements aren’t ideal for nested conditionals.

Loops

Loops will execute program statements until a specific condition is met or a program ends.

For example: while you have money to spend, you spend it on items until your balance equals zero. Here’s an example of how that would look like as a programming code:

var money_balance = 2000;

while (money_balance != 0) {
   money_balance = money_balance - item_cost;
}

The above example showcases one of the simplest loops: a “while” loop. It executes a program code until a condition is met. To make a “while” loop work, there should be some sort of counter for comparison. In the code above, the counter is money balance—as you spend money on items, the cost of the item is subtracted from money balance. When money balance reaches zero, the loop stops. You can use any operator for a “while” loop (==, !=, >, <, >=, <=), and you can use conditional statements within a “while” loop as well.

Another type of loop that exists is the “for” loop. It’s useful for when you need to reference the counter variable value. The loop is hugely relevant for looping through and checking a list of variable values (via the “array” data type). Using the money balance example, here’s an example of how a “for” loop would be written:

for ($counter = 0; $money_balance > 0; $counter + 1) {
   $money_balance = $money_balance - $item_cost;
}

The parameters of a “for” loop can be confusing, but here’s what it all means below:

  1. The first parameter sets up the counter variable’s value.
  2. The second parameter defines the conditions for when the “for” loop ends.
  3. The third parameter defines what happens each time a “for” loop is executed, usually defined by the use of operators.

If a counter has a pre-defined quantity limit and you don’t need to reference a counter’s variable value, then you can use the variant of the “for” loop, which is called a “foreach” loop. It’s similar to a “while” loop but it doesn’t conduct any checks for comparisons—this makes it only useful to loop through a list of variable values and ends when it does (otherwise, it loops forever). Per each loop, it executes code. You can interrupt a “foreach” loop by using an “if” statement to activate “break”. The example below is a PHP example of how a “foreach” loop operates:

$x = array(1,2,3,4,5);

foreach ($x as $number) {
   echo $number;
}

The variable $x represents a list of values (1, 2, 3, 4, and 5). Each value will be transferred into the variable $number as the “foreach” loop traverses the list of $x values, and then $number will be shown each time until the “foreach” loop ends.

The “foreach” loop is available in most programming languages but not all. Web development languages support “foreach” loops, but their formats differ.

Array (aka “List”)

Arrays are variables that contain a “list of values.” Working with them requires mastery of basic programming concepts but it’s good to have some awareness of them. The concept of arrays works the same in every programming language. Once you master arrays in one programming language, working with them becomes easy in another language.

The position of an item in the array is called its “index.” By default, the index starts at position 0. If that’s confusing, then let’s observe the following array list below as an example:

array numbers = {1, 2, 3, 4, 5};

The array “numbers” is a list of the numeric values 1, 2, 3, 4, and 5. If you ask for the position of number 1, you’d say “the index of 1 is [position] 0.” If you ask for the position of number 5, you’d say “the index of 5 is [position] 4.” In this example, the length of an array equals 5, but when referencing the index position of values in an array it’s always whatever its manual position is minus 1 (remember, “index” starts at position 0).

There are two ways to access values in an array: bracket signs[] and loops. If you want to access the first position in the “numbers” array, you’d state “numbers[0]”. To access the last position in the “numbers” array, you’d state “numbers[4]”. The number within in between the brackets[] represents the index position that you’re calling. You can also use any of the aforementioned loops to access values in an array, using the counter’s variable value as a reference to an index position. The code examples below showcase how to create loops to access array values:

$numbers = array(1,2,3,4,5);

$counter = 0;
while ($counter <= 4) {
   echo $numbers[$counter];
}

for ($counter = 0; $counter <= 4; $counter + 1) {
   echo $number[$counter];
}

foreach($numbers as $counter) {
   echo $counter;
}

Other array concepts that you’ll want to learn about:

  • Associative arrays
  • Multidimensional arrays
  • Merging, splicing, and other array operations

Every programming language works with arrays and you’ll want to master their usage if you want to at least become an intermediate programmer. A full-page can be dedicated to array discussions, so we’ll stop here about arrays.

Classes and Objects

Classes and objects are associated with a programming concept called “object-oriented programming.” An easy way to conceptualize OOP is to think about video games. There are the protagonist character(s), level objects, and enemies. Each one has unique properties and methods of acting within the game. Protagonists might have fists and kicks as attack methods, while enemies have weapon and spell methods. Protagonists might have small heads, enemies might have big heads and humongous bodies. Some level objects interact with you, while other level objects sway back and forth in the background. Each of these represents three concepts associated with OOP: properties, methods, and object inheritance.

“Object-oriented programming” is an intermediate concept, but beginners should know about its existence because its used in nearly every programming language. Web-devs and software engineers can utilize OOP to make their code simple and operate more smoother.

Commenting

Commenting is a crucial practice in programming. This allows you to leave documentation notes about how certain sections of code operates. By leaving good notes, you can return to a piece of code later and instantly re-learn how a piece of code runs. Furthermore, other people can review your code and discover how it works—and offer their suggestions for improvement.

There are two main ways to leave comments in your code:

//This is used to leave a single line of comments.

/* This is useful for when
you want to leave multiple lines
of comments in your code.
*/

/******* And another way to comment on a single line ******/

There are many articles and textbook pages dedicated to best practices for leaving programming comments. WordPress, a popular blogging platform, has its own commenting best practices. Their extensive documentation standards are good to follow for any programming language or project.

Debugging

Debugging is a skill that takes time and practice to master. Discovering errors in your code, and troubleshooting them, can be difficult and increases as your programming project grows in size. If you’re lucky, the platform you’re using to develop code will tell you about programming errors. Otherwise, errors are discovered through running your code and testing various scenarios.

What is debugging? It’s the process of troubleshooting your code (though some would argue “debugging” and “troubleshooting” are different, this article keeps it plain and simple for you). The easiest way to debug is to output variable and function return values throughout certain parts of your code to determine whether a programming routine is working properly. In some platforms, you can monitor variable values while you’re testing your code. There’s a lot to say about debugging, and it’s different for each programming language.

  • C++/C# allows debugging through software development software.
  • JavaScript debugging is usually done by calling the “console.log()” function.
  • PHP debugging is usually done by using “echo” statements and calling the “var_dump()” function.