Principles of Programming
Principles of Programming
PRINCIPLES OF
PROGRAMMING
Implementation phase
implement the program in some programming
language
Steps in Problem Solving
Pseudo code:
Detailed Algorithm:
Flowchart Characteristics:
1. shows the sequence of instructions in a single
program or subroutine
2. shows logic of an algorithm from start to finish
3. emphasizes individual steps and their
interconnections
4. control flow from one action to the next
5. Different symbols are used to draw each type of
flowchart
6. The start symbol indicates the beginning of a
program
7. The end symbol indicate the end of a program.
Flowchart Symbols
The Flowchart
START
Algorithm:
Input
M1,M2,M3,M4
Step 1: Input M1,M2,M3,M4
Step 2: GRADE (M1+M2+M3+M4)/4
GRADE(M1+M2+M3+M4)/4 Step 3: if (GRADE <50) then
Print “FAIL”
else
N Y
IS Print “PASS”
GRADE<50
endif
PRINT PRINT
“PASS” “FAIL”
STOP
Example 2
Pseudo code:
1. Input the length in feet (Lft)
2. Calculate the length in cm (Lcm) by multiplying LFT
with 30
3. Print length in cm (LCM)
Flowchart
Algorithm
START
Step 1: Input Lft
Step 2: Lcm Lft x 30 Input
Lft
Step 3: Print Lcm
Lcm Lft x 30
Print
Lcm
STOP
Example 3
Pseudocode
1. Input the width (W) and Length (L) of
a rectangle
2. Calculate the area (A) by multiplying
L with W
3. Print A
START
Algorithm
Step 1: Input W,L Input
W, L
Step 2: A L x W
Step 3: Print A
ALxW
Print
A
STOP
Example 4
Algorithm:
Input
Step 1: Input a, b, c a, b, c
Step 2: d sqrt ( b b − 4 a c )
d sqrt(b x b – 4 x a x c)
Step 3: x1 (–b + d) / (2 x a)
Step 4: x2 (–b – d) / (2 x a) x1 (–b + d) / (2 x a)
Print
x1 ,x2
STOP
Terminologies
Y N
is
A>B
Print A Print B
IF–THEN–ELSE STRUCTURE
If condition then
true alternative
else
false alternative
endif
IF–THEN–ELSE STRUCTURE
If A>B then
print A Y N
is
else A>B
print B
endif Print A Print B
Relational Operators
Relational Operators
Operator Description
> Greater than
< Less than
= Equal to
Greater than or equal to
Less than or equal to
Not equal to
Example 5
Input
VALUE1,VALUE2
Y is
N
VALUE1>VALUE2
Print
“The largest value is”, MAX
STOP
NESTED IFS
input output
Computer Program
Programming Language Concepts
LOAD r1,b
LOAD r2,h
Low-level program MUL r1,r2
DIV r1,#2
RET
Machine language
Operation code – such as addition or subtraction.
Operands – that identify the data to be processed.
Machine language is machine dependent as it is the only language the
computer can understand.
Very efficient code but very difficult to write.
Second Generation
Languages
Assembly languages
Symbolic operation codes replaced binary operation codes.
Assembly language programs needed to be “assembled” for execution
by the computer. Each assembly language instruction is translated into
one machine language instruction.
Very efficient code and easier to write.
Third Generation Languages
Declarative languages
Functional(?): Lisp, Scheme, SML
Also called applicative
Everything is a function
Logic: Prolog
Based on mathematical logic
Rule- or Constraint-based
The principal paradigms
Object-oriented languages
Objects are created rather than sequences of instructions
Some third generation, and fourth and fifth generation
languages
Traditional Programming
Languages
FORTRAN
FORmula TRANslation.
Developed at IBM in the mid-1950s.
Designed for scientific and mathematical applications
by scientists and engineers.
Traditional Programming
Languages (cont’d.)
COBOL
COmmon Business Oriented Language.
Developed in 1959.
Designed to be common to many different computers.
Typically used for business applications.
Traditional Programming
Languages (cont’d.)
BASIC
Beginner’s All-purpose Symbolic Instruction Code.
Developed at Dartmouth College in mid 1960s.
Developed as a simple language for students to write
programs with which they could interact through
terminals.
Traditional Programming
Languages (cont’d.)
C
Developed by Bell Laboratories in the early 1970s.
Provides control and efficiency of assembly language
while having third generation language features.
Often used for system programs.
UNIX is written in C.
Object-Oriented Programming
Languages
Simula
First object-oriented language
Developed by Ole Johan Dahl in the 1960s.
Smalltalk
First purely object-oriented language.
Developed by Xerox in mid-1970s.
Still in use on some computers.
Object-Oriented Programming
Languages (cont’d.)
C++
It is C language with additional features.
Widely used for developing system and application
software.
Graphical user interfaces can be developed easily with
visual programming tools.
Object-Oriented Programming
Languages (cont’d.)
JAVA
An object-oriented language similar to C++ that
eliminates lots of C++’s problematic features
Allows a web page developer to create programs for
applications, called applets that can be used through
a browser.
Objective of JAVA developers is that it be machine,
platform and operating system independent.
Special Programming Languages
Scripting Languages
JavaScript and VBScript
Php and ASP
Perl and Python
Command Languages
sh, csh, bash
Text processing Languages
LaTex, PostScript
Special Programming
Languages
(cont’d.)
HTML
HyperText Markup Language.
Used on the Internet and the World Wide Web (WWW).
Web page developer puts brief codes called tags in
the page to indicate how the page should be
formatted.
Special Programming Languages
(cont’d.)
XML
Extensible Markup Language.
A language for defining other languages.
A language is a language is a
language
Outlining the basic working of the interpreter the above figure shows that first a
source code is converted to an intermediate form and then that is executed by
the interpreter.
Interpreter Code Example
COMPILER vs INTERPRETER
Variables are named memory locations that are used for storing data
items of a value of a particular data type or a reference to an object.
➢ At the local evlevel inside a method or block of code , they are called
local variables
➢ At the object instance lel when defined as a Non-static attribute
, also called instance variables
➢ At the class level when defined as a static attribute , also called
class variables
➢Variables in method declarations—these are called parameters.
Data Types
➢ Value Types (Built in data types – Primitive data types Like: int ,
float , ..)
int x =5;
ref
Memory
Primitive Data Types
1. Numeric values
✓ integer (byte, short, int, and long)
✓ floating-point (float and double)
Variable Declaration
▪ To declare a variable, you use the syntax :
datatype varableName;
Initializing Variables
You can assign values to variables in your program during variable
declaration.
Uninitialized Variables
Attributes (instance or static) are always initialized to a default value
local variable must be initialized otherwise the Program does not compile
Integer Data Types
➢ Double data type is generally used as the default data type for
decimal values.
➢ Double data type should never be used for precise values such as
currency.
Character Type
✓ This data type is used for simple flags that track true/false
conditions.
1 + 2 * 3 // evaluated to int 7
int sum, number; sum + number // evaluated to an int value
// Evaluated to a double value
double principal, interestRate; principal * (1 + interestRate)
Operators
✓ Arithmetic Operators
✓ Assignment Operators
✓ Relational Operators
✓ Logical Operators
✓ Increment and Decrement Operators
✓ Bitwise Operators
✓ Operator Precedence
Arithmetic Operators
Operator Meaning
& logical AND
&& conditional AND
| logical OR
|| Conditional OR
^ exclusive OR (XOR)
! logical (NOT)
Logical Operators - The truth tables
Increment and Decrement Operators
Operator Meaning
++ Increment operator; increase the value of
a numeric variable or array element by 1
-- Decrement operator; reduce the value of
a numeric variable or array element by 1
Operator Description
Binary bitwise AND (&) Returns a 1 if both bits are 1
Binary bitwise OR (|) Returns a 1 if either bit is 1
Binary bitwise XOR (^) Returns a 1 if both bits have different values
Unary bitwise complement (~) Changes each bit in its operand to the
opposite value
Binary left shift (<<) Shifts bits on the left to the left by a distance
denoted by the right operand .Fills in zeros
Binary right shift (>>) Shifts bits on the left to the right by a distance
denoted by the right operand .Fills in the
highest bit on the left side.
Binary arithmetic and logical shift Shifts bits on the left to the right by a distance
(>>>) denoted by the right operand .Fills in zeros
Operator Precedence
precedence Operator
1 (..) [..] . (dot operator)
2 ++ -- ! ~ instanceof
3 new (type) expression
4 * / %
5 + -
6 << >> >>>
7 < > <= >=
8 = !=
9 &
10 ^
11 |
12 &&
13 ||
Functions
● Define functions
● Passing arguments to Function
● Return a value from function
● Scope of Objects
● Default arguments
● Positional and keyword arguments
● Variable length arguments
Functions
Calling the
Function
my_function()
Square function: Take one
arguments and prints its square
Function returning multiple value
Scope and Lifetime of variables