You are on page 1of 51

CHAPTER - 5

Programming
Concepts and Logic
Program

• The set of instructions that instruct the computer to perform a


particular operation or a task is called a program.

• Instruction is also called a statement. Generally, a statement


is an instructions to do only one task.

• A computer program is written in a specific programming


language. © padandaas.com © 2
Programming language

• A programming language is a set of rules that provides a way of


instructing the computer to perform certain operations.

• Programming languages are used by programmer to write a


computer program.

• The process of writing a program following the grammar of the


programming language is called programming.
© padandaas.com © 3
Types of Programming language

© padandaas.com © 4
Low level language

• Programming language that is close to the machine architecture.

• To write the program in low level language we should know about the
detailed internal architecture of computer and its instruction set.

• It is machine dependent programming language.

• Types
1. Machine language
2. Assembly language
© padandaas.com © 5
1. Machine Language (1GL)
• Machine language is the fundamental language of computer.
• It is written as string of binary (1s and 0s) and directly understood by
computer.
• Each family of processor have its own machine language.
• An instruction written in any machine language has two-part format.
• Instruction Format
OPCODE OPERAND
• OPCODE - tells the computer what function to perform.
• OPERAND - tells the computer where to find or store the data that
are to be manipulated. © padandaas.com © 6
1. Machine Language (1GL)
Merits
• It is directly understood by the processor so has faster execution time.
• The programs written in this language need not be translated.
• It doesn’t need a larger memory or Processing power.

Demerits
• It is very difficult to program using 1GL since all the instructions are
to be represented by 0s and 1s.
• Machine-level language is time-consuming to program.
• It is difficult to find errors and debug them.
• It is machine-dependent. Hence, the program made for one computer
may not work for others. © padandaas.com © 7
2. Assembly Language (2GL)
• Assembly language is also known as low-level language because to
design a program, a programmer requires detailed knowledge of
hardware specification.
• Language which allows instructions represented by symbolic notation
is called an assembly language.
• Instruction in Assembly language are represented as symbolic codes
called mnemonics. E.g. ADD, SUB, HLT, MVI, MOV etc.
• The program is converted into machine code by an assembler. The
resulting program is referred to as an object code.

© padandaas.com © 8
2. Assembly Language (2GL)
Merits
• It is makes programming easier than 1GL since it uses
mnemonics code for programming.
e.g. : ADD for addition, SUB for subtraction, DIV for division, etc.
• It makes the programming process faster.
• An error can be identified much easily compared to 1GL.
• It is easier to debug than machine language.

© padandaas.com © 9
2. Assembly Language (2GL)
Demerits
• Programs written in this language are not directly
understandable by computer so translators should be used.
• It is a hardware-dependent language so programmers are
forced to think in terms of the computer’s architecture rather
than to the problem being solved.
• Being a machine-dependent language, programs written in
this language are very less or not portable.
• Programmers must know its mnemonics codes to perform
any task.
© padandaas.com © 10
High level language
• Instructions of this language closely resemble human language or
English-like words. It uses mathematical notations to perform the task.
• A high-level language is easier to learn. It requires less time to write
and is easier to maintain the errors.
• Machine independent
• No need of knowledge of internal structure of computer.
• The high-level language is converted into machine language by one of
the two different languages translator programs: interpreter or
compiler.
• Types
3. Procedural-Oriented language (3GL)
4. Problem-Oriented language (4GL)
5. Natural Language (5GL)
© padandaas.com © 11
3. Procedural-Oriented language (3GL)

• General purpose programming language


• For solving the problem it require specification of how to
solve. (step by step procedure)
• A program in 3GL are list of instructions.
• These languages are designed to express the logic and the
procedure of a problem to be solved.
• Program are divided in functions or subroutine.
• E.g. COBOL, FORTRAN, C, JAVA, PHP etc.
© padandaas.com © 12
3. Procedural-Oriented language (3GL)

Merits

• Because of their flexibility, procedural languages are able to


solve a variety of problems.
• A programmer does not need to think in terms of computer
architecture which makes them focused on the problem.
• Programs written in this language are portable.
• It is easier to develop and debug programs compared to low
level languages.

© padandaas.com © 13
3. Procedural-Oriented language (3GL)

Demerits

• It is easier but needs a higher processor and larger


