From the Archive of 2015: Hello World: Introduction to C++

Let’s talk about coding

Welcome to my introduction into the topic of C++. Just over a week ago, I introduced the concept of programming to you, through my article: Introduction to Programming. In that article, I discussed different programming languages, and what it meant to actually be a programmer. I gave you resources to utilize, in order to figure out what kind of programming you wanted to do, and also mentioned different compilers you could download. In this article, however, we’re going to actually look at the logic and coding mechanics behind C++, instead of just talking about various resources that can be utilized to learn C++.

Now, in that aforementioned article, I mentioned that every programming language is different. Some of you out there may be more familiar with Java, or a website development language, for instance. My reasoning behind introducing you to C++ is simple: It is an incredibly powerful language, versatile and great for introductory programming. However, C++ can sometimes be tough and complicated; do not fret if you get too confused, but instead, contact me or do a little bit of researching until you’re clear on how to fix your problem. I’m going to demonstrate a few different, very basic programs and break them down for you so you can grasp the mechanics behind what they are performing.

Before I immerse you in the glorious world that is C++, however, we’re going to have to install an Integrated Development Environment (IDE) in which you will be writing your code. So, without further adieu, let’s go ahead and look at what an IDE is, how to install one and why we’re going to need one.

If you already have a C++ IDE, feel free to skip the next section and go straight to the actual coding section.


Integrated Development Environments

For the rest of this article, there are going to be two very essential tools you will need, in order to follow along. First and foremost, you will need a computer of some sort; I am, however, going to assume that you already have this, and are likely even reading this blog on a computer. Next, you are going to need an Integrated Development Environment; from this point on, I’m going to refer to these as compilers. To begin, let’s look at what a compiler actually is.

What is a compiler?

Quite simply put, a compiler is a piece of software that translates your code into binary. You see, computers are actually very simple. In fact, they only understand one single language: binary. Binary is a base-two number system that consists of 0s and 1s. For instance, if I wanted to type the letter “T” and have a computer understand it, I would need to type it out in binary: 01010100. Now, as you can see, it would be a huge pain to have to type everything out in 0s and 1s. In fact, you wouldn’t even be able to read this article, more than likely, unless you were fluent in binary. But, we do not have to do everything in 0s and 1s because the software we use every day, browsers and other programs, takes care of all of that for us. It turns our text, this very sentence, into binary for the machine to understand. And you get the benefit of being able to understand it as plain English.

When you really think about it, which of the following would you rather have to write:

std::cout << “Hello, world.” << std::endl;

Or…

01110011 01110100 01100100 00111010 00111010 01100011 01101111 01110101 01110100 00100000 00111100 00111100 00100000 00100010 01001000 01100101 01101100 01101100 01101111 00101100 00100000 01110111 01101111 01110010 01101100 01100100 00101110 00100010 00100000 00111100 00111100 00100000 01110011 01110100 01100100 00111010 00111010 01100101 01101110 01100100 01101100 00111011

I think it’s clear which one of the above most of us would choose. So, let’s go ahead and download and install a compiler. There are many compilers out there to pick from; and, in my screenshots, you will see me working with Microsoft’s Visual Studio 2015. Visual Studio 2015 is not free; however, there are free alternatives (and even a free community edition of Visual Studio.) Below, you can pick from a few different compilers. Pick one, download it and then follow the installation instructions it presents you on-screen. The default settings will be fine, for now. As such, I am not going to write about how to click ‘next’ a few times; just follow the instructions it gives you.

  1. Microsoft’s Visual Studio Community Edition
  2. Bloodshed’s Dev-C++

Hello World!

As the old adage goes, the best way to learn something is to simply do something. Or, at least, try to do something. Now that you have installed your compiler, let’s jump right into the coding process. To begin, I’m going to start you out right where most new programmers start: The ‘Hello World!’ of C++. For this article, we will be working with the development of console applications. Those are programs that run within your terminal (or command prompt), and have no actual graphical user interface. The “Hello World!” program is one such console application. It is a long-standing tradition that most new programmers begin with, and has been in use, in some variant of that output, since 1978! Below, I am going to show you the C++ variant of Hello World, and then we’re going to look at the different parts of the code.

