Sei sulla pagina 1di 53

Introduction to Programming

What Can a Program Do?


A program can only instruct a computer to:
Read Input Sequence Calculate Store data Compare and branch Iterate or Loop Write Output

Sequence Control Structures


Sequence control structures direct the order of program instructions. The fact that one instruction follows another in sequenceestablishes the control and order of operations.

Calculate
A program can instruct a computer to perform mathematical operations.

Store
A program will often instruct a computer to store intermediate results.

Compare and Branch


A program can instruct a computer to compare two items and do something based on a match or mismatch which, in turn, redirect the sequence of programming instructions.
IF IF-ELSE

IF-THEN
Entry Test condition p
false true

Exit

True statement a

IF-THEN-ELSE
Entry Test condition p
false true

false statement a

Exit

true statement a

Iterate
A program loop is a form of iteration. A computer can be instructed to repeat instructions under certain conditions.
No

Iteration Control Structures


Iteration control structures are looping mechanisms. Loops repeat an activity until stopped. The location of the stopping mechanism determines how the loop will work: Leading decisions Trailing decisions

Leading Decisions
If the stop is at the beginning of the iteration, then the control is called a leading decision. The command WHILE performs the iteration and places the stop at the beginning.

WHILE Loop
Entry Exit Test condition p
Yes No

Loop statement a

Trailing Decisions
If the stop is at the end of the iteration, the control mechanism is called a trailing decision. The command DO UNTIL performs the iteration and puts the stop at the end of the loop.

DO UNTIL Loop
Entry

Loop statement a Test condition p


No Yes

Exit

Programs are Solutions to Problems


Programmers arrive at these solutions by using one or more of these devices: Words Logic flowcharts Pseudocode Programming language

Example 1:
Write an algorithm to determine a students final grade and indicate whether it is passing or failing. The final grade is calculated as the average of four marks.

In words
Input a set of 4 marks Calculate their average by summing and dividing by 4 if average is below 50 Print FAIL else Print PASS

Logic Flowcharts
These represent the flow of logic in a program and help programmers see program design. It is a way of visually presenting the flow of data, the operations performed within the system and the sequence in which they are performed.

Rules for Drawing a Flowchart


It should contain only one starter and one end
symbol. The direction of arrows should be top to bottom and left to right. It should be simple and drawn clearly and neatly. The branches of decision box must be labeled.

Advantages / Disadvantages
Advantages Provides convenient method to understand the solution. helps in debugging process Provide guide for coding. Disadvantages Not suitable for large programs.

Calculate Pay

The program computes the sum, average and product of three numbers:

Sum of two numbers

Example
START Input M1,M2,M3,M4

GRADE(M1+M2+M3+M4)/4

IS GRADE<5 0

Step 1: Input M1,M2,M3,M4 Step 2: GRADE (M1+M2+M3+M4)/4 Step 3: if (GRADE <50) then Print FAIL else Print PASS endif

PRINT PASS

PRINT FAIL

STOP

Psuedocode
This device is not visual but is considered a first draft of the actual program. Pseudocode is written in the programmers native language and concentrates on the logic in a programnot the syntax of a programming language.

Pseudocode
Step 1: Step 2: Step 3: Input M1,M2,M3,M4 GRADE (M1+M2+M3+M4)/4 if (GRADE < 50) then Print FAIL else Print PASS endif

The program computes the average of three numbers


Begin input x input y input z sum = x + y + z avg = sum / 3.0 print avg End

Calculate Pay

Sum Of Two Numbers


Begin input x, y sum = x + y print sum END

Practice

Draw flowchart for computing Factorial of N

Practice Solution

Introduction to Programming Languages and Programming


.

Programs
A program is a set of step-by-step instructions that directs the computer to do the tasks you want it to do and produce the results you want. Computer programs are written in programming languages.

Programmer
A person who develops a program is called programmer. The programmer develops programs to instruct the computer how to process data to convert into information Programmers use programming languages to write programs

Programming Languages
A computer language is a means of communication between user and computer. A programming language is a set of rules that provides a way of telling a computer what operations to perform.

Levels of Programming Languages


1.Low Level Languages:
The languages are near to computer and far from human languages . Computer can understand these languages easily. Following are low level languages: 1.Machine language 2.Assembly Language

Low Level Languages:


1.Machine Languages
A type of language in which instructions are written in binary form . It is only language that is understood by the computer. It is fundamental language of computer. Program written in machine language can be executed very fast by the computer It is also known as first generation language.

Machine Languages
different for each computer processor

0100 001101 100000 001101 110001 00101 10001 10000 01110 111001 . . .

2.Assembly Languages
It is low-level language It is one step higher than machine language In assembly language, symbols are used instead of binary code These symbols are called mnemonics For example: sub instruction is used to subtract two numbers. It is also called symbolic language It is mostly used for writing system software. Assembly language is also known as second generation language.

Assembly Languages
different for each computer processor

main

proc pay mov ax, 4 mov bx, 3 add ax, bx

Levels of Programming Languages


2.High Level Languages
A type of language that that is close to human languages is called high level language. High level languages are easy to understand Instruction of these languages are written in English-like words such as input, print etc. High level languages are further divided into following categories: 1.Procedural Languages 2.Object Oriented Languages 3.Database Query Languages

High-Level Languages
1.Procedural Languages: Procedural Languages are also known as third generation languages or 3GL. In these languages program are written in predefined set of instructions Computer execute these instruction in the same sequence in which these instructions are written. Each instruction in this language tells the computer what to do and how to do.

1.Procedural Languages:
Some procedural languages are as follows FORTRAN (Formula Translation) BASIC (Beginners All Purpose Symbolic Instruction Code) COBOL (Common Business Oriented Language) PASCAL C

2.Object Oriented Languages


OOP is a technique in which programs are written on basis of objects. An object is a collection of data and functions Object may represent a person ,place or things in real world. In OOP data and all possible functions on data are grouped together C++ and Java are two most popular object-oriented languages.

Database Query Languages


Database is collection of related data in an organized way Database query languages are used to retrieve ,insert ,delete or search data from database These are non-procedural languages Non-procedural languages are also called fourth generation languages or 4GL In non-Procedural languages user only needs to tell the computer what to do not how to do.

Language Translator
Language processor or translator is software that converts these programs into machine language Each computer language has its own translators. Different types of language translator are : compilers Interpreters Assembler

Compiler
Translates the entire program to machine code before running it. A program written in high level language is called source program. The compiler convert source program in the machine code called object program. The object program can be executed many times. Compiler also checks syntax errors in the program A source program containing an error cannot be compiled

Interpreter
Translates instructions to machine code line-by-line or statement by statement. It executes statement before translating the next statement of the source program If there is an error in the statements , the interpreter stops working and displays an errors message. The disadvantage of interpreter is that it is not very efficient The interpreter does not produce an object code .it must convert the program each time it is executed . Visual Basic uses interpreter.

Programmers Lingo

Interpreter Process

Assembler
An assembler is a translating program that translates the instructions of a program written in assembly language into machine language.
Assembly language program code Object

Assembler

Programmers Lingo
Program - detailed set of instructions for a computer Programming Language - tool used to create a program; defined by semantics and syntax Semantics - the meaning of words in a language Syntax - rules for combining symbols of a language

Programmers Lingo
Source Code (code) - program you write using a programming language

ASSIGNMENT
1. Draw flowchart for calculating average of 25 exam scores. the

Potrebbero piacerti anche