0% found this document useful (0 votes)
11 views

3- Introduction to Programming

Programming
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

3- Introduction to Programming

Programming
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

C++ 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

• SO, without programmers, no programs; without


programs, the computer cannot do anything.
Programs and Programming
Languages
 Program
A set of instructions directing a computer to perform a
task.
 Programming Language
A language used to write programs.
 Algorithm
A set of steps to perform a task or to solve a problem
Order is important. Steps must be performed sequentially.
 Types of languages
Low-level: used for communication with computer

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

 Common elements in programming languages:

 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;

sum = num1 + num2;


cout << "The sum is " << sum;
}
Program Components (Continued)
 Key Words
 Also known as reserved words

 Have a special meaning in C++


 Can not be used for another purpose

 Written using lowercase letters

 Examples in program (shown in green):

using namespace std;


int main()
 Programmer-Defined Identifiers
 Names made up by the programmer

 Not part of the C++ language

 Used to represent various things, such as


variables (memory locations)
 Example in program (shown in green):

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].

• Blank lines improve the readability of a program.

Here are four sample lines. Line 3 is blank:

1. double num1 = 5, num2, sum;


2. num2 = 12;
3.
4. sum = num1 + num2;
Program Components (Continued)
In a source file,
• A statement is an instruction to the computer to perform an
action.
• A statement may contain keywords, operators, programmer-
defined identifiers, and punctuation.
• A statement may fit on one line, or it may occupy multiple lines.

Here is a single statement that uses two lines:

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.

• The name of the variable should reflect its purpose

• It must be declared before it can be used.

• Variable declarations indicate the variable name and the type of


data that it can hold.

• Example variable declaration:

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

Input Processing Output


The Programming Process
1. Define what the program is to do.
2. Use design tools to create a model of the program.
Hierarchy charts, flowcharts, pseudo code, etc.
3. Check the model for logical errors.
4. Write the program source code.
5. Compile the source code.
6. Correct any errors found during compilation.
7. Link the program to create an executable file.
8. Run the program using test data for input.
9. Correct any errors found while running the program.
Repeat steps 4 - 10 as many times as necessary.
11. Validate the results of the program.
Does the program do what was defined in step 1?
Kinds of Errors
• Compile-time errors: any type of error that prevents the
program from successfully compiling.
• Syntax errors: violations of the C++ syntax rules
• Type errors: misuse of data types
• Run-time errors: errors which occurs while the program is
running.
• Examples: attempting to divide by zero, or causing an
overflow by multiplying two large numbers together, or
accessing array elements that are beyond the range of
subscripts.
• Logic errors: a logic error is a bug in a program that causes
it to produce unintended or undesired output or other
behavior.

You might also like