+ All Categories
Home > Software > General Programming Concept

General Programming Concept

Date post: 06-Aug-2015
Category:
Upload: haris-bin-zahid
View: 23 times
Download: 1 times
Share this document with a friend
25
Agenda •Computer •Program •Human Vs. Computer Instruction •Programming Languages •Interpreter & Compiler •Memory •Recursion •Modularity
Transcript

Agenda •Computer •Program •Human Vs. Computer Instruction •Programming Languages •Interpreter & Compiler •Memory •Recursion •Modularity

Computer

• Is more intelligent than human ??? – Of course Not!

• A slave that only does what it is told • Is always right; can never be wrong • Good at performing lengthy and recurring tasks

Program

•A set of instructions that tells the computer what to do. –Takes input(s) –Processes input(s) –Produces output(s) –Garbage In, Garbage Out (G.I.G.O.)

•Computer executes the program exactly as you have instructed it. It will do no more or no less than what is contained in your specific instructions.

Human Vs. Computer Instructions • Ahmed throws the ball fast Subject Action Object Ahmed Throw Ball Computer Function Throw(ball) { 1. Lift the ball. 2. Move hand in backward direction 3. Move hand in forward direction exerting good amount of force. 4. Leave the ball. }

Computer instructions will be executed how it tells to do.Human instruction execution may change every time you instruct.

Programming Languages

•Low Level Languages –Machine Code

–Assembly Language

•High Level Languages –C#, X++, C etc. int a = 10; int b = 20; int result; result = a + b;

Interpreter and Compiler

•A computer can only understand machine language, therefore we need a program which can translate high level into machine language. •A program is translated into machine language by interpreter or compiler. •Interpreter –Each instruction is interpreted from the programming language as needed (line by line of code). –Every time the program is run, the interpreter must translate each instruction again. –E.g. •PHP, JavaScript, Pearl, Python etc. •Compiler –A compiler makes the translation once so that the source code don’t have to be translated each time the program is run. –Compiler take less time for execution since it translates code once. –E.g. • C#, Java, X++ etc. too.

Memory

• While a program is running, its data must be stored in memory.

• A running program uses two regions of memory to store data.

• –Stack • –Heap • How much memory is required for an item, and where and

how it’s stored, depends on its type. • Language runtime takes care of memory allocation. • For X++ the Microsoft Dynamics AX runtime manages the

storage allocation of data on stack and heap.

Stack

• A stack is an array of memory that acts as a last-in, first-out (LIFO) data structure. • Stack-based memory allocation is very simple and typically faster than heap-

based memory allocation • Data can be added to and deleted only from the top of the stack. • Placing a data item at the top of the stack is called pushing the item onto the

stack. • Deleting an item from the top of the stack is called popping the item from the

stack.

Heap

• Chunks of memory are allocated to store certain kinds of data objects.

• Memory can be stored and removed from the heap in any order.

• Heap is also known as dynamic memory.

Data type

A classification for identifying one of various types of data •A type as a template for creating data structures. •A type is defined by the following elements: –A name. –A data structure to contain its data members. –Behaviors and constraints.

E.g. –Integer •32 bit or 64bit, stores integers only. –Boolean •1 byte, true or false. –Real •64 bit, floating point numbers.

Data type There are two kind of data types: Value type –Stored in stack. –Require only a single segment of memory. –E.g. •Integer •Boolean •Real Reference type –Require two segments of memory –The first contains the actual data—and

is always located in the heap. –The second is a reference that points to

where in the heap the data is stored. –E.g. •String •Object

Variables

The name, or identifier, for a variable points to a memory location where information of a specific data type is stored. •Names must be limited to ASCII letters, digits, and the underscore character (_). •Letters can be either uppercase or lowercase. •The first character must be either a letter or the underscore character. •Best Practice, variable names should begin with a lowercase letter i.e. Camel Casing. –myVariable, custTable

•Why use Variables ? –Variables can maintain the same value throughout the program. –Changing value at one place will reflect throughout the scope.

Variable Declaration Variable always be declared with some data type.

Declaration with initialization

Multiple Declaration –The variables in a multiple-variable declaration must all be of the same type. –The variable names must be separated with commas.

Method

Lines of code that perform a specific task.

–[ Modifiers ] ReturnType MethodName( ParameterList ) { //body }

Public void Test(int i){

}

Modifiers public | protected | private|static

ReturnType Datatype | void

MethodName Identifier

Parameter Datatype Variableidentifier [ = Expression ]

Method Example Return sum of any two integers.

Method Invocation

•Execution of the current method suspends at that point of the invocation. •Control transfers to the beginning of the invoked method. •The invoked method executes until it completes. •Control returns to the calling method.

Method Parameters

• By Value • –It allocates space on the stack for the formal

parameters. • –It copies the values of the actual parameters to

the formal parameters. • By Reference • –It copies the address of variable not the value. • –The actual parameter must be a variable.

By reference & By Value Example

•Local variable –Alive until method returns. •Global variable –Alive until application shutdown. –Company Name •Constants –Once set, the value cannot be changed –Must be used instead of hard-coded values –E.g. Labels file

Variable Scope

Variable Scope Example

Control Statements Conditional •Skip part of code or execute only desired portion –If –If Else –If Else-if Else –Switch Loops •Executes same lines of code once or more than one time –While –Do - While –For

Conditional- if-[else-if] – [else]

if statement evaluates a question (condition). •Executes a statement(s) if condition is true. •Syntax if(expression) { statement } [ elseif(expression) //can have multiple elseif // statements { statement }……… else // only one else statement { statement } ]

Recursion

•Recursion can be used to manage repetition. •Recursion is a process in which a module achieves a repetition of algorithmic steps by calling itself. •Each recursive call is based on a different, generally simpler, instance.

Fundamental Rules Base Case –Always have at least one case that can be solved without recursion. Make Progress – Any recursive call must progress towards a base case. Always Believe – Always assume the recursive call works. Compound Interest Rule – Never duplicate work by solving the same instance of a problem in separate recursive calls.

Recursion Example

Calculating 5! Input n = 5; Factorial (n); 120 •5* a where a = Factorial(n -1)

a = 24 •4* a

a = 6 •3 * a

a = 2 •2* 1


Recommended