3- Introduction to Programming
3- Introduction to Programming
Introduction to Programming
Why Program?
• Computer – programmable machine designed to follow
instructions
• Program/Software – instructions in computer memory to
make it do something
• Programmer – person who writes instructions (programs)
to make computer perform a task
hardware directly.
High-level: closer to human language
From a High-level Program to an Executable
File
a) Create file containing the program with a text editor.
b) Run preprocessor to convert source file directives to source code
program statements.
c) Run compiler to convert source program statements into machine
instructions.
d) Run linker to connect hardware-specific library code to machine
instructions, producing an executable file.
Steps b) through d) are often performed by a single command or
button click.
Errors occurring at any step will prevent execution of the following
steps.
Compilation and linking
C++ source code
C++ compiler
Object code
Executable program
linker
Library Object code
• You write C++ source code (e.g. hello.cpp file)
• Source code is (in principle) human readable
• The compiler translates what you wrote into object code
(sometimes called machine code) (e.g. hello.obj file)
• Object code is simple enough for a computer to “understand”
• The linker links your code to system code needed to execute
• e.g., input/output libraries
• The result is an executable program
• e.g., hello.exe file
Program Components
Key Words
Programmer-Defined Identifiers
Operators
Punctuation
Syntax
Program Components (Continued)
Example Program
#include <iostream>
using namespace std;
void main()
{
double num1 = 5,
num2, sum;
num2 = 12;
double num1
Program Components (Continued)
• Operators
• Used to perform operations on data
• Many types of operators
• Arithmetic: +, -, *, /
• Assignment: =
• Examples in program (shown in green):
num2 = 12;
sum = num1 + num2;
• Punctuation
• Characters that mark the end of a statement, or that
separate items in a list
• Example in program (shown in green):
double num1 = 5,
num2, sum;
num2 = 12;
Program Components (Continued)
• Lines vs. Statements
In a source file,
• A line is all of the characters entered before a carriage return [Enter].
double num1 = 5,
num2, sum;
Program Components (Continued)
• Variables
• A variable is a named location in computer memory (in RAM)
• It holds a piece of data. The data that it holds may change while
the program is running.
double num1;
Input, Processing, and Output
• Three steps that many programs perform
1) Gather input data
from keyboard
from files on disk drives
2) Process the input data
3) Display the results as output
send it to the screen or a printer
write it to a file