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

What Is Programming and Program Development Life Cycle ?

The document discusses the programming process and program development life cycle, which includes analyzing the problem, designing an algorithm to solve it, implementing the algorithm in code, and maintaining the program if the problem domain changes. It also covers basic programming concepts like data types, variables, input/output, and interacting with the user. The full programming process involves understanding the problem, designing steps to solve it, writing code, compiling, executing, and debugging the program.

Uploaded by

Mìkel Hany
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

What Is Programming and Program Development Life Cycle ?

The document discusses the programming process and program development life cycle, which includes analyzing the problem, designing an algorithm to solve it, implementing the algorithm in code, and maintaining the program if the problem domain changes. It also covers basic programming concepts like data types, variables, input/output, and interacting with the user. The full programming process involves understanding the problem, designing steps to solve it, writing code, compiling, executing, and debugging the program.

Uploaded by

Mìkel Hany
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 49

What Is Programming and Program Development Life

Cycle ?

• Programming is a process of problem solving

• Step 1: Analyze the problem


– Outline the problem and its requirements
– Design steps (algorithm) to solve the problem

• Step 2: Implement the algorithm


– Implement the algorithm in code
– Verify that the algorithm works

• 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

• Understand the Overall problem

• Understand problem requirements


– Does program require user interaction?
– Does program manipulate data?
– What is the output?

• If the problem is complex, divide it into subproblems


– Analyze each subproblem as above

3
The Problem Analysis–Coding–Execution
Cycle (cont'd.)
• Run code through compiler

• If compiler generates errors


– Look at code and remove errors
– Run code again through compiler

• If there are no syntax errors


– Compiler generates equivalent machine code

• Linker links machine code with system resources

4
The Problem Analysis–Coding–Execution
Cycle (cont'd.)
• Once compiled and linked, loader can place program into main memory
for execution

• The final step is to execute the program

• Compiler guarantees that the program follows the rules of the language
– Does not guarantee that the program will run correctly

C++ Programming: From Problem Analysis to Program5 Design, Fifth Edition


The Language of a Computer

• Machine language: language of a computer

• Binary digit (bit):


– The digit 0 or 1

• Binary code:
– A sequence of 0s and 1s

• Byte:
– A sequence of eight bits

6
The Evolution of Programming Languages

• Early computers were programmed in machine language

• To calculate wages = rates * hours in machine language:


100100 010001 //Load
100110 010010 //Multiply
100010 010011 //Store

C++ Programming: From Problem Analysis to Program7 Design, Fifth Edition


The Evolution of Programming Languages
• Assembly language instructions are mnemonic

• Assembler: translates a program written in assembly language into


machine language

C++ Programming: From Problem Analysis to Program8 Design, Fifth Edition


The Evolution of Programming Languages
• Using assembly language instructions, wages = rates • hours can
be written as:

LOAD rate
MULT hour
STOR wages

9
The Evolution of Programming Languages

• High-level languages include Basic, FORTRAN, COBOL, Pascal, C, C++,


C#, and Java

• Compiler: translates a program written in a high-level language machine


language

• The equation wages = rate • hours can be written in C++ as:


wages = rate * hours;

10
The Problem Analysis–Coding–Execution Cycle

Problem Analysis

Using Any Algorithm Design


Programming
Language

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

The area of the Rectangle are given by the following formula:

Area = Rect Length * Rect Width.

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

cout << "My first C++ program." << endl;

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

– Preprocessor directives begin with # and are processed by a the preprocessor

– Use the compiler to:


• Check that the program obeys the rules
• Translate into machine language (object program)

– Linker:
• Combines object program with other programs provided by the SDK to
create executable code

– Loader:
• Loads executable program into main memory

– The last step is to execute the program


18
Processing a C++ Program (cont'd.)

19
Interacting With User: Displaying Messages on Screen

• In C++ , we use Cout << “ Text To be Displayed on The screen “ ;


• To use Cout << , we must use
– #include <iostream> ;
– using namespace std;

• #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

• We can put comment on our programs using


• // to comment a single line
// this progrm is written by mohamed El desouki

• /*
Mulitple Lines
• */ to comment multiple lines

/* This program is written by Mohamed El Desouki


On Monday 11/1/2012
*/

21
Example 1
• Write a program to find the Area of a rectangle

The area of the Rectangle are given by the following formula:

Area = Rect Length * Rect Width.

Input :
Rectangle Length , Rectangle Width.

Processing :
Area = Rect Length * Rect Width.

Output :
Print Out The area.

22
Central Processing Unit and Main Memory

C++ Programming: From Problem Analysis to Program


23Design, Fifth Edition
Interacting With User: Accept Input From User

• 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

Int length ; Length 5


Int width;
Int area;
Width 10
Cin>>Length;

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.

• Variables can be initialized when declared:


int first=13, second=10;
char ch=' ';
double x=12.6;

• All variables must be initialized before they are used in an arithmetic


operatation

– But not necessarily during declaration

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

Some valid names:


my_name, m113_1, salary, bluemoon , _at

Some invalid names:


my name, my-name , 1stmonth , salary! , guns&roses ,

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

•The double data type has a precision of fifteen digits

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.

For example, to multiply a times the quantity b + c

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

both have different meanings


33
Precedence of arithmetic operations
Example :

34
Precedence of arithmetic operations

• ? = 1 + 2 * (3 + 4)

– Evaluated as 1 + (2 * (3+4)) and the result is 15


• ?=5*2+9%4

– Evaluated as (5*2) + (9 % 4) and the result is 11


• ? = 5 * 2 % ( 7 – 4)

– Evaluated as (5 * 2) % (7 – 4) and the result is 1

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

• If operators are *, /, +, or – , then the type of the result will be:


– integer, if all operands are integer.
» Int A , B;
» A+ B  Integer.
– float, If at least one operand is float and there is no double
» Int A ; Float B;
» A + B  Float.
– double, if at least one operand is double
• Int A ; Float B; Double C;
» A + B + C  double.

36
Data Type of an Arithmetic Expression
Example
int * int; result int

int + float; result float

Int + double / float; result double

int – double; result double


Data Type of an Arithmetic Expression
• The data type of the target variable is also important
• If the result is a real number and the target variable is declared as
integer, only the integer part of the result will be kept, and decimal
part will be lost.
The result is calculated as
Example 16.66667

int avg; But avg will be 16

float sum=100.0, cnt = 6.0;


avg = sum / cnt;

38
Data Type of an Arithmetic Expression

The result of the division will be 16


float avg; avg will be 16.0
int sum=100, cnt = 6;
avg = sum / cnt;

• 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

int main() After type casting , The div will be


1.66667
{
int i=5, j=3;
float div;
div=(float) i/j;
cout<< div;
}
40
Increment and Decrement Operators

• Increment operator: increment variable by 1


– Pre-increment: ++variable
– Post-increment: variable++

• Decrement operator: decrement variable by 1


– Pre-decrement: --variable
– Post-decrement: variable --

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

• There is a difference between the following


x = 5;
Cout << ++x;

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.

• The control statements are categorized in almost two groups:


 Selection control statements

 Repetition control statements


SequentialofExecution
Transfer 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 ( Expression) •The Expression can be any valid


action statement ; expression including a relational expression
and even arithmetic expression
if ( Expression)
{ •In case of using arithmetic expressions , a
action statement 1 ;
non-zero value is considered to be true,
action statement 2 ;
. whereas a 0 is considered to be false
.
action statement n ;
}

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

> Greater Than


>= Greater 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.

• For example, the pseudocode statement

– If student’s mark is greater than or equal to 60


Print “Passed”
else
Print “Failed”

You might also like