Learning to Code in any Programming Language

Shout out to CheersKevin for creating a great video and helping me look past complicated subjects.

Understand Logic

Everything in programming has a cause and an effect. There is no way around it and most code boils down to true or false statements. All code also reads from top to bottom with minor exceptions. As well as all numbers in programming starts from 0 and increases or decreases.

> True or False

Imagine programming to consist of only facts. You want to write your code to prove that something is going to be true or prevent something from being false.

Let's take this line of code for example (Javascript):

0. var blueBird = "Blue";  
2. if (blueBird === "Blue") {  
3.      alert("Yep that's a Blue Bird!");  
4. } else {  
5.      alert("That is not a Blue Bird!");  
6. }  

Now reading that code you can determine that a variable called blueBird is assigned some text that says Blue. We can use an if statement to determine if the variable blueBird contains the world Blue. If it's true it will alert and state that the bird is blue. If it does not state that it is Blue then it will tell us something else.

So what can we learn about syntax from this minor amount of code.

  • Code is written / read from top to bottom. (With some exceptions)
  • === Probably means equals.
  • if is like asking a question. If this is true or false, then...
> Variables

Most programming languages have a few different ways to handle variables. Variables sometimes need types and sometimes they're just called var like in javascript. To give you an idea here's a few basic types of variables.

  • string or char is usually text. "This is some text"
  • int or double usually deals with numbers. 6,25,40,-50
  • boolean or bool usually deals with true or false true, false
  • In some languages you can often name an entire variables type based on an entire section of code. This would be called a struct or class
> Operators for Math

A lot of programming uses basic mathematics. You should definitely know your operators.

  • + is to add.
  • += is to add to.
  • = is to assign to.
  • == is often equal to.
  • * is to multiply.
  • - is to subtract.
  • -= is to subtract from.
  • / is to divide.
  • < Less than.
  • > Greater than.
  • <= Less than or equal to.
  • >= Greater than or equal to.

Basic example of using math operators (C#):

int myNumber = 25;

myNumber += 10;

Console.WriteLine($"My New Number is {myNumber}.");  

Console Output:
My New Number is 35.

So if you look at the logic of that simple bit of code. We start at the top and look at the variable called myNumber. It has a type called integer and we can assume that it's a number. We assigned the number 25 to it.

With the number 25 assigned we want to add 10 to our variable.
If we use += than we'll be able to add to our current variable value.

Which results in a new number which is 35.

> Operators for Comparing

There are also several ways to compare multiple values in programming. Most languages use the same set of code.

  • && is and.
  • || is or.

There are others but these are the most common ones you will use.

Let's throw these into action with some (Java):

0. int myNumber = 25;  
1. int otherNumber = 25;  
3. if (myNumber == 25 && otherNumber == 25) {  
4.      System.out.println("This is a true statement.");  
5. } else {  
6.      System.out.println("This is not a true statement.");  
7. }  

Console Output:
This is a true statement.

Now reading from top to bottom we can assume the following. We assign myNumber the integer of 25. We also assign otherNumber an integer of 25. We mentally know that these two values are equal. We do have to tell the computer that they are equal. We will ask it a question.

We state:
if (myNumber == 25 && otherNumber == 25)

This is read as:
if myNumber equals 25 and otherNumber equals 25 then...

After that statement we tell the console to log an answer for us. In java that just so happens to be System.out.println("Text");

> if and else Statements

All programming languages have their own interpretation of an if and else statement. They're easy to follow and pretty much read as logically as possible.

Here's a simple example (Python3):

0. playerHealth = 100;  
2. if playerHealth >= 0:  
3.  print('Player is looking healthy.')  
4. else:  
5.  print('Player is not very healthy.')  

Console Output:
Player is looking healthy.

> What's next?

If you've noticed through this brief post I've used different languages for each example. If you understood the basic material than you understand that most programming languages are very similar to one another.

I mean just look how these programming languages output to console or some sort of message box.

C# - Console.WriteLine("Hello World");

Python - print('Hello World')

C++ - cout << "Hello World";

Javascript - alert("Hello World");

Java - System.out.println("Hello World");

CoffeeScript - print "Hello World"

You can see that these languages have very similar structure. It's just a matter of learning the keywords used for each language.

What's next though?

Well you need a problem that needs a solution. I recommend starting small. Think of something you do on your computer on a daily basis, is there any way you can improve upon it. If you can maybe you should do some research on languages you could use to do that.

There are a lot of different languages out there that can solve different problems. You just have to determine what language will suite your needs to fix your problem. Here's a list of some of the most common programming languages in the world today.

  • C#
  • Javascript
  • Java
  • C++
  • Python
  • CoffeeScript

You're not wrong if you choose one language over another. Every programmer needs a good foundation and understanding before they can truly move forward.

After reading this short blog post, you should check out courses on Codecademy. The only way you'll truly learn to program and be proficient is to write something you're passionate about. Find your problem and learn to write about it.