Chapter 1
How a Computer Works
1.1 Introduction
In today's society, almost every task requires the use of a computer. In schools, students use computers to search the Internet and to send emails. At work, people use them to make presentations, to analyze data, and to communicate with customers. At home, people use computers to play games, to connect to social networks and to chat with other people all over the world. Of course, don't forget smartphones such as iPhones. They are computers as well!
Computers can perform so many different tasks because of their ability to be programmed. In other words, a computer can perform any job that a program tells it to. A program is a set of statements (often called instructions or commands) that a computer follows in order to perform a specific task.
Programs are essential to a computer, because without them a computer is a dummy machine that can do nothing at all. It is the program that actually tells the computer what to do and when to do it. On the other hand, the programmer or the software developer is the person who designs, creates, and often tests computer programs.
This book introduces you to the basic concepts of computer programming using the C# language.
1.2 What is Hardware?
The term hardware refers to all devices or components that make up a computer. If you have ever opened the case of a computer or a laptop you have probably seen many of its components, such as the microprocessor (CPU), the memory, and the hard disk. A computer is not a device but a system of devices that all work together. The basic components of a typical computer system are discussed here.
►The Central Processing Unit (CPU)
This is the part of a computer that actually performs all the tasks defined in a program (basic arithmetic, logical, and input/output operations).
►Main Memory (RAM – Random Access Memory)
This is the area where the computer holds the program (while it is being executed/run) as well as the data that the program is working with. All programs and data stored in this type of memory are lost when you shut down your computer or you unplug it from the wall outlet.
►Main Memory (ROM – Read Only Memory)
ROM or Read Only Memory is a special type of memory which can only be read by the computer (but cannot be changed). All programs and data stored in this type of memory are not lost when the computer is switched off. ROM usually contains manufacturer's instructions as well as a program called the bootstrap loader whose function is to start the operation of computer system once the power is turned on.
►Secondary Storage Devices
This is usually the hard disk or the SSD (Solid State Drive), and sometimes (but more rarely) the CD/DVD drive. In contrast to main memory (RAM), this type of memory can hold data for a longer period of time, even if there is no power to the computer. However, programs stored in this memory cannot be directly executed. They must be transferred to a much faster memory; that is, the main memory.
►Input Devices
Input devices are all those devices that collect data from the outside world and enter them into the computer for further processing. Keyboards, mice, and microphones are all input devices.
►Output Devices
Output devices are all those devices that output data to the outside world. Monitors (screens) and printers are output devices.
1.3 What is Software?
Everything that a computer does is controlled by software. There are two categories of software: system software and application software.
►System software is the program that controls and manages the basic operations of a computer. For example, system software controls the computer's internal operations. It manages all devices that are connected to it, and it saves data, loads data, and allows other programs to be executed. The three main types of system software are:
►the operating system. Windows, Linux, macOS, Android, and iOS are all examples of operating systems.
►the utility software. This type of software is usually installed with the operating system. It is used to make the computer run as efficiently as possible. Antivirus utilities and backup utilities are considered utility software.
►the device driver software. A device driver controls a device that is attached to your computer, such as a mouse or a graphic card. A device driver is a program that acts like a translator. It translates the instructions of the operating system to instructions that a device can actually understand.
►Application software refers to all the other programs that you use for your everyday tasks, such as browsers, word processors, notepads, games, and many more.
1.4 How a Computer Executes (Runs) a Program
When you turn on your computer, the main memory (RAM) is completely empty. The first thing the computer needs to do is to transfer the operating system from the hard disk to the main memory.
After the operating system is loaded to main memory, you can execute (run) any program (application software) you like. This is usually done by clicking, double clicking, or tapping the program's corresponding icon. For example, let's say you click on the icon of your favorite word processor. This action orders your computer to copy (or load) the word processing program from your hard disk to the main memory (RAM) so the CPU can execute it.
Programs are stored on secondary storage devices such as hard disks. When you install a program on your computer, the program is copied to your hard disk. Then, when you execute a program, the program is copied (loaded) from your hard disk to the main memory (RAM), and that copy of the program is executed.
The terms “run” and “execute” are synonymous and can be used interchangeably.
1.5 Compilers and Interpreters
Computers can execute programs that are written in a strictly defined computer language. You cannot write a program using a natural language such as English or Greek, because your computer won't understand you!
But what does a computer actually understand? A computer can understand a specific low-level language called the machine language. In a machine language all statements (or commands) are made up of zeros and ones. The following is an example of a program written in a machine language, that calculates the sum of two numbers.
0010 0001 0000 0100
0001 0001 0000 0101
0011 0001 0000 0110
0111 0000 0000 0001
Shocked? Don't worry, you are not going to write programs this way. Hopefully, no one writes computer programs this way anymore. Nowadays, all programmers write their programs in a high-level language and then they use a special program to translate them into a machine language.
A high-level language is one that is not limited to a particular type of computer.
There are two types of programs that programmers use to perform translation: compilers and interpreters.
A compiler is a program that translates statements written in a high-level language into a separate machine language program. You can then execute the machine language program any time you wish. After the translation, there is no need to run the compiler again unless you make changes in the high-level language program.
An interpreter is a program that simultaneously translates and executes the statements written in a high-level language. As the interpreter reads each individual statement in the high-level language program, it translates it into a machine language code and then directly executes it. This process is repeated for every statement in the program.
1.6 What is Source Code?
The statements (often called instructions or commands) that the programmer writes in a high-level language are called source code or simply code. The programmer first types the source code into a program known as a code editor, and then uses either a compiler to translate it into a machine language program, or an interpreter to translate and execute it at the same time.
While it may seem uncommon nowadays, it's entirely possible to write programs using a simple text editor!
1.7 Review Questions: True/False
Choose true or false for each of the following statements.
1)Modern computers can perform so many different tasks because of their ability to be programmed.
2)A computer can operate without a program.
3)A hard disk is an example of hardware.
4)Data can be stored in main memory (RAM) for a long period of time, even if there is no power to the computer.
5)Data is stored in main memory (RAM), but programs are not.
6)Speakers are an example of an output device.
7)Windows and Linux are examples of software.
8)A device driver is an example of hardware.
9)A media player is an example of system software.
10)When you turn on your computer, the main memory (RAM) already contains the operating system.
11)When you open your word processing application, it is actually copied from a secondary storage device to the main memory (RAM).
12)In a machine language, all statements (commands) are a sequence of zeros and ones.
13)Nowadays, a computer cannot understand zeros and ones.
14)Nowadays, software is written in a language composed of ones and zeros.
15)Software refers to the physical components of a computer.
16)The compiler and the interpreter are software.
17)The compiler translates source code to an executable file.
18)The interpreter creates a machine language program.
19)Considering that a program might be executed multiple times, after it has been translated through interpretation and executed once, the need for the interpreter becomes obsolete.
20)Source code can be written using a simple text editor.
21)Source code can be executed by a computer without compilation or interpretation.
22)A program written in machine language requires compilation (translation).
23)A compiler translates a program written in a high-level language.
1.8 Review Questions: Multiple Choice
Select the correct answer for each of the following statements.
1)Which of the following is not computer hardware?
a)a hard disk
b)a DVD disc
c)a sound card
d)the main memory (RAM)
2)Which of the following is not a secondary storage device?
a)a DVD reader/writer device
b)a Solid State Drive (SSD)
c)a USB flash drive
d)RAM
3)Which one of the following operations cannot be performed by the CPU?
a)Transfer data to the main memory (RAM).
b)Transfer data from the main memory (RAM).
c)Perform arithmetic operations.
d)Surgical operations.
4)A touch screen is
a)an input device.
b)an output device.
c)both of the above
5)Which of the following is not software?
a)Windows
b)Linux
c)iOS
d)a video game
e)a web browser
f)All of the above are software.
6)Which of the following statements is correct?
a)Programs are stored on the hard disk.
b)Programs are stored on USB flash drives (USB sticks).
c)Programs are stored in main memory (RAM).
d)All of the above are correct.
7)Which of the following statements is correct?
a)Programs are executed directly from the hard disk.
b)Programs are executed directly from a DVD disc.
c)Programs are executed directly from the main memory (RAM).
d)All of the above are correct.
e)None of the above is correct.
8)Programmers cannot write computer programs in
a)machine language.
b)natural language such as English, Greek, and so on.
c)C#.
9)A compiler translates
a)a program written in machine language into a high-level language program.
b)a program written in a natural language (English, Greek etc.) into a machine language program.
c)a program written in high-level computer language into a machine language program.
d)none of the above
e)all of the above
10)Machine language is
a)a language that machines use to communicate with each other.
b)a language made up of numerical instructions that is used directly by a computer.
c)a language that uses English words for operations.
11)In a program written in high-level computer language, if two identical statements are one after the other, the interpreter
a)translates the first one and executes it, then it translates the second one and executes it.
b)translates the first one, then translates the second one, and then executes them both.
c)translates only the first one (since they are identical) and then executes it twice.