Fundamental of Strucutred
Programming
1
Fundamental of Structure
Programming Course
Section 2
Introduction to C++
2
THE PROGRAMMING
PROCESS
3
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
- solve the problem.
3) Display the results as output
- send it to the screen or a printer.
- write it to a file.
4
The Programming Process
The programming process consists of several steps, which
include design, creation, testing, and debugging activities.
The problem must classified into:
• Purpose
• Input
• Process
• Output
5
The Programming Process
1. Define what the program is to do.
2. Visualize the program running on the computer.
3. Use design tools to create a model of the program.
(Hierarchy charts, flowcharts, pseudocode, etc).
4. Check the model for logical errors.
5. Write the program source code.
6. Compile the source code.
6
The Programming Process
(cont).
7. Correct any errors found during compilation.
8. Link the program to create an executable file.
9. Run the program using test data for input.
10. 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
7
The Parts of a C++ Program
//sample C++ program comment
#include <iostream> preprocessor directive
using namespace std; which namespace to use
int main)( beginning of function named main
{ beginning of block for main
cout << "Hello, there!"; output statement
return 0; send 0 back to operating system
} end of block for main
12
The Parts of a C++
Program
Statement Purpose
//sample C++ program comment
#include <iostream> preprocessor directive
using namespace std; which namespace to use
int main)( beginning of function named main
{ beginning of block for main
cout << "Hello, there“; output statement
return 0; send 0 back to the operating system
} end of block for main
13
Special Characters
Character Name Description
// Double Slash Begins a comment
# Pound Sign Begins preprocessor
directive
> < Open, Close Brackets Encloses filename used in
#include directive
) ( Open, Close Used when naming
Parentheses function
} { Open, Close Braces Encloses a group of
statements
" " Open, Close Quote Encloses string of
Marks characters
; Semicolon Ends a programming
statement
14
C++ Example Program
#include <iostream>
using namespace std;
int main)(
{
double num1 = 5,
num2, sum;
num2 = 12;
sum = num1+ num2
cout <<"The sum is " << sum;
return 0;
}
15
WHAT IS A PROGRAM
MADE OF?
16
What Is a Program Made Of?
There are certain elements that are common to all
programming languages.
Common elements in programming languages:
• Key Words
• Programmer-Defined Identifiers
• Operators
• Punctuation
• Syntax
17
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)(
18
C++ Key Words
19
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.)
Have specific rules.
Example in program (shown in green:)
double num1;
20
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 defined before it can be used. Variable
definitions indicate the variable name and the type
of data that it can hold.
Example variable definition:
double num1; double num2, sum;
21
C++ Example Program
#include <iostream>
using namespace std;
int main)(
{
double num1 = 5,
num2, sum;
num2 = 12;
sum = num1 + num2;
cout << "The sum is " << sum;
return 0;
}
22
Variables, Literals, and the
Assignment Statement
Variable
– Has a name and a type of data it can hold
variable
data type char letter; name
– Is used to reference a location in memory where a
value can be stored.
– Must be defined before it can be used.
– The value that is stored can be changed, i.e., it can
“vary.”
23
Variables
– If a new value is stored in the variable, it replaces
the previous value.
– The previous value is overwritten and can no
longer be retrieved.
int Age;
age =17; // age is 17
cout << age; //Displays 17
age = 18; //Now age is 18
cout<< age; //Displays 18
24
Assignment
Statement
Uses the = operator.
Has a single variable on the left side and a
value on the right side.
Copies the value on the right into the variable
on the left.
Example
item = 12;
25
Constant
s Literal
Literal
– Data item whose value does not change
during program execution.
– Is also called a constant
'A' //character constant
"Hello" //string literal
12 //integer constant
3.14 //floating-point constant
26
Identifiers
Programmer-chosen names to represent parts of the
program, such as variables
Name should indicate the use of the identifier
Cannot use C++ key words as identifiers
Must begin with alphabetic character or _, followed
by alphabetic, numeric, or _ . Alphabetic characters
may be upper- or lowercase
27
Multi-word Variable Names
Descriptive variable names may include multiple words
Two conventions to use in naming variables:
– Capitalize all but first letter of first word. Run words
together:
quantityOnOrder
totalSales
– Use the underscore _ character as a space:
quantity_on_order
total_sales
Use one convention consistently throughout program
28
Valid and Invalid Identifiers
IDENTIFIER VALID? REASON IF INVALID
totalSales Yes -----
total_Sales Yes -----
total.Sales No Cannot contain“ . “
total Sales No Cannot contain space
4thtotalSales No Cannot begin with digit
totalSale$ No Cannot contain“ $ “
29
72