We’ve talked a bit about what coding is. Now let’s get a better understanding of some fundamental concepts you will tend to utilize regardless of the coding language you choose to work with.
Coding is generally about manipulating some sort of information (data) to accomplish some task. There are many different types of data we might work with and manipulate through coding. Let’s take a look a a few of these –
Numerical data – This one is pretty straightforward. These are numbers in any form. Numerical data could be as simple as integers like 24 or -16, or they could be more complex number representations such as 0.1642 or 12e-10.
Strings – A string is any sequence of text. For example, “This is a string” and “thisisastring” and “…@&…” and “123456789” are all strings. Take note of a couple of things in our examples –
1. Strings can have spaces in them.
2. Numbers can also be represented as strings.
3. Various symbols can be part of a string.
4. In most coding languages, strings are inside single or double-quotes
Basically, any characters that computer systems know about may be part of a string.
Arrays – An array is a collection of data. It is usually a collection of data that has some commonality. For example … the heights and weights of a group of people could be represented as an array of data.
How that we know of some data types, how do we work with data?
We usually work with data by storing data inside something called a variable. Usually you choose a name for a variable and then assign data to it.
Very generically –
number = 3
$string = “hello, this is a string of characters”
In the first example, we are just storing the number 3 in a variable named “number.” In the second example, we are storing a collection of words spaces and and a comma inside a variable called “$string.” Do you notice something interesting about the second example? It has a dollar sign in front of it. No, it doesn’t mean that we are doing something with money. In many coding languages, a dollar sign at the beginning of a variable name tells the coding language that this variable is working with a string. This is not always true though. In php, for example, all variables are represented with a dollar sign. So, even if you’re working with numbers, you will always be using a dollar sign at the start of your variable names in php. In effect, php doesn’t really distinguish between numbers and strings.
More about that later. Let’s get back to some basics of working with variables.
We also had mentioned arrays. Let’s say we were working with code that stored the heights and weights of ten different people. We could use a different variable name for each, but that would create a whole lot of redundant code. So that’s where arrays might come in. Here, we could set some variable name indexed by a number, to specify which person we are talking about. So, height[n] could represent the height of person number n, and weight[n] could represent the weight of person number n.
Storing data in a variable is not all that exciting, but we can then manipulate the data to start to do exciting things.
For example, we can add numbers and assign the result to a new variable –
sum = number1 + number2
This example adds the contents of the variable “number1” with the contents of the variable “number2” and stores the sum in the variable named “sum”
Let’s get more specific –
number1 = 5
number2 = 10
sum = number1 + number2
That’s right – the above example would display “15” See that? Coding is not so hard after all. It’s just made up of very simple logical steps to arrive at a result. Even if we have complex tasks, we are always breaking them down into very simple steps.
Let’s look at an example with string manipulation. Let’s assume we had a bit of code that allowed a user to enter some data and it was stored in a variable called “$userinput”
The user typed in “Hello, my name is Michael”
Now we want to extract their name from this string.
If we knew that there name is the last seven characters of the string, we could do this as follows –
$name = right($userinput, 7)
Where does data come from?
Okay, code generally manipulates data – so, where do we get this data from?
Well, sometimes we get it from a user inputting data. For example, perhaps the user fills out a form on a web page and clicks a “submit” button.
Or, maybe we get this data from a file.
Or, often we get this data from a database with data stored in it.
Conditional statements tell the code to execute a set of instructions when a particular condition is met. Let’s look at this in its most common form –
If Then Else
The most basic conditional statement in most coding language is an “If” statement of some sort. Usually it is in this general format –
If (some condition is met)
Then (do something)
Else (do something else)
Let’s look at a really simple example.
If number > 10
Then echo “Your number is greater than 10”;
Else echo “Your number is not greater than 10”;
We haven’t talked about the “echo” command yet but it’s a simple one. It just displays text on your screen.
Let’s look at another example –
If first > second
Then echo “The first number is greater than the second number”;
Else echo “The second number is greater than the first number”;
Loops allow you to do many iterations of a set of instructions while or until some condition is met. This is useful for many purposes – often for running through a set of data, or sometimes just for doing something over and over a certain number of times (a fixed number of time or a variable number of times.)
Some common types of loops are –
For … Next
For loops are usually for doing something a predetermined number of times –
For X = 1 to 20
This is a simple loop that will print the numbers 1 to 20 on the screen.
Do … While
While … Until
Output data to screen
Output data to file
Get user input
Get input from file
Get data from database
Store data in database