Contents
Now we will begin our journey into the wonderful world of computer programming. Before we begin, we need to choose our language. There are many languages to choose and each language will have different strengths and weaknesses. Two that are very popular to start with are C++ and Java. They are both general purpose languages and come from the common ancestor of C. For this book, we will use C++. C++ is an object-oriented language with features for procedural programming mixed in. We will start by looking at the procedural aspects and then moving into the object-oriented aspects later on.
Starting off with a basic program is a staple for all programming texts and we will not disappoint. We will look at the classic Hello World program. This program is used because it shows many of the basic features of the language and allows us to get a short basic program to exercise our development environment.
01 #include <iostream>
02
03 using std::cout;
04
05 int main()
06 {
07
08 cout << "Hello, World!\n";
09
10 return 0;
11 }
Program 2.1
In our program 2.1 we have a basic program. It's a small 11 lines and it shows us several features of our chosen language. On line 01, we have this line: #include
Lines 02, 07, and 09 are simply blank lines. These lines are there for the developer of the code so they can more easily read the code. Use white space and blank lines to make your code easy to read. Don't be like the student who crams all of their math problems in the top left corner of their page. White space will not make your code that much larger and the ease of reading your code will greatly outweigh any size savings.
Line 03 is a using statement. This is one way we let the compiler "know" about any name that we may want to use. In this case, we want to use cout. cout is the standard output stream and it's how we get information to be displayed, typically, on the terminal window. There are other ways of doing this using statement and we'll see others later.
Line 05 is the start of our program. Every program in C++ must have a main program and it will have 1 of 2 signatures. The one shown on line 05 is common and the one we will use for most of our work. The word int tells the compiler that the main function will return something of the type int, short for integer. That's where line 10 comes in.
Lines 06 and 11 are the lines that contain the opening and closing curly braces respectively. These braces group the code that is inside of it as a unit. They do not need to be on lines by themselves and many programmers like to move the opening brace up to the line that precedes it. I prefer mine on lines by themselves, but there is nothing wrong with having them in different places as long as they group the correct parts of the code.
Line 08, is our output statement. cout is the standard output again. The << stand for the insertion operator, or some times call, put to. The words between the quotes are what is output. So the entire line can be read backwards as "Hello, World!\n" is put to cout. The result of the line is that the words "Hello, World!" followed by the "\n", which is the newline character are output on the standard output, or the terminal. The newline character is similar to pressing the enter or return key on the keyboard when you are typing.
Line 10, is the last executable line in our program, and technically not necessary in the main function, tells the compiler what value is returned from our main function. Zero is traditionally returned as the value when the program terminates successfully.
So that is a complete, albeit short, program. I suggest that you take that code and see about having it compiled and run on your computer. See the section about compiling and running code for more specifics for your operating system.
Now that we have seen some C++ code, it's time to learn about the 4 basic ways to structure our program. We will be using these same basic structures in all of our code whether it's procedural or object-oriented code.
With these 4 basic ways to structure our program, we can put together very complex software. We will combine these pieces in small, well defined ways to build our programs from basic parts to larger wholes. It is how we combine these structures that gives each program its unique functionality and design.
Sequence is the most basic way to structure a C++ program and we get it by simply putting one code line after another. The shear fact that a statement is later in the code then a prior statement implies it will be executed after the earlier statement. We can use this to our advantage when we want to delay when something is executed. It will also mean that there will be times that we will have have to alert the compiler about our wishes before we can do other operations.
At its most basic level, selection allows our program to either execute some code or not. Selection is the structure that allows our code to make decisions based on values that are stored in our program during execution. Selection can be simple, like a yes/no, or complex, like a multi-way selection that makes a choice out of an array of possible scenarios. How simple or complex the situation is depends on what problem we are trying to solve.
Iteration is the structure that allows our code to be repeated. We can repeat a single line of code or a large block of code. We can have our iteration repeat once, many times or even not at all.
Functions allow us to name blocks of code and be able to recall them when we need. We can combine the previous 3 ways to structure our code within functions. Functions allow us to easily reuse our code with minimal amount of work to do so. We can give our functions information and they can give us back results.
These four basic ways we structure our program by themselves are fine and can only do so much for us, so what we do is use all four of the in combination to build our code. We will practice each individual structure and then learn to assemble them together. When I go to write a program, I envision the problem as a set of these structures. So the larger problem is just assembled from the smaller basic parts.
The most basic way we structure a program is through sequencing. That means that the code that is written first in the program are executed first. This alone isn't enough; we need to know how to tell the computer more about what we are doing. We saw earlier how we can output messages to the standard output, but these programs are missing more information. We need ways to store information and compute new information.
In C++, there are 7 built-in data types for storing basic information. There are types for integer type information and types for floating point information. The following is a list of the built-in data types that C++ recognizes.
Types 1-5 are known as the integer types because they represent data that can be represented as integer data. Integer data is also known as whole numbers. It is important to note that C++ does represent character data as integers "under the hood" and that bool represents true and false values. C++ also represents bool data as integer values and this can cause issues for programmers who are unaware. int, short and long all represent regular integer values with varying amounts of storage set aside. For most of the purposes for our programs we will use int as our integer data type.
Types 6 and 7 are used to represent floating point or decimal values. For our purposes we will use double for our floating point values. It is not hard to introduce imprecision if we use float instead.
So why do we need to know about data types? We need to know about data types because we need a way to let the compiler know how much memory to reserve for our program. While it is possible to manage this sort of information on our own, it is much more convenient to simply let the compiler do this work for us. So to do this, we need to tell the compiler what data we need to store and process. When we do this, we use variables. A variable is simply a named piece of memory. We can now access this memory by using its name. This is how almost every memory location is accessed, via a variable name.
So to declare a variable we need to tell the compiler the type of the variable and then the name we wish to use. For example:
int fred;
char billy;
float boat;
It should be noted that when I name variables I almost always use lowercase letters. If the name I choose needs to be more descriptive, I typically will stick the two words together and capitalize any subsequent words where they are joined.
So we have just learned a basic statement. The statement declares to the compiler the type of the data and the name we wish to use. We can only use a name once in an area of code and the names are case sensitive. There are some basic rules to naming variables.
When we name a variable, we must start our name with a letter or an underscore and then we follow that with any (reasonable) amount of letters, numbers or underscores.
It's not a good idea to start your names with underscores as the compiler often will use names that start with underscores. So in practice I always start with letters. Also, names may not have spaces in them, nor any other special characters other than the underscore.
So now we know how to name variables so let's look at another basic example that does a little more than the first example.
#include <iostream> //this allows us to output
using std::cout; //this is our standard output
using std::endl; //this is like \n, the newline character
int main() // our main program
{
int value1; //this is our first value
int value2; //this is our second value
int sum;
value1 = 5; //this is the value we want for value1
value2 = 15; //this is the value we want for value2
sum = value1 + value2; //add value1 and value2 and store the result in sum
cout << "The sum of " << value1 << " and " << value2 << " is " << sum << endl; //This will print the output line
return 0;
}
Program 2.2
So this example allows us to add to numbers and print the sum. This has some issues. First, if we want to change the values, we need to recompile our program with new values and then rerun our program. It would be better if we could input the values when the program runs. We will be ready to do this very soon. We just need to learn about our standard input.
Second, this only adds two numbers and is very limited. It would be nice if we could add more than 2 numbers. To solve this problem, we will need to learn more about iteration.
So this little example shows us sequence in a number of ways. First, we declare the variables at the top. If we don't declare them first, then we can’t use them later on. Second, we then give the two variables their values. Again, we have to have declared them first. Then we use the variables to give the sum its value. Finally we print the result. We can't print the result first. Try moving the line with the cout to before the summing line and see what happens. Try moving the output line before the lines where value1 and value2 get their values and see what happens.
In this chapter we discussed one of the most basic ways in which we organize our programs using sequence, selection, iteration and functions. We learned about data types and variables. We also saw two different examples of programs illustrating sequence. In the following three chapters we will look at selection, iteration, and functions.
Written by: David McPherson © 2019