Today I want to learn…. Introduction to Programming II
Jun0
So want to learn more about coding? Ready for the craziest thrill ride of your life? Well, it’s not going to be that big of a deal. But If you read my previous post on programming, you should know what a program is and how it is made. So, it’s time to learn that gibberish and figure it out from there. To teach you these concepts, I’ll be showing examples in the most widely used programming language at the time of this article: C++.
Compiled Languages and Scripting Languages
All programming languages can be divided into two categories, compiled and scripting. So what’s the big difference? Well, remember when I was saying that we need a compiler to make the computer be able to execute your code? Well, the difference between these languages is when and how the code is compiled. Compiled languages, in order to be run, must be sent through a compiler and then executed. Scripting languages, on the other hand, are sent to the computer as is. The computer then has an “interpreter” built in, and “compiles” it every time it is run, on the spot. To be truthful, compile is the wrong word for scripting languages, so it is best to think like they’re being read by the computer. The best way to think about it is that for compiled languages, we need an interpreter (compiler) before the computer can read the code, and for scripting languages, the computer learns the language so it can do the instructions, just by giving it the instructions raw. Both types of languages have their own advantages and disadvantages. For the examples, we’re using a compiled language, C++.
Includes
So, we know what a compiled language is. So what’s next? Every language has a basic set of terms (also known as functions) by default. Some languages allow you to insert more terms into your program, and that is what includes consist of. C++ requires certain functions in order to act like it should. So, we need to include a list of functions, like so:
#include <iostream>
using namespace std;
as you can see, we have an <iostream>. This provides us with functions that allow us to produce actual output that users can see. as for the “using namespace std;”, this is the basic set of commands that the C++ program will understand. the “std” is short for standard. So, inserting this at the top of your file will include the iostream set of commands, as well as the standard set of commands.
The Semicolon
This is a good time to talk about one of the most vital parts of programming: the semicolon. Basically, think of the semicolon like a period; it ends your sentences and instructions
Functions
One of the things that almost all programming languages have in common is functions. A function is basically a way to package a set of instructions. So if we want our program to execute that set of instructions, we simply “call” that function. Now, for C++, we need a “main function” that the program executes when it starts. For C++, it looks something like this:
int main ()
{
//Instructions Here
}
So now we need a command, or some instruction to give the computer. One of the most standard beginning programs for a programmer to make when learning a language is called a “hello world” program. Basically, all it does is spit out “hello world”. So, let’s do that here, with the command in the iostream known as “cout”, which will basically print out whatever we write after it. So here it is:
cout << “Hello World!”;
So the compiler knows that we want the computer to produce this particular output “Hello World”. So, in the end, what does are code look like? Something like this:
#include <iostream>
using namespace std;
int main () {
#instruction here
cout << “Hello World”;
return 0;
}
If you give this to a compiler, it will understand exactly what you are talking about, and create a program that can be execute on whatever OS your compiler is for. Sweet, right?
No comments yet.