Unit 1 Introduction to Programmng
Unit 1 Introduction to Programmng
What is a Computer?
A computer is an electronic computing machine used to solve a large variety of problems
related with all the aspects of our life.
It consists of hardware and software. Physical components of the computer are known as
hardware and software means computer programs.
Computer Program
A computer program is a set of instructions written in a programming language to solve a
particular problem and achieve specific results. People learn programming languages to
use the computer as a problem-solving tool. Each statement of a program has syntax and
semantic.
Syntax
Syntax of a programming language defines the rules for writing valid statements in a
program. It is similar to the grammar of a natural language. It determines the correct form
of statements of a computer program.
For example, an assignment statement consists of a variable and an expression separated by
equal sign. This is the syntax and it can be expressed as:
variable = expression; sum = a + b;
Semantic
Semantic gives meaning to correct statements of a programming language. It describes
the sequence of operations to be performed when executing the statements of a program
.
For example, in the assignment statement
sum = a + b;
the semantic of the statement is to perform the expression, that is, add the values stored in
variables a and b and then store the result in variable sum.
Machine language
Programming language that is directly understood by the computer is known as machine
language.
It consists of zeroes and ones (0s & 1s).
It is almost impossible for humans to write programs in machine language.
Therefore, practically no programming is done in machine language. Instead,
assembly languages and high level languages are used.
Machine language is associated with the architecture of computer. Therefore,
programs written in machine language for one computer will not work on another
because of design difference.
Assembly Language
Assembly language consists of symbols or abbreviations known as mnemonics. A program
known as assembler is used to translate assembly language program into machine language
before it is executed by the computer. Assembly language is associated with the
architecture of the computer. Therefore, each computer has its own assembly language.
Some examples of mnemonics are given below with their meanings.
Mnemonic Meaning
MUL Perform multiplication
ADD Perform addition
JUMP Perform a jump operation
MOV Move data
Procedural Languages
Procedural language is based upon the concept of modular programming. In modular
programming programs are divided into smaller parts known as modules. Modular
programs consist of one or more modules. A module is a group of statements that
can be executed more than once in a program. Each module performs a specific task.
It is easy to design, modify and debug a program in a procedural language since it
provides better programming facilities.
Structured Languages
Structured languages consist of three fundamental elements, which are sequence,
selection and repetition.
Sequence
Sequence means that each step of the algorithm is executed in a specified order. The
following algorithm adds two numbers. It performs the steps in a purely sequential
order.
Step 1: Input a number, A
Step 2: Input the second number, B
Step 3: Set SUM = A + B
Step 4: Output SUM
Step 5: End
Selection (Decision)
Decision control structure is used when the execution of a process depends on the
result of some condition. For example, If x = y then print “EQUAL”. So the general
form of IF construct is:
If condition Then Process
If the condition is true then the Process will be executed.
A decision statement can also be stated in the following manner.
If condition
Then Process1
Else
Process2
Flowchart
Flowchart is a diagrammatic or graphical representation of algorithm. It describes what
operations are required to solve a given problem.
Flowchart Symbols
Flowcharts are drawn using standard symbols. These symbols have specific meaning and are
connected by arrows indicating the flow from one step to another. These symbols are given
in the following table.
Symbol Symbol
Symbol Shape Symbol Shape
Description Description
Start/Stop Decision
Input/Ouput Connector
Flow Line: It is a line with arrow head used to connect various flowchart symbols and
indicates the flow of control in the flowchart.
Start/Stop Symbol: It is a rounded rectangular shaped symbol. It is used to indicate the start
or end of a flowchart. We can only write the words Start or Stop inside this symbol. A
flowchart can only have one start but it may have many ends.
Input/Output Symbol: Parallelogram represents input or output operations in a flowchart. It
contains the word READ or INPUT along with the variables for input operation or PRINT or
OUTPUT along with the output data for output operation.
START
INPUT A,B,C
SUM=A+B+C
PROD=A*B*C
AVG=SUM/3
OUTPUT SUM,PROD,AVG
STOP
Problem 2: Draw flowchart to input the length of one side of cube and print its volume.
START
INPUT L
V = L*L*L
OUTPUT V
STOP
START
K=1
OUTPUT K
K=K+2
YES
K < 100 ?
NO
STOP
START
SUM=0, K=2
SUM=SUM+K
K=K+2
YES
K ≤ 100 ?
NO
OUTPUT SUM
STOP
START
INPUT X, Y
TEMP=X
X=Y
Y=TEMP
OUTPUT X, Y
STOP
START
INPUT F
C = 5/9(F-32)
OUTPUT C
STOP
START
INPUT M
YES
M ≥ 90 ? OUTPUT
YES“A”
NO
YES
M ≥ 80 ? OUTPUT “B”
NO
YES
M ≥ 70 ? OUTPUT “C”
NO
STOP
YES
M ≥ 60 ? OUTPUT “D”
NO
YES
M ≥ 50 ? OUTPUT “E”
NO
OUTPUT “F”
START
INPUT N
F=1, K=1
F=F*K
K=K+1
YES
K≤N?
NO
OUTPUT F
STOP