1 Introduction
Hussam Ghunaim, Ph.D.
Learning Objectives
At the end of this chapter, you will be able to
- Describe how computers work.
- Describe how computers run programs.
- Differentiate between High-level and Low-level programming languages.
- Explain what algorithms are.
- Explain the basic structure of C++ code.
- Differentiate between syntax, logical, and run-time errors.
How Do Programs Work?
Hardware and Software
Computer systems are made of hardware and software components.

Hardware
Computers have various types and purposes. Figure 2 shows some examples. A computer that is normally used by only one person at a time is called a Personal Computer PC. However, Servers and Mainframes are larger machines that can be used by many users at the same time. When a number of computers are connected so that they may share resources and information, they are called a network.

In general, computers have five major hardware components:
Input Devices: This group includes any device used to read data into computers, like keyboards, microphones, scanners, touchpads, and many others.
Output Devices: This group includes any device used to output data from computers, like screens, printers, speakers, and many others.
CPU (Central Processing Unit): This device is responsible for running all types of programs on a computer. You can think of the CPU as the brain of a computer, figure 3 (a).
Main memory RAM (Random Access Memory): The CPU can not do anything without the help of RAM, figure 3 (b). For a CPU to be able to run a program, it must first load the program code into RAM along with all needed data.

Secondary Memory Devices: Although RAM is very important to run programs, it saves programs’ code and data temporarily. This means that when a computer is turned off, all information in RAM will be lost. Here comes the need for secondary memory devices to save information permanently. Examples are hard drives, soft drives, flash drives, CDs, DVDs, and many others, figure 4.

Software
Software consists of a set of programs written to control computer operations. Broadly, software falls into two main categories: operating systems and applications. Operating systems are specifically designed to manage computer operations and oversee all applications running on the system. When you power on your computer, the first software to load into RAM and execute is the operating system. Subsequently, other applications load and run. Notable examples of operating systems include Windows, MacOS, and Linux. Meanwhile, popular applications encompass Microsoft Office, Adobe products, internet browsers, favorite games, and more.
Software is developed using programming languages to write code that can be understood and executed by computers. Generally, programming languages can be classified into two major categories: High-Level and Low-Level programming languages.
High-Level and Low-Level Programming Languages
High-Level Programming Languages
This type of programming languages allow software developers to write computer instructions in a language similar to human languages. In Example 1 code below, you can read and guess what each line of code will do when executed by a computer. Popular examples of high-level programming languages are C++, Java, Python, and many others.

Low-Level Programming Languages
Conversely, this type of programming languages are written in a language that is closer to what computers can understand, but it is harder for people to use. Figure 5 shows an example of such languages called Assembly language. In the early days, developers had to use this type of programming languages as they were the only type available. Later, it became evident that it is better to develop high-level programming languages that are easier for everyone to use to write code.
How do programs run on computers?
The story starts with developers writing code for a program, such as Windows Word or probably your favorite Internet browser. This code will be mainly written in a high-level programming language. But we already know that this code can not be understood and run by a computer. Therefore, we need to translate this code into a machine language that computers understand and can run. This process is really complex and has many details. For now, it is adequate enough to understand the basics of it, Figure 6 depicts the major steps in this process.