memory.
• It needs to be translated therefore its execution time is
more.
• High-end GUI-based programs cannot be created
using 3GL.

© padandaas.com © 14
4. Problem-Oriented language (4GL)
• Application specific language.
• It allows the users to specify what the output should be, without describing all the
details of how the data should be manipulated to produce the result.
• These are result-oriented and include database query language.
• 4GL language supports for
Database management
Report generation
Mathematical optimization
Web or GUI development
• Examples
SQL, FOCUS etc. (Database query language)
RPG-II (Report generator language)
Mathematica (mathematical optimization) etc.
© padandaas.com © 15
4. Problem-Oriented language (4GL)
Merits

1. Easy to understand & learn.


2. Less time required for application creation.
3. It is less prone to errors.
4. Programmer need not to think about the procedure of the program.
Demerits

1. Memory consumption is high.


2. Has poor control over Hardware.
3. Less flexible.
4. It needs to be translated therefore its execution time is more
© padandaas.com © 16
© padandaas.com © 17
5. Natural language (5GL)
• A natural language statement’s text is very similar to human speech and
5GL is used in artificial intelligence.
• Any programming language based on concept that rather than solving a
problem algorithmically, an application can be built to solve it based on
some constraints, i.e., we make computers learn to solve any problem is
referred to as a fifth-generation programming language (5GL).
• They have following characteristics:
They are designed to make the connections that humans have with computers
more natural – more humanlike.
They are designed to allow the computer to become smarter- to actually
simulate the learning process by remembering and improving upon earlier
information.
• Examples are LISP and PROLOG
© padandaas.com © 18
5. Natural language (5GL)
Merits
• Easy to program. Since the program uses normal sentences.
• They are easy to understand and no use of intensive coding.
• The programs designed using 5GL will have artificial intelligence
(AI).
• The programs would be much more interactive and interesting.

Demerits
• It is slower than previous generation language as it should be
completely translated into binary code which is a tedious task.
• Highly advanced and expensive electronic devices are required to run
programs developed in 5GL. Therefore, it is an expensive approach.
© padandaas.com © 19
Language Processor
• Is a software that translate program written in other language into
equivalent machine codes.
• Machine only understood machine language instructions. So, program
written in other language (called source program) need to translate
into equivalent machine code before execution.
• There are types of language processors:
1. Assembler
2. Compiler
3. Interpreter

© padandaas.com © 20
1. Assembler
• Language processor that translates an assembly language program into
its equivalent machine language program of the computer is known as
assembler.
• Program in assembly language is called a source program which is
input to the assembler.
• The output generated by the assembler is the object code or machine
code which is understandable by the computer.
• Assembly language uses mnemonics(instructions) like ADD, MUL,
MUX, SUB, DIV, MOV and so on. and the assembler basically
converts these mnemonics in Binary code.

© padandaas.com © 21
2. Compiler
• Language processor that translates a high level language program into its
equivalent machine language program in one go if the source program is error free
is known as compiler.
• Each high-level language has its own compiler. A compiler can translate only
those source programs, for which it is designed.
• Compiler generates the object program.
• Gives the error message (if any) only after scanning entire program.
• Compiled program runs faster.

© padandaas.com © 22
3. Interpreter
• Language processor that translate the high level language program into
machine codes line by line (statement by statement) and executes the
resulting code before moving to next line is known as interpreter.
• Interpreter displays an error at a time. The programmer should fix that
error to interpret the next line.
• Interpreter doesn't generate object program.
• Interpreted program runs slower than compiled program.
• Programming languages such as Python, Ruby, PHP, Perl are some
examples of interpreter-based languages.

© padandaas.com © 23
Syntax and Semantics
Syntax

• Syntax is grammar of a language, i.e. it refers to the rules and


regulations for writing any statement in a programming language
like C/C++.
• In other word it is a rule that tells us whether program written using
programming language are correct or not.
• It does not have to do anything with the meaning of the statement.

© padandaas.com © 24
Syntax and Semantics
Semantics

• The semantic refers to the meaning associated with the statement in a


programming language.
• It is all about the meaning of the statement which interprets the
program easily.
• Sometimes, grammatically correct words do not make sense, even
when they are correct grammatically. Semantics helps add the layer of
meaning so that words make sense.

