Coding

In my younger days I spent a lot of time at an awesome place called AACAL, where I fed my urges to invent, dabbled in robotics, and first learned what an engineer was.
This is also the wonderful institution where I was introduced to the world of code.
Sure I had a class or two about coding in college before I changed my major, but it was already one of my favorite subjects.

These days, I spend a lot of time waiting for things.
Waiting for class or rehearsal to start, waiting for my to-go order at a restaurant, waiting for a friend to finish an errand – because if you ask me to run errands with you, I’m going to sit in the car while you go inside boring buildings and do boring stuff – waiting in various waiting rooms to do adult junk, and so on, and so on.
During these moments of nothingness, I like to find ways to keep myself entertained.
Thanks to randomly perusing the App Store on my iPad, my new – well, renewed – entertainment is…
Yes, I’m sure you’ve guessed it… Coding.
I’m not an expert, because I’m not an expert, but nevertheless I want to share with you some of my knowledge of the basics.
The building blocks if you will.

There are many, many coding languages to choose from, but the one I’m going to focus on is called C++.
Why? Simply because it’s one of the easiest.

So, what is it?
Created by Bjarne Stroustrup, C++ is a programming language that I like to think of as the buff star football-playing son of the programming language C.
I don’t know what you get, but that gives me a nice visual to work with.
C++ can be used with a wide range of software, it is one of the easier languages to learn as mentioned before, and it’s behind a lot of today’s computer games.
Just in case: a programming (coding) language is a special arrangement of commands, statements, variables, abbreviations, and values that essentially makes a computer do what you want it to. Each language is different, and all software is written using one of them.

Before you start…
To write your code and create your program, you first have to have:
a) An Integrated Development Environment (IDE)
b) A compiler
The IDE is where you will be writing your code, meaning any program where text can be edited will work, and the compiler is what will put the pieces of your code together like a sandwich so you can enjoy what you’ve just created.

There is a free program you can download to your computer with an IDE and compiler called “Code::Blocks”, and it can be found HERE. Just choose the version that matches your operating platform.
image
The app I use on my iPad is called “CppCode”. You can search for it in the App Store, or use the link HERE. It also includes both the IDE and compiler.
image

Let’s get it going.
We’re going to make the first program I’m sure everyone learns to create.
It’s very basic, so no need to be nervous!
The first line of a C++ program is – #include iostream

There are pointed brackets included in this line, but I could not show them here. The computer thinks they are part of an active code, so in the published version the information inside them is deleted. You can scroll down to the final example to see where they should go.

The pound sign (#), is what tells the compiler’s pre-processor to allow your next set of code to do its job. “include” tells the pre-processor to include all the functions associated with the header, and “iostream” is the header we’re going to be using. There are different C++ headers designed for different types of programs to work properly. This particular one, will allow us to use a certain stream that has objects for the input and output of data.
The next line is – using namespace std;
This tells the compiler we want to use all the features in the C++ Standard Library.

Simple, right?… Eh… Kinda… Well let’s keep going anyway!

Our next line is – int main()
“int” is actually an abbreviation for integer, and when used it means you are declaring a variable. Our variable for this program is “main()”. There is no limit to how many variables you can use, but we will only need one.
This is also the most important variable. Think of it as the key that allows you entry to the program you are creating.
The next line is – {
Yep, that’s right. One little symbol.
This curly bracket tells the compiler that you are beginning a function, and the information between this bracket and our closing bracket, is called the function’s body.

As your codes become more and more complex, you’ll realize that correct placement of spaces and tabs while you are writing is important for readability. You can review the example below to see what I mean. They may seem like a hassle at first, but tough cookies; they have to be included if you want to be awesome at life.

Our next line is – cout <<"Hello world!";
Remember when we used “iostream” so that we could access a stream with objects that input and output data?
This is where we put it to work.
“cout” is the object we will use to tell the program what we want to show up on the computer screen. It must always be used with its partner in crime, “<<" – the insertion operator. The information we write after the insertion operator is what "cout" is going to look at, and know to send to the computer screen for everyone to see. The statement ""Hello world!"" is text that we want displayed, so it is important to remember to put in in quotations. If there are no quotations, "cout" will think it is a variable, and the program will not work because the only variable we have defined is "main ()". The semicolon at the end is also very important, and must be included at the end of every statement. Think of it as a punctuation at the end of a sentence.

Only two lines left! Aren't you excited?!

Our next line is – return 0;
“return 0;” is known as the return statement. Think of it as a single giant semicolon for the entire function of “main ()”. This let’s the compiler that no other information is to be returned to the program, and that we are ready to close our tab at the C++ bar. If this line is forgotten, the compiler will automatically add it to stop the function.
Finally, our last line is – }
The first curly bracket told the compiler that we were beginning a function, this one tells the compiler that we are ending that function.
Everything is closed and put to an end, so our program is now officially complete.
Save the project. Compile the project. Run the project. Give yourself a pat on the back.
You did it.

Example of code
image

Example of finished result
image

Advertisements