The process of preparing programs to run on computers starts with a compiler that translates the source code of a high-level language into an intermediate code, usually called Object Code. Then comes the job of a linker that links our program object code with other needed code written and compiled by other developers, usually called libraries. Lastly, a loader loads all necessary code into RAM, which makes the program ready to run.
History of Programming Languages
The early start of computers can be traced back to the eighteenth century. At that time, only mechanical calculators existed, see figure 7(a). In a later era, punched cards were used as a storage medium to store programmed instructions, figure 7 (b). Some notable names in this field are Charles Babbage, who designed the first programmable computer which also known as the Analytical Engine. Ada Lovelace wrote the first program for Babbage’s machine.
![]() |
![]() |
High-level programming languages that were capable of handling complex operations did not come into existence until the 1950s. These languages were designed for specific fields. For example, COBOL specialized in business applications, FORTRAN in scientific computing, and BASIC in educational applications.
Programming languages are a dynamic field and are constantly evolving. New languages are developed to meet the challenges and needs of various industries. The C programming language was developed in the 1970s to support both low-level and high-level features for writing operating systems and other system software. Object-Oriented Programming (OOP) languages such as Smalltalk and C++ were developed in the 1970s and 1980s to allow programmers to write more complex software applications. The 1990s saw the birth of a new programming paradigm known as scripting languages. Some examples are Perl and Python. Rust, Julia, and Swift are another set of languages that are designed to address specific needs such as system programming, scientific computing, and mobile applications.
Writing Programs to Solve Problems
It is important to note that every program is developed to solve a problem or to meet a need or requirement. Solutions to problems are usually called algorithms. An algorithm can generally be defined as a series of steps that must be carried out to solve the problem at hand. Algorithm writing is usually the first phase in the software writing lifecycle, which includes analysis, design, implementation, and testing.
Algorithms
The best way to explore how algorithms are used to solve problems is through examples.
Example 01
Let’s start with an example that everyone is familiar with, which is calculating the average of a set of numbers. Before checking the provided answer and as a practice, write in detail the series of steps that you would need to find the average. As you already know, computers need detailed instructions to do anything useful.
Answer:
1. Read all the given numbers
2. Add all the given numbers and find their total
3. Divide the total by how many numbers we have
4. Print out the average
The following section shows how algorithm steps can be translated into code.
Converting Algorithms into Programs
This section is not about introducing C++ coding; rather, it is just a simple example to show how algorithms can be converted into code. Throughout this book, sometimes line numbers are provided for easier reference, they are not part of the code. The first 4 lines are the standard way to start C++ code. More on that in Chapter 2. Lines 6, 7, 8, and 9 are used to read three integers (whole numbers) from the keyboard. These lines are to execute the first step in our algorithm described in the previous section. Then, line 10 finds the total of these entered numbers, that is, step 2 in the algorithm. After that, line 11 finds the average, step 3. Finally, line 12 prints out the result, hence, step 4.

Comments Styles
One essential skill that is usually overlooked by students and first-time programmers is writing meaningful comments. If you look again at the example described earlier, you might say it is clear and easy to understand. This is true for now. But what will happen after a while? Do you think you will be able to remember all the details? Further, in the software industry field, it is common for many people to work together to develop the same program. This means developers need an effective way to communicate and exchange information. One of the methods to do that is through writing comments. Therefore, it is one of this book’s objectives to help students build up the habit of writing comments whenever it is desired or needed.
Below is the revised code with suggested comments that would help in explaining the code to someone who is not familiar with it. Read the code and try to come up with your own comments that you think it is helpful to add. Note that in this example, we used two comment styles. We used // for single-line comments and /* */ for multiple-line comments.

