If you've read our blogs, especially the ones written by yours truly, you know there's an insane amount of different programming languages. To the every day non-developer, they probably all look the same, even those that are fundamentally different like, say PHP and the original "C", will look equally analogous to gibberish. But once you get started learning, you realize how much they all tend to differ from one another. Or do they?
The main difference is, sorry to disappoint, something called Syntax. And that's really it. Languages like PHP or JavaScript will use the semicolon and the end of a statement, where something like Python or Ruby doesn't. Object-oriented or procedural, or even functional style, how things are handled, things will be different from one language to another. But programming languages are not entirely unlike spoken languages. Sure, English to Spanish is very different. But if you speak to someone who has at least studied multiple Latin-based languages, they'll tell you that Spanish, Italian, and even French are all very similar. Some may even argue that someone can speak Spanish to an Italian, and the Italian can understand at least the basics of what is being said.
Fortunately, the same can also be said for programming. Everything, or at least damn-near everything, is based off the grandfather of all programming languages, "C". And because of this, nearly all programming languages share the same basic fundamentals. So let's take a look at some of these.
So you have two main ways to do this, but it basically comes down to really just one. The "variable" is what we call it, and it's exactly what it sounds like. It can hold a 'variable' that can be anything you want it to. Let's say you want to have a prompt load on a web page asking for someone's first name, so you can put that first name throughout the page to make it feel more personal. Well, you can just take whatever they enter and save it as a variable called "name". So then, any time you use the variable of 'name', it will display whatever they put in the box.
Some variables can be set to arrays (or objects) that can be thought of as a sort of "super" variable. Though that's not what we call it, we just go with variable. But you can use something called an "associative" array that can store multiple things. Let's say you want to store someone's first name, last name, and Date of Birth in one variable, you can do that. Then you can call it by typing "person.firstname" (syntax may vary) that will print out the first name of the person variable. Pretty cool, yeah?
So you want a program to do something, but you only want it to do something sometimes. Let's say you want your website to display content based on the user's age. Well, use a prompt to have them enter their age, then you can have the program check to see if it's over a certain age. it would look something like this.
if (age >= 18) {
alert("You can legally vote!");
} else {
alert("Sorry, you can't vote yet...");
}
This little snippet here will cause a different alert window to pop up depending on the age of the visitor. If their age is greater than ( > ) or equal to ( = ) 18, then we tell them that they're old enough to vote. "Else", basically meaning the default behavior, is to says that they're not old enough yet. It can get more complex than this for sure, but as for the fundamentals, it really is this simple. Also, notice the semicolons at the end of the two alert functions. That's letting the computer/browser know that we're done with that statement and it's time to move on to the next statement. It's like the period, but for programming sentences.
So above, I mentioned associative arrays and how they are basically a variable with multiple values saved to it. Well, you can also have an "indexed" array that is just a numbered list of values. Oddly enough, it starts at zero rather than one, but other than that, you can loop through and just display data. Let's say you have a list of grocery items. It'll look something like this.
groceries = ['milk', 'eggs', 'bread'];
Now, let's say you wanted to loop through all of them. You can build a loop that goes through each item and displays them on the screen as an individual item, and have it look something like this.
I'm not going to bother showing a loop code block in action, because they can be a little more intense (depending on the language) as they can have a few too many moving parts. This is especially true someone who is just getting started and hasn't been through the true fundamentals of programming.
So there you have it. You know the basics of how programs store data, what a loop will do for you, and even how to limit your program to more condition-specific situations. Stay tuned for a follow up where we discuss a few more aspects of programming, and possibly even show you some more detailed examples, including how to put all of the concepts together to make a solid (albeit simple) application!