Programming Concepts & Logics



Programming Concepts & Logics


Concept programming is a programming paradigm focusing on how concepts, translate into representations that are found in the code space. A programming paradigm is a fundamental style of computer programming. (Compare with a methodology, which is a style of solving specific software engineering problems.) Paradigms differ in the concepts and abstractions used to represent the elements of a program (such as objects, functions, variables, constraints, etc.) and the steps that compose a computation (assignment, evaluation, continuations, data flows, etc.).

Computer program
A computer program (also software, or just a program) is a sequence of instructions written to perform a specified task with a computer. A computer requires programs to function, typically executing the program's instructions in a central processor. The program has an executable form that the computer can use directly to execute the instructions. The same program in its human-readable source code form, from which executable programs are derived (e.g., compiled), enables a programmer to study and develop its algorithms.
Computer source code is often written by computer programmers. Source code is written in a programming language that usually follows one of two main paradigms: imperative or declarative programming. Source code may be converted into an executable file (sometimes called an executable program or a binary) by a compiler and later executed by a central processing unit. Alternatively, computer programs may be executed with the aid of an interpreter, or may be embedded directly into hardware.

Programming language
A programming language is an artificial language designed to communicate instructions to a machine, particularly a computer. Programming languages can be used to create programs that control the behavior of a machine and/or to express algorithms precisely. A programming language is a notation for writing programs, which are specifications of a computation or algorithm.

Types of programming languages
There are several types of languages:
·         Low level programming language
·         High level programming language
·         4 GL

Low-level programming language
Low-level languages can be converted to machine code without using a compiler or interpreter, and the resulting code runs directly on the processor. A program written in a low-level language can be made to run very fast, and with a very small memory footprint; an equivalent program in a high-level language will be more heavyweight. Low-level languages are simple, but are considered difficult to use, due to the numerous technical details which must be remembered.
Examples of Low-level programming language are: Machine Code and Assembly language
Machine code
Machine code is the only language a microprocessor can process directly without a previous transformation. Currently, programmers almost never write programs directly in machine code, because it requires attention to numerous details which a high-level language would handle automatically, and also requires memorizing or looking up numerical codes for every instruction that is used. For this reason, second generation programming languages provide one abstraction level on top of the machine code. Machine code may be regarded as a primitive (and cumbersome) programming language or as the lowest-level representation of a compiled and/or assembled computer program. While it is possible to write programs in machine code, because of the tedious difficulty in managing CPU resources, it is rarely done any more, except for situations that require the most extreme optimization.
Assembly
An assembly language is a low-level programming language for computers, microprocessors, microcontrollers, and other programmable devices. It implements a symbolic representation of the machine codes and other constants needed to program a given CPU architecture. This representation is usually defined by the hardware manufacturer, and is based on mnemonics that symbolize processing steps (instructions), processor registers, memory locations, and other language features. An assembly language is thus specific to certain physical (or virtual) computer architecture. This is in contrast to most high-level programming languages, which, ideally, are portable.
Assembly language is considered a low-level language because while it is not a microprocessor's native language, an assembly language programmer must still understand the microprocessor's unique architecture (such as its registers and instructions). These simple instructions are then assembled directly into machine code. The assembly code can also be abstracted to another layer in a similar manner as machine code is abstracted into assembly code.

High level programming language
A high-level programming language is a programming language with strong abstraction from the details of the computer. In comparison to low-level programming languages, it may use natural language elements, be easier to use, or be from the specification of the program, making the process of developing a program simpler and more understandable with respect to a low-level language. "High-level language" refers to the higher level of abstraction from machine language. Rather than dealing with registers, memory addresses and call stacks, high-level languages deal with variables, arrays, objects, complex arithmetic or boolean expressions, subroutines and functions, loops, threads, locks, and other abstract computer science concepts, with a focus on usability over optimal program efficiency. Unlike low-level assembly languages, high-level languages have few, if any, language elements that translate directly into a machine's native opcodes. Other features, such as string handling routines, object-oriented language features, and file input/output, may also be present.
Examples of high level programming languages are: C,C++,C#, LISP, PROLOG etc


