I’ll be forthcoming on this; programming draws a lot from mathematics. And while that may sound scary for some people, the truth is that most of it is no more complicated than the simple math that makes up daily life. For the average program, the math gets to nothing more than Algebra. If you were always one to struggle with math, don’t panic, and don’t let yourself get turned off by the idea of having math being a part of this process. I struggled at a lot of points in dealing with math education, and I can vouch for the fact that most applications won’t get that complicated on you.
At worst, if you have to deal with advanced math concepts, it’s only in developing something like a game. And even there, the computer is set up to help you a lot. You aren’t doing math so much as learning to plug in an equation. The computer will actually solve the problem, you just tell it which problem you want solved.
At any rate, programming comes down to some logic, and solving simple equations. Before we get to doing that, though, we need to understand some core concepts about programming languages.
This article will contain a list of common terminology.
Most software is fairly complex when you look at it as a whole. But the simple fact is that programming is essentially a forest; and that means that a program consists of individual trees. Yes, trees have some complexity to them, but individually, they’re not so hard to understand. That vast majority of challenge comes from learning how these trees combine to become that forest.
There are a number of concepts that are common to nearly all programming languages. I say nearly all, because there are a few languages that may not have these as readily identifiable concepts. But the truth is, if you’re discussing a language that doesn’t include these concepts, that means you’re dealing with a highly unusual or very specialized language, and not one you’re likely to encounter outside of highly theoretical or advanced development work. I work as a software engineer, and I have for 20 years. I have never used those unusual languages in a professional context. However, what I am covering here is stuff I use on a daily basis.
- A variable is essentially a placeholder for some piece of data. As an example, the name of a person can be a variable. So could a value such as X in a simple math equation such as 4 + X. If X is 1, the equation would be equal to 5; if we change X to a value of 3, the result is now 7.
- A function is a series of instructions that are used regularly for calculation or action; rather than having to write individual instructions repeatedly, we can build a function that can perform the process we need over and over again.
For example: if we needed a way to calculate your age based on your birthday and the current day, we could do the math for it in a single block of code. But what if we wanted to do that for multiple people with different birthdays, the amount of code we have to write would scale up very quickly, and make our program inefficient, bloated and messy.
A function could be put to take your birthday in as a variable, and return the resulting age.
- Objects are part of a concept called object-oriented programming. Building on the idea of reusable code, an object is a sort of structure that can be used to define complex re-usable idea. For example, you can create a person object, which would have space for a first name, last name, date of birth, address and phone number. Then, for each person, you’d create a new copy of this object, and populate it with its own data. The proper name for these copies is instances.
- Variables that are a part of an object are generally referred to as properties. They essentially are the same as a variable, but because they form a specific part of a type of object, we call them by this name to indicate that they are “owned by” that object.
- If a property is a variable in an object, a method would be a function in one. A great example would be to build that age method into the person object. Then when you needed a particular person’s age, you don’t need to get the date from the object and send it into a separate function to get back the age. You can call the method on the person and get their date back directly. In other cases, instructions for an object that is very specific to a person could be a part of the object. For example, you can have a stand-up function. When it’s called on a particular person, that one stands up.
- Conditions are logical rules to help control the flow of information. Programs that can’t respond based on conditions are really not useful, because they would run in a linear way and not allow for different operations based on what a person can do.
For example, if a person’s age is under 5, a person goes to daycare. if they are over 5 and under 18, they go to school. Over 18, and they could go to college, trade school, get a job, and more.
If statements (sometimes referred to as if-else or if-else if-else) are a common control approach to provide choice control and reaction.
- Loops are a control structure that allows for repetition of a task based on a set of conditions. For example, if you needed a person to take steps until they get to a door, you would need one of two things; either you need to know the number of steps to the door and have to program the take-a-step method to happen that number of times or; you could use a loop that checks to see “is the person at the door” and if they aren’t, they take another step. That loop would continue until the condition of that person being at the door is met.
- Logical Operations
- Commonly used as parts of conditions and loops are logical operations. These can be a bit tricky, but commonly, there are 3 that we start being concerned with: AND OR and XOR (also called exclusive or).
AND is pretty simple. You must meet all conditions for the AND statement to be met. For example: if the person is at the door and the door is closed, open the door. If the person is not at a door, the condition fails at that point and is skipped. If the person is at the door but it is already opened, the step of opening the door is still skipped. Only if the person is at the door AND it is closed can you open the door.
OR (also known as an inclusive or) and XOR are slightly more complicated, but only because the reality is that in a language like English, we tend to assume that we mean OR when we mean XOR. OR means that 1 or more conditions are met. XOR means that only one of the conditions is met.
The best way to understand this is with a “restaurant” example. If you go to a restaurant and order an entree (please get one for me, too), you will probably be asked if you would like soup OR salad. Now what English tells us is that we can have the soup, or we can have the salad, but we can’t have both. In programming, OR means you can. The translation of this would be “You can have the soup” or “you can have the salad”. If the first case is true, then you don’t even worry about the second one..
With an exclusive or, if you have the soup, it is exclusive, so you cannot have the salad. If you have don’t have the soup, then you can have the salad. If you have both or if you have neither, the condition on that fails.
Most programming languages have a direct implementation of AND and OR (in logic, they are displayed as && and ||, respectively). However, not a lot of languages have a direct XOR operation. However, you can emulate that process by saying “if soup is true and salad is false, do something; if soup is false and salad is true, do something else. And if the person tries to say soup AND salad, or neither soup, nor salad, nothing at all is triggered.
Of course, I don’t recommend that if the waiter says “would you like the soup or a salad” that you answer with “sure”! They might not find it funny.
- A comment isn’t actually a part of code, in the sense that it doesn’t “do” anything. A comment is exactly what it sounds like; it’s a statement of a generic sort – when you build a project, you can use comments to note what you did and why. The compiler just ignores them, and go right on past them. But if you used a comment properly, then in several months when you go back to look at your code, you can leave a note for yourself on why you did something, when you made or changed something, where things come from and more.
Comments are the most useful ignored feature in programming, precisely because they let you track what’s going on, exactly where it’s going on, without actually doing anything to influence the outcome of the program itself.
This isn’t our only terminology; not by a long shot. But this is enough for the moment. In the next part of this set of articles, we’ll talk about the various types of variables, and their relationship to the concept of functions and methods.