Object-Oriented Programming (OOP)
Programming languages are a fast-evolving field of computer science. One of the remarkable advancements is a developing methodology known as Object-Oriented programming (OOP). Rather than designing programs as a set of instructions, in OOP, a program is designed as a set of objects. Each object has its own data and algorithms to handle the data. This design methodology has proved advantageous over other methodologies by providing the following characteristics:
- Encapsulation: Known as an information-hiding technique that improves the security of the code, in addition to adding simplicity for developing the code.
- Inheritance: Objects can use code written in other objects, which can greatly simplify the code-development process.
- Polymorphism: This is a technique that can allow writing a code once, while it is possible to behave differently in various scenarios.
Students need not worry if those concepts are not clear, as they will be discussed again in due course.
Looking into a Simple C++ Program
Code_Snippet 1 shows that any typical C++ program includes several parts. In the following subsections, a brief description is given for each of these parts to get you started writing code. More details will be given as they become needed.
Keywords/Reserved words
Keywords or reserved words are a set of words used by the C++ system itself. This means you cannot use these names in ways different from their original definitions. Some examples are int, double, and return. Appendix A has the full list of these keywords.
As you might already have noticed, we can not do anything useful without being able to read and write data. These operations are really complex operations to be done. Luckily, we do not need to have deep knowledge to read or write data, as C++ generously provides us with a large set of predefined classes that can do a lot of work for us. In Code_Snippet 1, cout
and cin
are called stream objects. While cout
is used to send data to the computer console, cin
is used to read data from the computer keyboard.
Identifiers and Naming Rules
Reading and writing data dictate the need to have a place in the computer memory to save the data; otherwise, we risk losing it. RAM is the main memory used to run programs and provides space for them to save any data they need. RAM memory locations are represented as binary or hexadecimal numbers. Binary numbers are made from digits that can hold either a zero or a one, which is known as a bit. Every 8 bits consists of a byte.
Once again, we are lucky not to refer to RAM location addresses using these complex and hard-to-remember numbers. Instead, we can create labels to refer to any needed memory location. These labels are formally known as variable names. In Code_Snippet 1, we have created five of those variables. Three variables to save the values of the numbers, namely, number01
, number02
, and number03
. Another variable to save the sum of these numbers is called the total
. Finally, the average
variable is used to save the calculated average.
You can create and name variables as you wish. However, you must always create meaningful and descriptive variable names. Additionally, there are a couple of restrictions that you have to follow. Variable names cannot start with a number. They must start with a letter or underscore. Variable names can only contain letters, numbers, and underscores. Spaces are not allowed in variables’ names.
Exercise 01
For each of the following variable names, determine whether it is an acceptable name or not. Give reasoning for your choices: (answers are provided at the end of the chapter.)
1. 12 calculate
2. First_Name
3. myLastName
4. thisIsIt
5. total-score
6. final&GPA
7. x
Operators
Appendix B has a list of operators available in C++. In this section, we will only discuss the operators used in the provided Code_Snippet 1. Others will be introduced while you are reading through the book. In Code_Snippet 1, the following lines have three operators, the addition +
, the division /
and the assignment =
operators:
total = number01 + number02 + number03 ;
average = total / 3;
From these two lines, you can deduce that operators are called as such because they operate on something, usually called operands, to produce some sort of a result. The addition
+
adds its operands’ values, and the division /
divides its operands. The assignment =
operator assigns the value resolved from the right side to the variable name on the left side. That is, the assignment operator is always executed from right to left in opposition to most other operators that are executed from left to right, like the addition operator and all other similar arithmetic operators.
The following two lines have different types of operators, <<
is called the insertion operator, while the >>
is called the extraction operator.
cin >> number01 >> number02 >> number03 ;
cout << " The average = " << average << endl ;
As you can tell from the code, the extraction operator extracts (which means reads) data from the input stream cin
. In Code_Snippet 1, we are using the extraction operator to extract ‘read’ three numbers from the keyboard and save them into the provided three variable names on the right side of the extraction operator. On the opposite of the extraction operator, there is the insertion operator that inserts whatever is provided on the right side into the output stream cout
given on the left side. In Code_Snippet 1, we are sending to the output stream the string ”The average = ”, the value stored in the variable name average, and the object endl that inserts a new line. We can insert a new line by using \n in any quoted text, like this cout << "Hello!\n"
. Note that in C++, the semicolon ;
is used to end every line of code.
Including Libraries
Computers are complex systems, and operating them requires sophisticated software. In the early days of programming, developers had the unfortunate task of writing all the code they needed from scratch. This practice persisted until it was recognized that different programs often require a common set of operations. For instance, operations such as reading and writing data are essential in all programs. The realization that certain operations could be developed once and reused across multiple programs led to the creation of what we know as libraries. These libraries contain collections of useful operations that many programs can utilize.
In Example 01, we needed to read from the keyboard and write to the console. Rather we developing our own streams to do that, we simply used the predefined streams included in the C++ library called <iostream>
library. By doing so, we saved ourselves a great deal of work and technical complexity. Other libraries will be needed while you are progressing through this book. For example, to be able to create variables that are able to store entire sentences, you need to include a library called <string>
. The following include directives do that.
#include <iostrem>
#include <string>
As libraries are usually huge, there is always a possibility of name conflicts of their contents. Therefore, in every program that you will write in this course, you must specify what namespace you want to use. The following code is called a use directive:
using namespace std;
Common Syntax Errors with special characters!
Writing code can contain some confusing characters that might look similar but are entirely different. You need to find those characters on your system’s keyboard, as they might be located differently based on your system’s manufacturer and model.
- Forward slash
/
and backward slash\
- Underscore
_
and dash-
. The dash character is used for the subtraction operation and to denote the negative sign as well. - Single quote
’
and double quote”
and the tick character‘
. The tick character, sometimes called the Grave Accent character, is usually found on the top left corner of a standard keyboard. Using two single-quote characters instead of one double-quote character will produce errors, although they might appear to you as the same! - Colon
:
semi colon;
comma,
and dot.
characters might be mixed up by first-time programmers. Therefore, you have to pay more attention when you type your code.
Offline and Online Compilers
When you start writing code, you have to make a choice whether to use an online compiler or an IDE. IDE stands for Integrated Development Environment. IDEs have become popular for their many features that make writing code easier and more efficient. In this book, we will use Visual Studio IDE for two reasons: it is powerful and freely available to install and use on your system. In addition, readers are free to use one of the many available online C++ compilers. They are popular for their simplicity and free users from having an IDE installed on their systems. This link
takes you to a website that gives a nice comparison between top IDEs and online compilers. The projects at the end of this chapter will provide you with detailed help to install Visual Studio IDE and run your first C++ program. Additionally, it will show how to use some online compilers.
Find and Fix Errors in Your Programs
Writing code dictates that you fully understand the syntax rules and follow them literally. In practice, it is extremely rare for someone, no matter how experienced they are, to write code free of syntax errors. That is, it is an essential skill whenever you write a piece of code to test it thoroughly until you are pretty sure it is free from bugs. Bugs are an informal term that means errors in code. Historically, Grace Hopper discovered the first documented computer bug.
Syntax rules are the rules that say how we should write code. In the example discussed in this chapter, we followed all required syntax rules to declare variables, read numbers from the keyboard, and then calculate the average. If you tried to input the code yourself, you might get error messages from the compiler. IDEs are very helpful in discovering those bugs. This type of error is called a syntax error, which is relatively easier to find and fix compared to other types of errors discussed in the following sections.
When you compile your code without getting any error messages from the compiler, this means your code is free from syntax errors. However, this does not guarantee the code will run correctly. There are other error types that compilers can NOT detect, called logical and run-time errors.
Logical Errors
Logical errors are caused by programmers when they misunderstand the purpose of the program. For example, in our earlier example in this chapter, we had a code to calculate the average of a set of numbers. If you used a wrong equation to do that, the compiler will not complain, however, the results will be wrong.
total = ( number01 * number02 * number03 ) ; // you mistakenly multiplied all numbers instead of adding them!
average = total / 3;
Another popular logical error is declaring the wrong type for a variable. In Code_Snippet 3, the result of a division operation is saved as an integer type, which causes the fraction part of the result to be lost! In this example, instead of having the result as 2.67, we get 2, as shown in Figure 8!