© padandaas.com © 25
Program errors
• Programming errors are the problems or the faults that occur in the
program which prevents a program from compiling, and running
correctly.
• Programming errors are also known as the “bugs” and the process of
removing these bugs is known as debugging.
• Types of programming errors.
1. Syntax error
2. Run-time errors
3. Logical errors

© padandaas.com © 26
Syntax error
• The errors which are occurred due to the violation of rules of the
specified programming language is known as syntax error.
• These type of error are detected with the help of compiler.
• When syntax errors are presents, the compilation fails and is
terminated after listing errors and the line numbers in the source
program, where error have occurred.
• After viewing error message, programmer can simplify fixed it.

© padandaas.com © 27
If we write the following statement to print Hello
World.
Then compiler report the error message, semicolon
missing as every C statement should terminate with
semicolon.

• This can be fixed by adding semicolon as

© padandaas.com © 28
Runtime error
• The errors which occurs during the execution of program is known as
runtime errors.
• Execution of program with runtime errors will terminates abnormally.
• That is program with runtime errors will compiled, run , takes input
but do not produce the complete result.
• Runtime errors are due to :
Attempt to divide by zero
Trying to open a fie that is not created
Lack of memory space etc.

© padandaas.com © 29
Logical error
• Errors due to poor understanding of the problem, incorrect translation of the
algorithm into the program and a lack of clarity of hierarchy of operators are
known as logical errors.
• Compiler can not catch the logical errors.
• Program with logical error will compiled, run ,takes input but produce undesired
result.
• To debug the logical errors
First we have to run the program, gives the input data.
If it gives the undesired result, then we know there is logical error in program.
To detect the error, we have to check the entire program line by line or trace it
or needs help of expert.
Then only it can be fixed by writing appropriate code.
© padandaas.com © 30
Control Structure

• Control structure determines the order in which the program


instructions are performed by computer.
• That is, it control the flow of executions of statements in a program.
• There are three types of control structure:-
(1) Sequence
(2) Selection
(3) Iteration

© padandaas.com © 31
Sequence

• It is a logic which executes statement one after another serially.


• Instruction are put in predefined sequence and the next instruction is
executed by the CPU only after the execution of previous statement.

© padandaas.com © 32
Selection / Conditional / Branching

• Also called branching


• Set of statements that executes conditionally.
• It is used to select an action or not, based on result of condition(s).
• Used for decision making.
• Different form of branching statement are:
• if statement
• switch statement

© padandaas.com © 33
Iteration / Looping / Iteration

• Loop is the set of statements that executes repeatedly and conditionally.


• In loop a block of statements executes repeatedly until the condition is true.
• Used when we want to repeat a part of program or block of statement several
times.
• There are three types of loop in C:
• while loop
• do while loop
• for loop

© padandaas.com © 34
Program Design Tools

• Program design tool is a tool that is used to design the logic of a


program before it is actually developed.
• Once we have logic design, it can be easily converted into program.
• Some of the common tools used for program design are
1. Algorithm
2. Flowchart
3. Pseudo code

© padandaas.com © 35
1. Algorithm

• A set of rule that define how a particular problem can be solved in a


finite number of steps is known as algorithm.
• An algorithm is composed of finite number of steps, each of which
may require one or more operations.
• It is step by step logical representation of solution of problem. I
• It is programming language independent.
• Once we have an algorithm, it act as basis for coding.

© padandaas.com © 36
1. Algorithm
Properties

1. Inputs/outputs: There must be some inputs and output.

2. Definiteness: Each step in algorithm must be in simple language and clear.

3. Finiteness: An algorithm should be finished in finite steps.

4. Independent: Algorithm must be programming language independent.


5. Executable: The steps mentioned in the algorithm should be executable by the
computer.

© padandaas.com © 37
1. Algorithm
Example 1:
Design an algorithm to go to cinema and watch movie.
Step 1: START
Step 2: Go to cinema hall.
Step 3: Is ticket counter open?
Yes: go to Step 4
No: go to Step 6
Step 4: Buy the ticket and go to the hall.
Step 5: Watch movie.
Step 6: Return home
Step 7: Stop

© padandaas.com © 38
1. Algorithm
Example 2:

Design an algorithm to find the sum of the two numbers.


Step 1: START
Step 2: Read two numbers A and B.
Step 3: Add two number and store in SUM.
SUM = A + B
Step 4: Display SUM
Step 5: Stop.