High level programming Execution models
There are three models of execution for modern high-level languages:
Interpreted 
Interpreted languages are read and then executed directly, with no compilation stage. A program called an interpreter reads each program line following the program flow, converts it to machine code, and executes it; the machine code is then discarded, to be interpreted anew if the line is executed again.
Compiled 
Compiled languages are transformed into an executable form before running. There are two types of compilation:
Machine code generation 
Some compilers compile source code directly into machine code. This is the original mode of compilation, and languages that are directly and completely transformed to machine-native code in this way may be called "truly compiled" languages.
Intermediate representations 
When a language is compiled to an intermediate representation, that representation can be optimized or saved for later execution without the need to re-read the source file. When the intermediate representation is saved, it is often represented as byte code. The intermediate representation must then be interpreted or further compiled to execute it. Virtual machines that execute byte code directly or transform it further into machine code have blurred the once clear distinction between intermediate representations and truly compiled languages.
Translated 
A language may be translated into a lower-level programming language for which native code compilers are already widely available. The C programming language is a common target for such translators.

Fourth-generation programming language
A fourth-generation programming language (1970s-1990) (abbreviated 4GL) is a programming language or programming environment designed with a specific purpose in mind, such as the development of commercial business software. All 4GLs are designed to reduce programming effort, the time it takes to develop software, and the cost of software development. The 4GL is an example of 'black box' processing; each is further from the machine. It is this latter nature that is directly associated with 4GL having errors that are harder, in many cases, to debug. In terms of applications, a 4GL could be business oriented or it could deal with some technical domain. Being further from the machine implies being closer to domain.
Examples of high level programming languages are: SQL, Clipper, FoxPro, Informix-4GL etc


Concept of Programming Statement
In computer programming a statement can be thought of as the smallest standalone element of an imperative programming language. A program written in such a language is formed by a sequence of one or more statements. A statement will have internal components (e.g., expressions). Many languages make a distinction between statements and definitions, with a statement only containing executable code and a definition declaring an identifier. A distinction can also be made between simple and compound statements; the latter may contain statements as components.

Kinds of statements
Simple statements
Int a,b,c;
a=20;
b=30;
c=a+b;
printf(“%d”,c);


Compound statements
For(int a=1;a<=5;a++)
{
Statement…
}
Syntax
Programming languages are characterized by the flavor of statements they use (e.g.: The curly brace language family). Many statements are introduced by identifiers like if, while or repeat. Often statement keywords are reserved such that they cannot be used as names of variables or functions. Imperative languages typically use special syntax for each statement, which looks quite different to function calls.
For example:
For counter syntax:
For(initial value;condition;counter)
{
Statement ….
}

