5
Computer Programming
• Itis the process of giving instructions (commands) to the computer to do a
meaningful task.
• It is an act of teaching the computer on how to do a task.
• The art and science of creating instructions for a computer to follow.
• Creating a sequence of instructions to enable the computer to do something.
7
Why Programming?
• Computeris just a dumb machine made up of different electronic components. It
is like a box which cannot do anything by itself.
• It is the user who tells the computer “what it has to do?”
• If we need our computer to perform some task, we first have to teach the
computer in detail “how it will accomplish that task?”
• Once the computer is taught about a particular task, it will completely obey it but
cannot do anything that it is not taught to.
8.
8
Why Programming?
• Likethe humans, we can teach the computer through communicating with it
using a particular language.
• The language that computer understands is machine language, also called as
binary language. Machine language is the language of 0s and 1s.
• We give detailed instructions to the computer to solve a particular task.
Programming is the term that refers to teaching, instructing or giving
commands to the computer.
9.
9
Why Programming?
• Programmingis more about problem-solving skills than writing the code itself.
• Programming teaches you how to understand, analyze, and solve problems. It
enhances your analytical reasoning abilities and helps you cope with daily real-
life problems as well.
• Hence learning to program is important because it develops analytical and
problem-solving abilities.
10.
10
Programmer
• The personwho gives the instructions (commands) to the computer is known as
the programmer.
• A person who designs and writes computer programs.
12
Instruction
• Instruction isany command given to the computer.
• For example:
1) Add two variables A and B
2) Display result
3) Read file
• Each of these is the individual instruction to the computer.
14
Program
• Program isa set (collection) of instruction to do a meaningful task.
• A sequence of instructions that are interpreted and executed by a computer. It
can be made of a single or hundred of instructions.
• For example: In order to teach the computer on how to calculate average of
three numbers? We need to give multiple instructions to the computer to do the
task.
15.
15
Program
Instruction 1: Getfirst number from the user and store it in A variable
Instruction 2: Get second number from the user and store it in B variable
Instruction 3: Get third number from the user and store it in C variable
Instruction 4: Add A, B, C and store the result in SUM variable
Instruction 5: Divide SUM by 3 and store result in AVG variable
Instruction 6: Display AVG variable
• Instructions 1-6 are used to solve a single task. This collection of instruction is
known as a program.
17
Programming Language
• Aprogramming language is an artificial language designed to
communicate instructions to a computer.
• A programming language is a notation for writing programs.
• A vocabulary and set of grammatical rules for instructing a computer to
perform specific tasks.
18.
18
Programming Language
• Eachlanguage has a unique set of keywords (special words that it understands)
and a special syntax (format) for organizing program instructions.
• There are many programming languages. For example:
• GW Basic
• C
• C++
• JAVA
• Pascal
• COBOL
• Python
• C#
19.
19
Types of ProgrammingLanguages
• There are three types of programming languages:
Programming
Languages
Low-Level
Languages
High-Level
Languages
Middle-Level
Languages
21
Low-Level Language
• Alow level language is one which is closer to the machine (computer).
• It is easier for machines to understand and difficult for humans to understand.
• It is faster in execution as compared to high and middle level languages.
23
Machine Language
• Itis one of the low level language.
• It is the language of 0s and 1s.
• Machine languages are the only languages directly understood by the
computers.
• While easily understood by computers, machine languages are almost
impossible for humans to use because they consist entirely of numbers (0s and
1s).
24.
24
Machine Language
• Itis the native language of the machines (computers).
• Here all the instructions are written as code of binary sequence. For example:
• In order to do addition, the code is: 10010001
• In order to decrement a number by one, the code is: 11011011
• In order to move data from one place to another, the code is: 10000111
25.
25
Machine Language
• Thereare hundreds of instructions and each instruction has a binary code.
• Is it possible to remember all the codes of hundreds of instruction?
• Obviously not! Hence machine language almost impossible to understand.
26.
26
Machine Language
• Machinelanguage program example:
10010010
11001010
01001010
11110101
00000101
00101000
11101010
10101010
27.
27
Assembly Language
• Assemblylanguage is same as machine language but uses English like words
to represent individual operations.
• For example: Instead of binary codes it uses : ADD, MOV, SUB, INC
• Assembly language is also a low-level language.
• It is easier than the machine language but still it is very difficult to control a
larger program using assembly.
28.
28
Assembly Language
• Asassembly language contains English like words, which will not be
understood by the computer (because it only understands 0s and 1s)
• A translator first converts the assembly language program into machine
language program.
• Translator used with assembly language is called Assembler.
31
High-Level Language
• Ahigh-level language is one which is closer to the human
(programmer).
• It is easier for humans to understand and difficult for machines
to understand.
• It is slower in execution as compared to low level languages.
32.
32
High-Level Language
• Likeassembly language, it also uses English like words for the
operations.
• For example: for, if, else, break, continue, while, include,
using, import
• It is more easier than assembly language.
33.
33
High-Level Language
• Someof the high level programming languages are:
• GW Basic
• C++
• JAVA
• Pascal
• COBOL
• Python
• C#
• Visual Basic
• J#
• Ruby
• PHP
34.
34
High-Level Language
• Highlevel language program example:
int main()
{
int a = 5;
int b = 6;
if(a > b)
cout<<“First number is greater.”;
else
cout<<“Second number is greater.”;
}
36
Middle-Level Language
• Amiddle level language is one which is closer to machine
(computer) as well as to human (programmer).
• A language that has the features of both low level and high level
languages.
• More formally, a high level language that allows you to write low
level programs in it is called as middle level language.
37.
37
Middle-Level Language
• Someof the middle level programming languages are:
• C
• IBM PL/S (Programming Language/Systems)
• BCPL (Basic Combined Programming Language)
• BLISS (Bill's Language for Implementing System Software)
38.
38
Source Code andObject Code
Source Code
• The set of instructions written in any
language other than machine language is
called as source code.
• It is not directly understood by the
machine (computer).
Object Code
• The set of instructions written in machine
language is called as object code. It is also
known as machine code.
• It is the only code which is directly
understood by the machine (computer).
39.
39
Source Code andObject Code
Source Code
• It is in the form of text.
• It is human readable.
• It is generated by human (programmer).
• It is input to the language translator.
Object Code
• It is in the form of binary numbers.
• It is machine (computer) readable.
• It is generated by the language translator.
• It is the output of the language translator.
41
Language Translators
• Languagetranslator is a program that converts the source code in
to the object code.
Source Code Object Code
CONVERT
Language Translator
Translator
42.
42
Why Language Translators?
•Computer only understands object code (machine code).
• It does not understand any source code.
• There must be a program that converts source code into the object code so that the
computer can understand it.
• The language translator is one which does this job.
• The programmer writes the source code and then translator converts it in machine
readable format (object code).
43.
43
Types of LanguageTranslators
• There are three types of language translator:
Language Translators
Assembler Compiler Interpreter
44.
44
Assembler
• Assembler isthe language translator that converts assembly
language code in to the object code (machine code).
Assembly
Source Code
Object Code
CONVERT
Assembler
45.
45
Compiler
• Compiler isthe language translator that converts high level
language code in to the object code (machine code).
• It converts the whole code at a time.
High-Level
Source Code
Object Code
CONVERT
Compiler
46.
46
Compiler
Line 1 :Instruction 1
Line 2 : Instruction 2
Line 3 : Instruction 3
Line 4 : Instruction 4
Line 5 : Instruction 5
Program
Line 1 : Instruction 1
Line 2 : Instruction 2
Line 3 : Instruction 3
Line 4 : Instruction 4
Line 5 : Instruction 5
Read whole Program
Convert whole
program in to object
code
Execute
1 2 3 4
47.
47
Interpreter
• Interpreter isthe language translator that converts high level
language code in to the object code (machine code).
• It converts the code line by line.
High-Level
Source Code
Object Code
CONVERT
Interpreter
48.
48
Interpreter
Line 1 :Instruction 1
Line 2 : Instruction 2
Line 3 : Instruction 3
Line 4 : Instruction 4
Line 5 : Instruction 5
Program
Read Line 1 Convert in to object code Execute
1
Read Line 2 Convert in to object code Execute
Read Line 3 Convert in to object code Execute
Read Line 4 Convert in to object code Execute
Read Line 5 Convert in to object code Execute
2
3
4
5
6
49.
49
Difference between Compilerand Interpreter
Compiler
• It converts whole code at a time.
• It is faster.
• Requires more memory.
• Errors are displayed after entire program is
checked.
• Example: C, C++, JAVA.
Interpreter
• It converts the code line by line.
• It is slower.
• Requires less memory.
• Errors are displayed for every instruction
interpreted (if any).
• Example: GW BASIC, Ruby, Python
51
Bug
• An erroror defect occurred inside a computer program or
hardware that causes it to produce an incorrect or unexpected
result, or to behave in unintended ways is called as a bug.
• Most of the bugs arise from mistakes and errors made by
programmer in source code.
52.
52
Bug
• The termbug was used by Grace Hopper in 1946.
• Hopper used to work on Mark II computer, there some error
occurred in the system. The cause of the error was a moth (bug)
trapped in a relay creating short circuit.
• That caused the term bug to be coined.
54
Debugging
• It isthe process of finding and fixing the bugs (errors) in the
program.
• It is the process of removing errors.
• The programmer manually does this by examining the source
code.