Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 27

Introduction to

Programming

Ms Qurat-ul-Ann
What Can a Program Do?

 A program can only instruct a computer to:


• Read Input
• Maintain Sequence
• Calculate
• Store data
• Compare and branch
• Iterate or Loop
• Write Output
Ms Qurat-ul-Ann
Sequence Control Structures

 Sequence control structures direct the order


of program instructions.
 The fact that one instruction follows another
—in sequence—establishes the control and
order of operations.

Ms Qurat-ul-Ann
Calculate

 A program can instruct a computer to


perform mathematical operations.

Ms Qurat-ul-Ann
Store

 A program will often instruct a computer


to store intermediate results.

Ms Qurat-ul-Ann
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
Ms Qurat-ul-Ann
Programs are Solutions
to Problems
 Programmers arrive at these solutions by
using one or more of these devices:
 Words
 Logic flowcharts
 Pseudocode
 Programming language

Ms Qurat-ul-Ann
 Algorithm
• A FINITE set of clear, executable steps that will
eventually terminate to produce the desired
outcome
• Logical design used to solve problems – usually a list
of actions required to perform task
 Pseudocode
• Written like program code but more “English Like”
and doesn’t have to conform to language syntax
 Flowchart
• Diagram that visually represents the steps to be
performed to arrive at solution.
Ms Qurat-ul-Ann
Example 1:

 Write an algorithm to determine a student’s


final grade and indicate whether it is
passing or failing. The final grade is
calculated as the average of four marks.

Ms Qurat-ul-Ann
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”

Ms Qurat-ul-Ann
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.

Ms Qurat-ul-Ann
Common Flowchart Symbols
Common Flowchart Symbols

Terminator. Shows the starting and ending points of


the program. A terminator has flowlines in only one
direction, either in (a stop node) or out (a start node).

Data Input or Output. Allows the user to inputdata


and results to be displayed.

Decision. The diamond indicates a decision structure. A


diamond always has two flowlines out. One flowlineout
is labeled the “yes” branch and the other is labeled the
“no” branch.
Ms Qurat-ul-Ann
Common Flowchart Symbols

Processing. Indicates an operation performed by the


computer, such as a variable assignment or
mathematical operation.

Off-page connector. Even fairly small programs can


have flowcharts that extend several pages. The off-
page connector indicates the continuation of the
flowchart on another page. Off-page connectors come
in pairs.
Flowline. Flowlines connect the flowchart symbols
and show the sequence of operations during the
program execution .
Ms Qurat-ul-Ann
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/right to left.
 It should be simple and drawn clearly and neatly.
 The branches of decision box must be labeled.

Ms Qurat-ul-Ann
Advantages / Disadvantages

Advantages
 Provides convenient method to understand the

solution.
 Helps in debugging process
 Provides guide for coding.
Disadvantages
 Not suitable for large programs.

Ms Qurat-ul-Ann
Calculate Pay

Ms Qurat-ul-Ann
The program computes the sum, average and
product of three numbers:

Ms Qurat-ul-Ann
Sum of two numbers

Ms Qurat-ul-Ann
START

Input
M1,M2,M3,M4 Example

GRADE(M1+M2+M3+M4)/4
Step 1: Input M1,M2,M3,M4
Step 2: GRADE  (M1+M2+M3+M4)/4
Step 3: if (GRADE <50) then
Print “FAIL”
N Y
IS else
GRADE<50
Print “PASS”
end if

PRINT PRINT
“PASS” “FAIL”

Ms Qurat-ul-Ann
STOP
Pseudo code

 This device is not visual but is considered a


“first draft” of the actual program.
 Pseudocode is written in the programmer’s
native language and concentrates on the logic
in a program—not the syntax of a
programming language.

Ms Qurat-ul-Ann
Pseudocode

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

Ms Qurat-ul-Ann
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

Ms Qurat-ul-Ann
Calculate Pay

Ms Qurat-ul-Ann
Sum Of Two Numbers

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

Ms Qurat-ul-Ann
Practice

Draw flowchart for


computing Factorial
of N

Ms Qurat-ul-Ann
Practice
Solution

Ms Qurat-ul-Ann
OOPS!!!
ASSIGNMENT

1. Draw flowchart for calculating the


average of 25 exam scores.

Ms Qurat-ul-Ann

You might also like