Semantics
Semantically many statements differ from subroutine calls by their handling of parameters. Usually an actual subroutine parameter is evaluated once before the subroutine is called. This contrasts to many statement parameters which can be evaluated several times (e.g. the condition of a while loop) or not at all (e.g. the loop body of a while loop). Technically such statement parameters are closures which are executed when needed. When closure parameters are available for subroutines a statement like behavior can be implemented by subroutines also. For languages without closure parameters the semantic description of a loop or conditional is usually beyond the capabilities of the language. Therefore standard documents often refer to semantic descriptions in natural language.
Semantics is the study of meaning. It focuses on the relation between signifiers, such as words, phrases, signs and symbols, and what they stand for.
For example:
x += y (C, C++, C#, Java, Perl, Python, Ruby, PHP, etc.)
LET X = X + Y   (BASIC)

Semantics errors
In computer programming, a Semantics errors
 Or logic error is a bug in a program that causes it to operate incorrectly, but not to terminate abnormally (or crash). A logic error produces unintended or undesired output or other behavior, although it may not immediately be recognized as such. Logic errors occur in both compiled and interpreted languages. Unlike a program with a syntax error, a program with a logic error is a valid program in the language, though it does not behave as intended. The only clue to the existence of logic errors is the production of wrong solutions.
Common causes: The mistake could be the logical error in a statement (for example, a wrong or incorrect formula or divided by zero etc.), an error in an algorithm, or even the wrong algorithm selected.

Program Control Structures: Sequence, Selection and Iteration.
Programming languages that support complex logic (i.e. like logic decisions) will implement sequence, decision and repetition structures. These structures will provide the programming flexibility necessary for computer programs to solve complex problems. Control structures enable the grouping of processing steps, the implementation of decisions and the use of repetition in our logic and then our programs.
Three control structures:
             (1)        Sequence
            (2)        Selection structures
            (3)        Repetition (a.k.a. iteration or looping)

Sequence structures
Sequence structures are the simplest of control structures to implement and the closest in format. Sequence structures have one input and one output within the sequence structure and typically consist of instruction(s) (i.e. a calculation or maybe an assignment). The result is passed on to the next structure. Sequence structures appear as rectangles in flowcharts and statements in pseudo code.
Pseudo code example:
 total = sales times *salesTax
 
Figure 2: Sequence structure flowchart

Selection structures
Selection structures also known as Decision structures; provide our program logic with the ability to test a Boolean expression that will return a Boolean value of true or false. The test constitutes a decision in our logic. The Boolean expression typically consists of two identifiers, literals or other expressions joined by a logical operator. It is important to understand that Boolean expressions can only produce a solution with two alternatives. By nesting (adding new decision structures within the true and false branches), even complex decisions can be implemented in a program. Decision structures appear as diamond shaped symbols in flowcharts and are implemented in pseudo code as an if statement. The flowchart diamond symbol (which also identifies the boolean expression) will have a true line and optionally a false line drawn from the points on the diamond. Pseudo code will include statements indented under the’ if’ statement for true Boolean expressions and under the else statement for a false Boolean expression.
Pseudo code for .10 percent book discount with purchase of more than 3 books:
 if (books > 3 then
  totSales = sales * .90
 else
  totSales = sales
Flow Chart for .10 percent book discount with purchase of more than 3 books:

Figure 3: Decision structure flowchart
Iteration structures
Iteration structures, also known as looping or Repetition structures, provide our program logic with ability to repeat a block of statements based on the evaluation of a Boolean expression. This structure allows us to take a group of several statements and repeat their execution based on some decision or condition. The repetition could be as simple as iteration that allows us to repeat through a block of code a certain number of times or can be as complex as repetition based on a Boolean comparison (i.e. when sales > 1000). Like the decision structure, the repetition evaluates a Boolean expression to control reentry into the loop. Repetition structures appear as a diamond with a line returning above the diamond to facilitate repetition and another line showing the flow of the logic when the loop concludes. In pseudo code and program code, the repetition structure is usually implemented with a while loop and has the statements included in the repetition indented under the while loop.
 Pseudo code to repeat a sequence statement 10 times:
 while cnt < 10
total = cnt * qty
Flow Chart for to repeat a sequence statement 10 times:



Figure 4: Repetition structure flowchart
Program Design tools – Pseudo code, Algorithm and Flowchart
Software design is a process of problem solving and planning for a software solution. After the purpose and specifications of software are determined, software developers will design or employ designers to develop a plan for a solution.
The most common ways to design the logic of a program is to use pseudo code, Algorithm and Flowchart.
Pseudo code
Pseudocode is a way for computer programmers to create high level descriptions of algorithms that they would like to figure out on paper before programming the actual code itself. It doesn't strictly follow the programming structures of any single language. In fact, it's usually done in a very ad hoc fashion in a way that allows programmers to sketch out their ideas informally so that they can wrap their minds around a particular programming problem.
There is no standard syntax for pseudocode, and writing in pseudocode will not yield an executable program. Pseudocode can be considered to be simplified programming code that can apply to multiple languages. It often includes descriptions of programming actions written in plain English or in terms that are very close to plain English in nature. A piece of pseudocode for an algorithm that logs a user into some online system, for example, might look something like this:

example1:
pseudocode: If then Else
If age > 17
  Display a message indicating you can vote.
Else
  Display a message indicating you can't vote.
Endif

Example 2:
pseudocode: Case
Case of age
  0 to 17   Display "You can't vote."
  18 to 64  Display "Your in your working years."
  65 +      Display "You should be retired."
Endcase
Example 3:
pseudocode: While
count assigned zero
While count < 5
  Display "I love computers!"
  Increment count
Endwhile
Example 4:
pseudocode: For
For x starts at 0, x < 5, increment x
  Display "Are we having fun?"
Endfor
Example 5:
pseudocode: Do While
count assigned five
Do
  Display "Blast off is soon!"
  Decrement count
While count > zero
Example 6:
pseudocode: Repeat Until
count assigned five
Repeat
  Display "Blast off is soon!"
  Decrement count
Until count < one

Algorithm
Algorithm is a procedure for solving a problem. It is basically the description of steps for computation of a problem. This is also referred as a step by step procedure to solve a problem. It abstracts the data, operations and the semantics of a problem and then creates a solution using all those abstractions. It describes the process of solving a particular problem. A computer program can also be viewed as an elaborate algorithm.
The main points about algorithms are:
What are the inputs for that particular algorithm?
What will be the output with respect to the given inputs?
How much time will be required for that algorithm to solve the problem (i.e. the efficiency of that algorithm)?

This means that by giving an input data, a particular algorithm is expected to give the output within a particular time which is counted in time units and speed. It can be implemented in any of the languages (may be C, C++, Java or ASP.net etc…). Algorithm is often rendered in pseudo code. Algorithms are slightly different to pseudo code. An Algorithm is much more descriptive and does not contain mathematical symbols. Pseudo code is the next step in the software development cycle. The algorithms are then translated into descriptions which are pretty much closer to the actual programming language.
Algorithms play a very important role in designing good software and also help in finding best solution to a problem. Algorithm can be formally written down and has very practical consequences to achieve software re-use which helps in saving a lot of time for software developers. There are a number of formalizations of algorithms. E.g. is Turing machine. Using the Turing machine, an algorithm is a description of a states and steps required to compute something.
The computers are increasing in complexities and therefore more and more software algorithms are being used these days. The algorithms have taken a form which is also called hard software. The algorithms have become a basic part in computer’s circuitry and can also stand alone in special devices. A number of different applications algorithms are available which are highly advanced systems. For example – Algorithms such as Artificial intelligence algorithms will be becoming very common in near future. Thus helping day to day needs not only in the field of computers but in other fields as well.
Algorithm for finding factorial of any number:
1. Take a number as a input from the user.
2. Initialize a variable fact=1

3. fact=fact*number
4. number=number-1
5. repeat step 3 to 5 if number>0
6. print the value of fact variable
Algorithm  illustrate in c programming language:
void main()
{
int x;
int factorial=1;
printf("enter number :");
scanf(“%d”,&x);
for (int i=x; i > 0;i--)
{
factorial =factorial * i;
}
printf(" facotorial of x  is: “,factorial);
}

Flowchart
Flowcharting is a tool developed in the computer industry, for showing the steps involved in a process. A flowchart is a diagram made up of boxes, diamonds and other shapes, connected by arrows - each shape represents a step in the process, and the arrows show the order in which they occur. Flowcharting combines symbols and flow lines, to show figuratively the operation of an algorithm.
Flowcharting Symbols
There are 6 basic symbols commonly used in flowcharting of assembly language programs: Terminal, Process, input/output, Decision, Connector and Predefined Process. This is not a complete list of all the possible flowcharting symbols, it is the ones used most often in the structure of Assembly language programming.







Flowchart for finding factorial of any number:


Flowchart  illustrate in c programming language:
void main()
{
int x;
int factorial=1;
printf("enter number :");
scanf(“%d”,&x);
for (int i=x; i > 0;i--)
{
factorial =factorial * i;
}
printf(" facotorial of x  is: “,factorial);
}



Introduction to Data Type

Although some contemporary programming languages allow programmers to invent own data types, and define their related operations, there are a number of traditional data types found in most programming languages:
Integer
Integers are numeric data items, which are either positive or negative including zero, i.e. 1, 488, -22, 0, 456. Some programming languages put restrictions on the magnitude of integers which may be used in program instructions. These restrictions are usually dependent on the size of the memory location of the computer in which the language may run.
Real Numbers
There are two types of real numbers, Fixed-Point and Floating Point.
Fixed Point
Fixed point data items are numbers which have embedded decimal point i.e. 1.5, 458.4589, -0.569.
Floating Point
Floating point data items are numbers, which are, held as binary fractions by a computer. The numbers are expressed in a form where you have a mantissa and an exponent, for example
Number                                   Mantissa          Exponent
12.3 = 0.123 * 102                        0.123               2
123000 =0.123 * 106                    0.123               6
0.000123 =0.123 * 10-3              0.123               -3
Floating point representation of data is used to overcome the restrictions placed on the magnitude of numbers by the size of computer’s memory locations.
Character
Character data, sometimes referred to as “string” data, may consist of any digits, letters of the alphabet or symbols which, the internal coding system of the computer is capable of representing. Many programming languages require character data to be enclosed by quotation marks when used in program instructions, for example PRINT “HAPPY NEW YEAR”.
Boolean
Boolean data items are used as status indicators and may contain only one of two possible values: True or False.


1 comment:

Thanks for your great comment!