Intro

This article will briefly review through some of the main features of programming that almost all programming languages require. These features make it possible for programs to implement their logic.

Data Types

In programming, there are 3 main data types that you will run into (there are a lot more depending on languages, but these should get the point across):

  • Integers: These are non-decimal numbers (decimals are called Floats). Below, we will define a valid integer in JavaScript. var x = 1;
  • Strings: These are essentially text. Example for declaring a valid string:
    var x = “This Needs 2 Be Surrounded By Quotes To Be A String!”;
  • Booleans: These are data types that represent true or false:
    var x = false;

It is important to know that in JavaScript, you can define almost any of these ‘primitive’ data types with the ‘var’ keyword. Different programming languages may require different keywords. For example, declaring an integer in Java would look like int x = 1;

If Statements

  • “If statements” help a program detect conditions and perform actions based on the conditions. For example, a self driving car may want the following logic: IF light is GREEN, press gas. If light is RED, stop.
  • In the following example, we make a program says “Go” if the variable x is Green and “Stop” if the variable is Red:
var x = "Green";
if (x == "Green")
{
console.log("Go");
}
else if (x == "Red")
{
console.log("Stop");
}

The double equals sign in the parenthesis just means equals, so if (x == “Green”) means “if the variable x equals the text of Green. “

!= would mean ‘does not equal’. These are called comparators and you can find a list of them here for more details:

Different languages may also have different ways of comparing. For example, in Java, the same statement above would be if (x.equals(“Green”)).

If you are using Google Chrome, you can try running this code in your browser by opening the browser’s developer tools and typing this into the console.

Developer tools can be access by pressing the … button at the top > More Tools > Developer Tools > Click on the tab that says Console.

You can change the x variable to “Red” and re-run the code to see what it outputs.

Note:

You may have noticed the squiggly brackets {}. These brackets are used to wrap the code that lets the program know what to do under that if condition. Some people may prefer this format where the brackets are on new lines, however, other people may prefer this format:

var x = "Green";
if (x == "Green") {
console.log("Go");
}
else if (x == "Red") {
console.log("Stop");
}

These are both valid syntax, and as long as the brackets are there to wrap up the code, the way it’s styled is just preference. If you choose a bracket/indenting style for a project, the important thing is to be consistent with it.

Also, be aware that some languages such as Python might take indentation into consideration for valid code, so if you are following other tutorials for different languages, it is recommended to follow their syntax for the project to avoid headaches.

Loops

There are two main types of loops, the For loop and the While loop.

For Loop

The For loop looks like this:

for (i=0; i < 3; i++) {
console.log("Walked staircase number " + i);
}
console.log("Successfully walked 3 levels. ");

The part that says “i=0; i < 3; i++” basically means:

  1. (i=0) Declare the variable “i” and that it has a value of 0.
  2. (i < 3) Continue doing the actions below as long as “i” is less than 3.
  3. (i++) After finishing the actions in the loop, add 1 to the variable “i”.

The console.log between the curly braces is the loop. In this case, the program will write “Walked staircase number i” 3 time before printing out “Successfully walked 3 levels. “

If you run the above code (you can run in it your browser console to see), then the output would be:

Walked staircase number 0
Walked staircase number 1
Walked staircase number 2
Walked staircase number 3
Successfully walked 3 levels.

This type of loop will usually be used if you know how many times you want to run a certain function(s) before moving on with the next steps.

While Loop

The While loop looks like this (don’t run this in your browser since it will run forever):

var button = "on";
while (button == "on") {
console.log("The button is on. ");
}

This is basically saying to print out “The button is on. ” while a variable called “button” has the value of “on”. The functions in here can run forever as long as the button variable does not change. in a real program, there would be a way for this variable to change. For example, there could be a function that sets the button’s value to be “off” when someone clicks on a part of the screen.

Navigation

The next article can be found here. Previous article is here.