// HELLO WORLD! - C++
#include <iostream>

int main()
{
  std::cout << "Hello World!";
}

Now, straight out of the gate, we already have seven lines of code. To those of you with any familiarity to programming, you can likely look at this program and visually see the logic behind it. But, this is an introductory article – and as such, we are going to go through each line and talk about what each one of those lines do for the program. Let’s begin with Line 1, shall we?

Line 1:  // Hello World! – C++

Line 1 in the above program is super simple. The entire point of that line is to function as a comment, within the compiler. A comment is a single line of text (although, you can have multiple line comments, as well) that has no effect on the behavior of the program or the rest of the code. It is denoted by the double slashes (//), and is used by the programmer to give brief descriptions of something. Think of it as a note – it serves no function other than to help you, or someone else, understand something better. Comments are, however, incredibly useful. As you all begin to familiarize yourself with programming, take advantage of comments. They will help you keep track of what you’re doing in your program. Feel free to use them as much as you need to, to remain comfortable.

Line 2: #include <iostream>

Line 2, and any other line that may start with a pound sign (#), are known as preprocessor directives. These directives are interpreted by your compiler prior to the compilation process, and tell the compiler that you are bringing in additional information that will be necessary for the compilation of the program. In this case, we included the <iostream> header file; as such, we now have the ability to perform various input and output tasks, such as printing to your console or screen, or taking in information from user input.

Line 3: [Blank Line]

Line three is a blank line. Blank lines have no effect on the code, however, programmers will utilize blank lines to increase the readability of their code. It is good convention to observe readability etiquette when writing code. Get yourself into the habit of developing code that is readable and properly formed, not just functional. This will make you a much more desired programmer, in the future.

Line 4: int main ()

This line is what you will come to know as a declaration of a function. In very simple terms, a function is a named group of various statements within a program. In this case, the name of our function is ‘main’. The proper construction of a function will include a data type (int, in this case), a function name (main, in this case) and a pair of parentheses. All in all, you will have something similar to: int main ().

Line 5 (and 7): {}

Line five and seven are braces. The opening brace, which you will find on line five, indicates the beginning of our function – which, in this case, is named ‘main’. The closing brace, at line seven, indicates the end of the ‘main’ function. The information inside these braces are known as statements, which we will look at next. Everything within these braces are known as the body of the function; the statements within the body is the action that will be performed when we call upon the function.

Line 6: std::cout << “Hello World!”;

Line six is a statement. These are the fun bits of code that actually perform an action. Typically, statements are a specific expression within your C++ code that will produce some desired (or sometimes, an undesired) effect. If there are multiple statements within a function’s body, the statements will execute in the order that they are coded.

In this example, our statement includes four different parts. The first part we run into in our statement is std::cout. This part identifies the standard character output device, which is normally our monitors, in which the output of our program will be displayed on. Next, we see <<. This is an insertion operator, and this insertion operator indicates that whatever follows it will be inserted into our std::cout part, so that it can be displayed to us. And, finally, we have “Hello World!” This is the actual content that will be inserted into the cout and displayed on the screen.

The fourth part of our statement is the semicolon. I’ve decided to discuss this in a paragraph of its own, as that one tiny little character at the end of line six is incredibly important. The semicolon indicates that we are finished with our statement. Just like you put a period, or some other form of punctuation, at the end of every sentence, you must include this semicolon at the end of every C++ statement. Failure to do so will cause compilation errors, and your program will not build correctly or function. Forgetting to include the semicolon is, verily, the most common syntax error that you will see. So, learn early to include the semicolon after every single statement.


More Code

Now, that was all relatively simple, after we broke it down, right? Go ahead and write the “Hello World!” source code out in your compiler a few times (don’t just copy and paste!) so that you can get used to how to develop simple console applications. When you’re finished, how about we move on to a slightly more complex console application? This time, we’ll look at a fundamental logic concept known as the If…Else statement. 

The If…Else statement is an extremely common, fundamental statement that you will use very frequently within C++. Having the ability to control the flow of your program – allowing your program to decide on what code it wants to execute – is incredibly useful, and very necessary. An If…Else statement will allow your program to decide whether or not it is going to run a certain line of code, based on whether or not a resulting condition is true or false. Let’s take a look at a very simple if statement first, and then we will add the optional else statement in the next rendition of our program. For now, let’s focus on the If statement.

//Basic "if" statement example - Tyler Jones.
#include <iostream>

int main()
{
 if (100 > 50)
 {
    std::cout << "It's true: 100 is greater than fifty.";
 }
}

Now, in the above example, we have our first if statement. Basically, we told the compiler that if 100 was greater than 50, output: “It’s true: 100 is greater than fifty.” But, of course we know that 100 is greater than 50. We don’t need to write a program to tell us that. But, what if it would have been a false condition? We could have utilized an optional else statement, depending on what we were trying to do. Let’s take a look at an if…else statement now, instead of simply an if statement. We’ll also play with some integers too, this time. We’ll even get interactive and take some user input, as well!

//Fun if...else statement. -- Tyler Jones.
#include <iostream>

int main()
{
 int a = 0,
     b = 0;

 std::cout << "Welcome to your first if...else program!" << std::endl << std::endl;
 std::cout << "Please input a number between 1-100: "; 
 std::cin >> a; std::cout << std::endl;
 std::cout << "Please input a second number between 1-100: "; 
 std::cin >> b; std::cout << std::endl;

 if (a > b)
 {
    std::cout << "Your first number was: " << a << std::endl;
    std::cout << "Your second number was: " << b << std::endl;
    std::cout << "Your first number entered was larger than your second number entered!" << std::endl;
 }
 else
 {
    std::cout << "Your first number was: " << a << std::endl;
    std::cout << "Your second number was: " << b << std::endl;
    std::cout << "Your second number entered was larger than your first number entered!" << std::endl;
 }
 std::cout << std::endl;
 system("pause"); // Alternatively, you can remove this line and simply "Start without debugging."
}

 

Now, the above code really isn’t as complicated as it looks. Essentially, it starts off by welcoming the user to their first C++ if…else program, in line 10. Then, it asks the user to put in a number between 1-100. It then asks the user to put in a second number between 1-100. At this point, we have called an if statement into play, and it basically says: if (the first number is greater than the second number)output: “Your first number entered was larger than your second number entered!”. Just like the first if statement we worked with. But this time, we have an else statement directly following our if statement. That else statement says, else, output: “Your second number entered was larger than your first number entered.” Think of the else as “otherwise”. In our case, if our second number entered was larger, the if statement we made would not turn out being true, so the else statement would, instead, be invoked in the if statement’s place. Fun, right? But, what if the first or second (or both) number entered was not between 1-100? Well, we could write an additional statement that would detect if the user input an integer between 1-100, and if it did not, it would inform the user of their mistake. But, we’ll leave that for another lesson.


Time to exercise!

I’m glad that you made it this far, reader. I hope you understood the concepts and logic we were working with, as I explained it line by line. Now, it’s time for you to demonstrate your understanding. After playing around with my “if…else” program above a few times to learn how it all works, I want you to write your very own first program. And we’re not talking about a simple “Hello World!” program, either. This time, I’m going to tell you the kind of program to write, and you see if you can do it!

Write a simple console application that asks the user to input a single integer. If the integer they enter is within 35 to 75, have the program output: “Yay, you just won this simple game!” or something similar to that. Else, if the integer is not within 35 to 75, have the program output: “You lose! Better luck next time.” or something similar to that. Good luck.

Feel free to hit me up on Facebook with screenshots of your code, if you decide to take this exercise! See y’all on the other side.

Tyler
Written by Tyler