What Is Programming and Program Development Life Cycle ?
What Is Programming and Program Development Life Cycle ?
Cycle ?
• Step 3: Maintenance
– Use and modify the program if the problem domain changes
Algorithm:
– Step-by-step problem-solving process
2
The Problem Analysis–Coding–Execution Cycle
3
The Problem Analysis–Coding–Execution
Cycle (cont'd.)
• Run code through compiler
4
The Problem Analysis–Coding–Execution
Cycle (cont'd.)
• Once compiled and linked, loader can place program into main memory
for execution
• Compiler guarantees that the program follows the rules of the language
– Does not guarantee that the program will run correctly
• Binary code:
– A sequence of 0s and 1s
• Byte:
– A sequence of eight bits
6
The Evolution of Programming Languages
LOAD rate
MULT hour
STOR wages
9
The Evolution of Programming Languages
10
The Problem Analysis–Coding–Execution Cycle
Problem Analysis
Coding
Complier
Translate Code Errors
To Machine
Language
Execution
Errors
Get Results
11
Then
Think Write Code
12
Basic Definitions
• Programming language:
– a set of rules, symbols, and special words used to write computer
programs.
• Computer program
– Sequence of statements whose objective is to accomplish a task.
• Syntax:
– rules that specify which statements (instructions) are legal
13
Computer System
Oper
2
Oper
Input 2
Operation
Output
1
Processing
14
Example 1
• Write a program to find the Area of a rectangle
Input :
Rectangle Length , Rectangle Width.
Processing :
Area = Rect Length * Rect Width.
Output :
Print Out The area.
15
Example 2
• Write a program to find the perimeter and area of a square
The perimeter and area of the square are given by the following formulas:
perimeter = Side Length * 4
area = Side Length * Side Length
Input:
Square Side Length
Processing:
perimeter = Side Length * 4
area = Side Length * Side Length
Output:
Print Out The Perimeter and Area.
16
Your First C++ Program
#include <iostream>
using namespace std;
int main()
{
// This program is written by Mohamed El Desouki
return 0;
}
Sample Run:
My first C++ program.
17
Processing a C++ Program (cont'd.)
• To execute a C++ program:
– Use an editor to create a source program in C++
– Linker:
• Combines object program with other programs provided by the SDK to
create executable code
– Loader:
• Loads executable program into main memory
19
Interacting With User: Displaying Messages on Screen
• #include <iostream> notifies the preprocessor to include the contents of the input/output
stream header file <iostream> in the program
• We can use the Escape Sequence to format the Displayed Text.
20
Interacting With User: Comments
• /*
Mulitple Lines
• */ to comment multiple lines
21
Example 1
• Write a program to find the Area of a rectangle
Input :
Rectangle Length , Rectangle Width.
Processing :
Area = Rect Length * Rect Width.
Output :
Print Out The area.
22
Central Processing Unit and Main Memory
• In C++ , we use Cin >> Variable; To accept an input from the user.
• To use Cin >>, we must use #include <iostream> ;
• #include <iostream> notifies the preprocessor to include in the program the
contents of the input/output stream header file <iostream>.
• A variable is a location in the computer’s memory where a value can be stored for
use by a program.
• All variables must be declared with a name and a data type before they can be
used in a program.
• Declaration
DataType Identifier;
Int width ;
Float salary ; 24
C++ Data Types
Character or small signed: -128 to 127
char 1byte
integer. unsigned: 0 to 255
signed: -2147483648 to 2147483647
int Integer. 4bytes
unsigned: 0 to 4294967295
signed: -32768 to 32767
short int (short) Short Integer. 2bytes
unsigned: 0 to 65535
signed: -2147483648 to 2147483647
long int (long) Long integer. 4bytes
unsigned: 0 to 4294967295
Boolean value. It can
bool take one of two values: 1byte true or false
true or false.
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
Double precision floating
double 8bytes +/- 1.7e +/- 308 (~15 digits)
point number.
Long double precision
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
floating point number.
25
Working With Variable
Cin>>width; area 50
Area = Length * width ;
26
Example 2
• Write a program to find the perimeter and area of a square
The perimeter and area of the square are given by the following formulas:
perimeter = Side Length * 4
area = Side Length * Side Length
Input:
Square Side Length
Processing:
perimeter = Side Length * 4
area = Side Length * Side Length
Output:
Print Out The Perimeter and Area.
27
Declaring & Initializing Variables
• Initialization means to give a variable an initial value.
28
Rules on Variable Names
• DO NOT use reserved words as variable names
(e.g. if, else, int, float, case, for, …).
• The first character has to be a letter or underscore. It can not be a numeric digit.
• The second and the other characters of the name can be any letter, any number, or
an underscore “_”.
Examples
29
Frequently used data types
Data Types Bytes Used • The data type unsigned is used to represent positive
int 4 Bytes integers.
short 2 Bytes •float and double data types for storing real numbers.
double 8 Bytes
The float data type has a precision of seven digits
unsigned 4 Bytes
-This means after the decimal point you can have seven digits
Float 4 Bytes
Double 8 Bytes -Example: 3.14159 534.322344 0.1234567
Char 1 Byte
Example :
-3738.7878787878 3.141592653589790
0.123456789123456
30
Frequently used data types
• We can use Scientific Notation to represent real numbers that are very large or very small in
value.
• The letters e or E is used to represent times 10 to the power.
Example:
• 1.23 x 10 5 is represented as 1.23e5 or 1.23e+5 or 1.23E5
• 1 x 10 -9 is represented as 1e-9
31
Arithmetic Operations
Parentheses are used in C++ expressions in the same manner as in algebraic expressions.
we write a * ( b + c ).
There is no arithmetic operator for exponentiation in C++,
so x2 is represented as x * x.
32
Precedence of arithmetic operations
For example,
2 + 3 * 5 and (2 + 3) * 5
34
Precedence of arithmetic operations
• ? = 1 + 2 * (3 + 4)
35
Data Type of an Arithmetic Expression
• Data type of an expression depends on the type of its operands
– Data type conversion is done by the compiler
36
Data Type of an Arithmetic Expression
Example
int * int; result int
38
Data Type of an Arithmetic Expression
• Only the integer part of the result will be considered if two operands are integer
Even when the target variable is float
39
Type Casting
int main()
The div will be 1.0
{ and this is not write
int i=5, j=3;
float div;
div= i/j;
cout<< div;
Type cast: tells the compiler to treat i
} as a float
• Examples :
++K , K++ k= K+1
--K , K-- K= K-1
41
Increment and Decrement Operators
• If the value produced by ++ or – – is not used in an expression, it does
not matter whether it is a pre or a post increment (or decrement).
• When ++ (or – –) is used before the variable name, the computer first
increments (or decrements) the value of the variable and then uses its
new value to evaluate the expression.
• When ++ (or – –) is used after the variable name, the computer uses the
current value of the variable to evaluate the expression, and then it
increments (or decrements) the value of the variable.
x = 5;
Cout << x++; 42
special assignment statements
• C++ has special assignment statements called compound
assignments
+= , -= , *= , /= , %=
• Example:
X +=5 ; means x = x + 5;
x *=y; means x = x * y;
x /=y; means x = x / y;
43
Control Statements
• Normally, statements in a program are executed one after the other in the order
in which they’re written.
• This is called sequential execution.
• There are control statements enable you to specify that the next statement to
be executed may be other than the next one in sequence.
• This is called transfer of control.
45
Selection Statements : If Statement
• Selection statements are used to choose among alternative courses of action.
• For example, suppose the passing mark on an exam is 60. The pseudocode
statement
– If student’s marks is greater than or equal to 60 Then
Print “Passed”
In C++ , The syntax for the If statement
if ( grade >= 60 )
cout <<"Passed\n“;
47
Relational Expression and Relational Operators
• Relational expression is an expression which compares 2 operands and returns a
TRUE or FALSE answer.
Example : a >= b , a == c , a >= 99 , ‘A’ > ‘a’
• Relational expressions are used to test the conditions in selection, and looping
statements.
Operator Means
== Equal To
!= Not Equal To
< Less Than
<= Less Than or Equal To
48
Selection Statements : If Statement
Example : write a program that accept an integer from the user and in case this
integer is even print out the following message
“This number is even “ .
49
Selection Statements : If .. Else Statement
• The IF…Else selection statement allows you to specify that there is a course of
actions are to be performed when the condition is true and another course of
actions will be executed when the condition is false.