© padandaas.com © 39
1. Algorithm
Example 3:

Design an algorithm to find weather the number is odd or even.


Step 1: START
Step 2: Read a number.
Step 3: Is number perfectly divisible by 2.
Yes: Print number is even.
No: Print number is odd.
Step 4: Stop.

© padandaas.com © 40
2. Flowchart

• A flow chart is a graphical representation of step by step solution of a


problem.
• It uses different graphical symbols to show a data flow with in a
program.
• Flowcharts are used in analyzing, designing, documenting or
managing a process or program in various fields.

© padandaas.com © 41
2. Flowchart
Advantages
• A flowchart is a pictorial representation of the flow of operations. Hence it is easy
to understand.
• The error in the logic can be easily detected.
• Flowchart is an effective communication tool.
• Once flowchart is complete, it acts as guideline to write a program.

Disadvantages
• Drawing flowchart is time consuming.
• Flowcharts are difficult to modify as redrawing of flowchart is required.
• When a process is relatively complex, this can make flowchart look messy and
clumsy.
• Flowcharts are difficult to create as© they require various shapes.
padandaas.com © 42
2. Flowcharts
Symbol Used in Flowchart

© padandaas.com © More Symbols 43


2. Flowchart
1. Draw the flowchart to add two numbers.

© padandaas.com © 44
2. Flowchart
2. Draw the flowchart to calculate profit and loss.

© padandaas.com © 45
3. Pseudocode

• Pseudo code is a false code which can be understood by even a non


programmer.
• Pseudocode is an informal way of programming description that does
not require to follow any programming language syntax.
• Pseudocode is not an actual programming language. So it cannot be
compiled into an executable program.
• It uses short terms or simple words to write code before it is actually
converted into a actual programming language.

© padandaas.com © 46
Codes
• Digital computer only understands binary code (0s and 1s).
• Any text, image, audio, and video data are stored by the computer in the form of
bits(a series of 1s and 0s).
• There are different types of codes to represent the data in binary form, and each of
them may use different numbers of bits.
• Some of the methods are:
Absolute Binary
BCD (Binary Coded Decimal)
ASCII
EBCDIC
Unicode

© padandaas.com © 47
Absolute Binary
• In this type, every number is represented in term of absolute binary.
• For example the decimal 15 is represented as 1111.

BCD (Binary Coded Decimal)


• BCD is the simplest code used to represent the decimal digit into four bit binary
number.
• If a decimal number consist of more than one digit, each decimal digit is
represented individually by its 4-bit equivalent.
• For example to represent 15, both 1 and 5 is converted into 4 bit binary,
i.e. 0001 0101.
Thus BCD of 15 is 00010101.
© padandaas.com © 48
ASCII
• ASCII stands for American standard code for information interchange.
• It is a worldwide standard for the code numbers used by computers to represent all
uppercase and lowercase alphabets, numbers, punctuation marks and some special
characters and symbols.
• It use 7 bit to represent a character, hence 128 different characters can be
represented by using ASCII: 0000000 to 1111111.
• Among seven bits first three bits are called zone bits and remaining four bits are
used for digit values.
• There is also ASCII-8 that use 8 bits to represent characters, and can represent 256
characters.
• Among 8 bits first four are zone bits and last four are value bits. E.g. ASCII of A
is 0100 0001
© padandaas.com © 49
Unicode
• Since the ASCII code can only encode the Latin alphabets so, some other codes
are required to encode alphabets of other languages or some special symbols.
• Unicode is a universal code used for character encoding of any written language
of the world.
• To keep character coding simple and efficient the Unicode standard assigns each
character a unique numeric value and name independent of platform.
• It is international standard that use 16 bit to represent character. Hence 2^16 =
65536 different characters can be represented using Unicode..
• This is sufficient to represent any character of any language.
• The major advantage of Unicode over other texts code symbol is its compatibility
with ASCII code.

© padandaas.com © 50
EBCDIC
• EBCDIC stands for extended binary coded decimal
interchange code.
• It is an IBM code widely used in IBM equipment's for
representing characters as numbers.
• It uses 9 bits in which first 8 bits are used for each characters
and ninth bit for parity.
• 8 bits can be used to represent 256 characters

© padandaas.com © 51

You might also like