Introduction

Lesson by Rogério Júnior

The first question people ask when they decide to start studying computer science is: what do I need to make a program? It is quite possible to write and compile a file in any language using notepad and a computer terminal, but it is much better to use tools that facilitate this process. Ideally for those just in the beginning is to use a program that joins the editor and the compiler, an IDE. IDE (Integrated Development Environment) is where we are going to write the program, it facilitates the reading, correction and organization of the code, and also makes debugging (fixing errors in the code) easier. Having written the code, the compiler will transform the commands (written in some programming language) into something the computer understands and then create the program.

In this course, the language used will be C++ . You may use any IDE you want, but we recommend, and we will use during the examples, CodeBlocks , which already comes with compiler. If you want to download it, click here . Another very useful IDE in Dev-C++, click here if you wish to use it. Regardless of the choice, the installation is done very simply (usually by pressing "Next, Next, Next ...").

Now that you have everything you need, let's begin.

To begin writing our first program go to the "File" menu, then hover over "New" and press "Empty file".

Save your file with a name of your choosing and the computer will create a program witha a similar name. It is very important that you write ".cpp" (without the quote marks") at the end of your file's name, like myfile.cpp, so the compiler can recognize its language as C++.

The first program we will write simply prints something on the screen. To begin with, just as we would begin any other, let's declare the libraries we will use. Libraries are kinds of dictionaries with some commands. So, when we use a command in the program, the library in which it is located must be declared at the beginning of the code so that the computer knows what that command means.

If the name of a library is name, to declare it we write #include < name >. It is important to know that the computer does what we tell it to do in the order the commands are written, so it is important to declare the libraries that we will use at the beginning of the code, since we can't use a command before declaring its library. To print some sentence on the screen, we will use a function called cout, which is in the iostream library.

After declaring the libraries, you should write the following line: using namespace std;, it is important to make using cout and, eventually cin easier. But we won't be specific about what it does know.

After declaring the libraries, let's declare function main. In C++, main is the first thing the computer will look for when running the program. Inside it, there will be the commands that the computer will perform in the order in which they appear. The main displays the grammar: int main () {program commands}. This means that we will write int main followed by parentheses and, in braces, what we want the program to do. At the end of main, it is common to put return 0;, this command informs the operating system that our program has reached the end without any errors.

Also, notice there are comments. They are all that are written after a //. As the name says, they are just comments from the author of the code and the computer completely ignores everything that is after a //.

That said, let's see what a generic code would look like.


#include <iostream> // Declare the library you will use, in this case, iostream

using namespace std;

int main(){ // declare the main and open braces
  
  // here you should write all the commands of the program in the order they should be executed
  
  return 0; // tell the operating system that the program ended
} // finish main closing braces

So far, we import libraries and declare main . Let's now use a function that does the job we want, which is to print something on the computer screen. Before that, know that most of the commands we give to the computer (with very few exceptions) are followed by ; (semicolon.) With functions it's no different. The function that prints characters on the screen is cout, which is in iostream, which we have already declared. To print a sentence, we call cout, followed by two smaller than signs (<), and shortly thereafter, we write the phrase we want in double quotation marks, with ; after the function to indicate end of command: cout << "phrase we want to print\n"; . The '\n' at the end of the sentence is a character meaning line wrap (i.e. it's like pressing the Enter key when writing a text). We should get used to putting it because problems usually want us to do this at the end of the answers.

Let's see how a code that prints "Ola Mundo!" (which stands for "Hello World!" in portuguese) on your computer screen:


#include <iostream> // Declare the library you will use, in this case, iostream

using namespace std;
 
int main(){ // declare the main and open braces
  
  cout<<"Ola Mundo!\n"; // command that displays one phrase on the screen
  
  return 0; // tell the operating system that the program ended
} // // finish main closing braces

Now, look for the compile button on your IDE. Compiling is the action of transforming your code in a computer program. In Codeblocks, it is at the top, in the toolbar, as ilustrated below:

Generally pressing the F9 key helps.

Ta-da! Now that you have made your first program and it should sort of like this:

Now, let's put your knowledge to test. After every lesson you should first do the problems and only then advance to the next subject.

To see the problems, click on the puzzle icon at the top of the page (as shown below) and if you have any doubt you can ask for help using the comment section, which can be acessed clicking at the balloon.