Run-time Errors
This type of error only occurs when you run the code. An example is when the code has a statement to divide by zero. As we all know, division by zero is not defined and cannot be calculated. Therefore, the execution of the program will be terminated and show an error message.
In the previous section example, if you changed the statement that calculates the average as below, the code will cause a run-time error!
average = total / 0;
How should you test your code?
Testing code is a fundamental skill that every programmer must master. In the software industry, many testing methodologies have been developed solely for this purpose. In this book, however, testing will not be presented as an independent topic; rather, it is going to be emphasized and encouraged. Students will be repeatedly encouraged to test their code while working on exercises and projects.
The major theme of the testing that they should follow is incremental testing. This means that rather than waiting until finishing writing the whole code, which probably can be dozens or even hundreds of lines of code, to start testing, incremental testing will be far more efficient. Imagine that after writing a lot of code, you received an error message from the compiler (that is a syntax error), or even worse, a logical or runtime error. Then, you would start wandering around your code trying to find that error. Certainly, you will spend a great amount of time trying to fix the code. However, if you follow our advice, you should start testing after writing a couple of lines of code. In case you get an error warning, it will be relatively easy to find and fix that error.
How much testing is enough?
When it comes to testing code, there isn’t a specific number of tests that you must run. However, a good rule of thumb is to keep testing until you’re confident that your code is error-free. The concept of “sufficient testing” is relative, but for every program you write, you should run multiple tests to cover all possible input scenarios.
Consider the following guidelines for effective testing:
- Input Domains: If your code expects various types of input (such as numbers, strings, or characters), make sure your tests cover all of these input types.
- Faulty Input: Be deliberate about testing faulty input. What happens if the user provides unexpected data? Your code should gracefully handle such scenarios, whether it’s by providing informative error messages or taking appropriate actions.
- Comments and Console Messages: Writing lots of meaningful comments and printing meaningful messages to the console are essential skills you must use while testing your code.
Wrap up!
This chapter explored the basics of computers, software, and hardware. The main objective was to provide students with a better understanding of how code is written and executed within the hardware. Algorithms, C++ code structures, and preliminary information on keywords, operators, identifiers, and libraries were discussed. Additionally, this chapter emphasized the importance of thorough testing, including handling syntax, logical, and runtime errors.
Answers Key
Exercise 01:
1. Not acceptable because it starts with a number.
2. Acceptable
3. Acceptable
4. Acceptable
5. Not acceptable because it uses a not allowed special character. The only acceptable special character is the underscore.
6. Not acceptable because it uses &.
7. Acceptable, though it is not recommended because it tells nothing about its purpose.
End of Chapter Exercises
- In your own words, describe what hardware and software are.
- Give examples of input and output devices other than those mentioned in this chapter.
- What is the main function of RAM, CPU, and Hard drives?
- What is the difference between high-level and low-level programming languages? Give an example for each.
- Briefly explain the function of a compiler, a linker, and a loader.
- What were punch cards used for?
- In your own words, describe what algorithms are.
- Write an algorithm to find out the total cost of your groceries.
- Write an algorithm to find out whether a student is passing or not the computer science course based on their grades.
- Why is it important to write comments within the code?
- How can you insert comments in C++ code?
- What is the use of
cout
andcin
objects? - What are the uses of the variables? What rules do you have to follow when creating variables’ names?
- What are the uses of
<<
and>>
operators? - In your own words, define what libraries are and why they are included in C++ coding.
- What does IDE stand for? What IDE will be used in this book? Why?
- Find the syntax errors in this code:
_
- What errors can a compiler not find?
- In your own words, describe the three different types of errors: syntax, logical, and run-time errors.
- Why should you test your code? How many tests, on average, should you run on your code?
Projects
Project 1: The goal of this project is to get you to start using Visual Studio to write C++ code. All provided links are tested at the time of writing this book. If any link becomes inaccessible, you are advised to search the web for the updated links.
The official Microsoft Visual Studio page (click here) provides two links to download the Visual Studio installer for Windows and Mac users. For either platform, three options are available: Community, Professional, and Enterprise. Choose the Community option as it is free and yet powerful enough to learn to code in C++ effectively and efficiently. Below are two suggested YouTube videos that can guide you through the installation process. However, you are advised to search for the most updated help available online.
After installing Visual Studio, create your first project as follows:
Step 1: Start Visual Studio and create a new project. Name the project as firstProject.Proj1
Step 2: Create a new file and rename it as yourName.cpp. Usually, you need only to submit the cpp file for grading, NOT the entire project folder.
Step 3: In the yourName.cpp file, add the following code. Pay close attention to the capitalization of the words. C++ is a case-sensitive language, which means words like myname, myName, MYNAME, and MyName are all recognized as different words.
Step 4: Fill in your name in line 5. Run the code and examine the output.
Step 5: Writing comments is an essential skill all programmers must master. Thus, add the following comments at the top of the file. Replace the dots with your name and other appropriate details.
/*
* Author: Your Name Goes Here.
* Email Address: you@domain_name
* Description: .....................
* Last Changed: .....................
*/
Step 6: Change the keyword cout to Cout in line 5. Try to run the code and write your comments on what happened and why.
Cout << "My name is ........ < < endl ; // The Cout with capital C ( run/ did not) run because .......
Step 7: Change back the Cout to cout again to fix the error.