0% found this document useful (0 votes)
4 views

Programming Principles

The document outlines the process of adding two numbers using structured English, pseudocode, and flowchart representations. It covers various programming concepts such as variables, constants, operators, and control structures, including loops and conditionals. Additionally, it discusses data types, arrays, sub-programs, and file handling in programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Programming Principles

The document outlines the process of adding two numbers using structured English, pseudocode, and flowchart representations. It covers various programming concepts such as variables, constants, operators, and control structures, including loops and conditionals. Additionally, it discusses data types, arrays, sub-programs, and file handling in programming.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 42

Programming Notes

Adding any two number together

Algorithm

Structured English
1. Start
2. Take any two numbers
3. Add both the numbers together
4. Output the sum of numbers
5. End

Pseudocode

1. INPUT x, y
2. Z ¬ x + y
3. OUTPUT z

Flowchart

Start

Input x, y

z←x+y

Output z

Stop
Structure Diagram
Addition

GET Values for x, y z¬x+y OUTPUT z

Based on stepwise refinement or Top-down approach, decomposition,


modularization
Pseudocode
Skills + Creativity (Management)

Components

Variables

Constants

Operator

Expression

Incrementing

Totalling

INPUT (READ) & OUTPUT (PRINT)

Selection (Condition)

Loop / Repetition / Iteration

Formatting / Presentation

Sub Programs

Array

Variable

(Variable name is not case-sensitive but its string value is)

Always keeps its most recent value

A ¬ 10 A

A ¬ 20 30
A ¬ 30

PRINT A
x ¬ -10.25 (Numeric variable)

Temp ¬ 95.7 (Decimal) 95.7

Temp ¬ 100 (Integer) Num ¬ 10

Price ¬ 450.5 (Real) 450.5

Price ¬ 450 (Real) 450

y ¬ “Huzayn” (String variable)

z ¬ “24-C”

y ¬ “123”+1(1231) x ← 123+1

y←““

y ¬ “Z” (String variable)

Address ¬ “945-G” (String variable)

Decision ¬ False (Boolean variable)

Answer ¬ ‘G ’ (Character variable)

DOB ¬ #12/03/05# (Date variable)

Now ¬ “6:48” (Time variable)

A ¬ 65

B ¬ 72

C ¬ 45

D¬C

Saad ¬ 65

Rohan ¬ 72

Sana ¬ 45
A←“ “
y ¬ 10

y¬y+1

y¬y+1 (Increment statement)

y¬y–1 (Decrement statement)

x¬x+3

Declaring / Defining a variable (Optional in pseudocode)

DECLARE Marks : Real

DECLARE Name: String

Assigning value to a variable / Initializing a variable

Marks ¬ 25.5

a ¬ 100

Name ¬ “Huzayn”

DECLARE Num: Integer ¬ 100


Constant

DECLARE CONST Num: Integer

CONST a ¬ 100

Or

CONSTANT a ¬ 100

a ¬ 200

a ¬ 200

CONST Discount ¬ .10

INPUT5 ¬ .20 (error)

CONST Pi ¬ 3.14

Naming Rules for identifiers

1. Must start with a letter.


2. Must not have any space or any special character except an
underscore(_).
3. Must not be a command name. e.g. IF, INPUT, WHILE etc.

Lahore-19

cOVID_19

4_WestStreet

Input5

Ahmed Mubeen

A+b
Operators

1. Assignment (¬)
2. Arithmetic (+, -, *, /, ^)
3. Comparison (=, >, <, >=, <=, <>, !=)
4. Logical (AND, OR, NOT)

Num1 ¬ 200

Num1 ¬ Num2

10^3

Num1 ¬ Num1 + 1

IF (Num1 >= Num2) THEN

IF Num1 > Num2 AND Num1 > Num3 THEN

Expression

Combination of variables, values and operators.

Count ¬ 10

Total ¬ Num1 + Num2 (Operands, Num1 & Num2)

Incrementing

Count ¬ 10

Count ¬ Count - 1 (Usually used in loops)

Totalling

Sum ← 0

Sum ¬ Sum + Marks (Usually used in loops)

Marks ¬ 78

INPUT & OUTPUT


INPUT Num1, Num2

INPUT “Enter student’s marks: ”, Marks

INPUT Marks

A ¬ 58

Total ¬ Num1 + Num2

OUTPUT “The result is:”, Total

INPUT “Please enter the First Value”, Num1 (Prompt message)

INPUT “Please enter the Second Value”, Num2

Total ¬ Num1 + Num2

OUTPUT “The Sum of Num1 and Num2 is”, Total (Alert Message)

OUTPUT Total

PRINT Total

Selection (Condition) (ELSE is optional)

IF <Condition/s> THEN

<Statement/s>

ELSE

<Statement/s>

END IF

IF A > B THEN A ¬ B (Single Line)

Example

INPUT Num1, Num2 (20 , 30)

IF (Num1 < Num2) OR (Num1 = Num2) THEN

(False) OR (False)
OUTPUT Num1

ELSE

OUTPUT Num2

END IF
Nested IF

IF Bill > 10,000 THEN

Discount = 50/100

ELSE

IF Bill > 5,000 THEN

IF Voucher = True THEN

Discount = 50/100

ELSE

Discount = 25/100

END IF

END IF

END IF

Multiple IF

IF Num1 > Num3 THEN

--------

ELSE

-----

END IF

IF Num2 > Num3 THEN

----

ELSE

-----

END IF
Grades

INPUT Percent

IF Percent >= 90 THEN

OUTPUT “A*”

ELSE IF Percent >= 80 THEN

OUTPUT “A”

ELSE IF Percent >= 70 THEN

OUTPUT “B”

….

….

ELSE IF Percent < 50 THEN

OUTPUT “U”

END IF

Case Of Statement

INPUT Percent

CASE OF Percent

>= 90: OUTPUT “A*”

>= 80: OUTPUT “A”

>= 70: OUTPUT “B”

>= 60: OUTPUT “C”

>= 50: OUTPUT “D”

OTHERWISE

OUTPUT “U”
ENDCASE

Remainder ¬ MOD(Slabs,20)

IF Slabs < 20 OR Slabs >100 THEN

OUTPUT “Wrong data entry”

ELSE

IF Remainder <> 0 THEN

CASE OF Slabs

> 80: Slabs ¬ 100

> 60: Slabs ¬ 80

> 40: Slabs ¬ 60

> 20: Slabs ¬ 40

ENDCASE

ELSE

[Calculation]
END IF

ELSE

OUTPUT “Data entered within range”

END IF

Formatting / presentation of pseudocode

1. Indentation / White spaces

FOR Students ¬ 1 TO 5

INPUT Num

IF …… THEN

OUTPUT ….

END IF

END FOR

OUTPUT Total

2. Meaningful identifiers
3. Letter Case
4. Remarks

// The following pseudocode is showing…

// and …

FOR Count ¬ 1 to 10 //Since there are 10 students

IF Marks >= 101 THEN //Validation check

REM Pseudocode ends here

‘ 10 Students’ average marks


5. Annotation

For Counter ¬ 1 to 5

Counter loop gets 10 values


Loop / Iteration / Repetition

1. Unconditional loop (Counter)


2. Conditional loop

1. Unconditional Loop

FOR Students ¬ 30 TO 0 STEP -1

INPUT Marks

Total ¬ Total + Marks

END FOR

Avg ¬ Total/20

OUTPUT Avg

1 Count

2 Count

3 Count

Sum ¬ 0

// Following is Avg loop

FOR Count ¬ 1 TO 15

INPUT Marks

Sum ¬ Sum + Marks

ENDFOR

Avg ¬ Sum / 15

OUTPUT Avg
Sum

70

130
Conditional Loop
1. Pre-condition
2. Post-condition

1. Pre-condition

WHILE-ENDWHILE (runs if condition is True)


DECLARE Num: integer
Num ¬ 0
WHILE Num < 5
Num ¬ Num + 1
OUTPUT Num
ENDWHILE

Output

2. Post-condition

REPEAT .. UNTIL (runs if condition is False)

Num ¬ 0

REPEAT

Num ¬ Num + 1
PRINT Num

UNTIL Num >= 5


Output

Example

Input Marks of a class of 23 students, calculate the average, highest and


lowest marks obtained by them. Also output the number of students who
got marks above 80.

// Following pseudocode outputs Average, highest, lowest and Above-80

// marks

Sum ¬ 0

Highest ¬0

Lowest ¬ 999999

Above_80 ¬ 0

Avg ¬ 0

Highest_Name ¬ “ “

Lowest_Name ¬ “ “

FOR Students ¬ 1 TO 23

OUTPUT “Enter Marks”

INPUT Marks

OR
INPUT “Enter Marks:”, Marks

INPUT “Enter Student Name:”, St_Name

Sum ¬ Sum + Marks

IF Marks > Highest THEN

Highest ¬ Marks

Highest_Name ¬ St_Name

END IF

IF Marks < Lowest THEN

Lowest ¬ Marks

Lowest_Name ¬ St_Name

END IF

IF Marks > 80 THEN

Above_80 ¬ Above_80 + 1

END IF

ENDFOR

Avg ¬ Sum/23

OUTPUT “The Average marks obtained by class:”, Avg

OUTPUT “The Highest marks obtained by a student:”, Highest

OUTPUT “The Highest marks were obtained by:”, Highest_Name

OUTPUT “The Lowest marks obtained by a student:”, Lowest

OUTPUT “The number of students who got above 80 marks:”, Above_80

Highest, Lowest, Above_80


68, 88, 45, 5

Student Mark St_Nam Highes Highest_Nam Lowes Lowest_Nam Above_8


s s e t e t e 0
1 50 Kamal 50 Kamal 50 Kamal 0
2 65 Osama 65 Osama 45 Fahad
3 45 Fahad
4 82 Hussai 82 Hussain 1
n
Validation Check

1. Range Check (100 to 1000)


2. Limit Check
3. Presence Check

Q. Input Students’ marks (that must be from 0 to 100), calculate average marks
obtained by 10 students.

FOR Students ¬ 1 TO 10

REPEAT

INPUT “Enter marks”, Marks

IF Marks > 100 OR Marks < 0 THEN

OUTPUT “Wrong data, plz enter again:”

INPUT Marks

END IF

UNTIL Marks >= 0 AND Marks <=100

Sum ¬ Sum + Marks

END FOR

AVG ¬ Sum / 10

OUTPUT Avg

Students Marks Avg


1 90

Presence Check

REPEAT

INPUT “Plz enter a name: “, Name

UNTIL Name <> “ “


Array (Static Data Structure)
A group of variables of the same data type.

INPUT “Enter the size of Array”, Index

DECLARE Marks[10]: Integer

Marks[1]

Marks[2]

Marks[3]

Marks[4]

Marks[5]

..

..

Marks[25]

FOR Counter = 1 TO Index

INPUT Marks[Counter]

Total = Total + Marks[Count]

NEXT

OUTPUT Total

OUTPUT Marks[5], Marks[21]


Swapping values between Array variables

65, 55

Temp = 65

Marks[Count]= 55

Marks[Count+1] = 65

FOR Count ¬ 1 TO 15

IF Marks[Count] > Marks[Count+1] THEN

Temp ¬ Marks[Count]

Marks[Count] ¬ Marks[Count+1]

Marks[Count+1] ¬ Temp

NEXT
2-Dimension Array

Subject1 Subject2 Subject3


Student1 90 (1,1) 58(1,2) 39(1, 3)
Student2 80(2,1) 67(2,2) 85(2,3)
Student3 65 44 54
Student4 93 89 66
Student5 23 65 87

Total ¬ 0

Avg ¬ 0

Grand_Total ¬ 0

Agg_Avg ¬ 0

DECLARE Marks[1:5,1:3]: Integer

FOR Students ¬ 1 TO 5

Total ¬ 0

INPUT “Enter Student Name: “, StName

FOR Subjects ¬ 1 TO 3

INPUT “Enter Student Marks: “, Marks[Students, Subjects]

Total ¬ Total + Marks[Students, Subjects]

NEXT Subjects

Avg ¬ Total /3

OUTPUT Avg

Grand_Total ¬ Grand_Total + Total

NEXT Students

Agg_Avg ¬ Grand_Total /15


Marks[1,1]

Marks[1,2]

Marks[1,3]

Marks[2,1]
Stepwise refinement
Sub-Programs

1. Procedure
2. Function

Scope of Variable

1. Local
2. Global

DECLARE Num: Integer ¬ 53

Start Main Program

DECLARE Num1: Integer

Num1 ¬ 100

End Main Program

Procedure Abc

Num ¬ Num + 1

OUTPUT Num

End Procedure

Function Xyz

Num ¬ Num + 2

OUTPUT Num

End Function
PROCEDURE Addition(Num1: Real, Num2: Integer)

DECLARE Sum: Integer

Sum ¬ Num1 + Num2

OUTPUT “The total value is: “, Sum

OUTPUT New

END PROCEDURE

New ¬ 100

FUNCTION Square(Num: Integer) RETURNS Integer

Sq ¬ Num * Num

RETURN Sq

OUTPUT Sq, Sum

OUTPUT New

END FUNCTION

Main Program

INPUT X, Y

CALL Addition(X, Y)

Sq_Value ¬ CALL Square(20)

OUTPUT Sq_Value

OUTPUT New
Data Types and structure
Record Data Type

TYPE BooksType

DECLARE BookID: Integer

DECLARE BookTitle: String

DECLARE BookPrice: Currency

END TYPE

DECLARE BookArray[1:10]: BooksType

BookArray[2].BookPrice ¬ 1450000

Text File

Write / Append

Text File
Program Code

Read

Writing to a Text File

OPENFILE “Project.txt” FOR WRITE

WRITEFILE “Project.txt”, DataLine

CLOSEFILE “Project.txt”
Appending data to a Text File

OPENFILE “Project.txt” FOR APPEND

WRITEFILE “Project.txt”, DataLine

CLOSEFILE “Project.txt”

Reading from a Text File

OPENFILE “Project.txt” FOR READ

READFILE “Project.txt”, DataLine

CLOSEFILE “Project.txt”

Q3. (c) (i)

TYPE UserRecord

DECLARE UserID: String

DECLARE UserPassword: String

END TYPE

(ii)

DECLARE User[0:20] OF UserRecord


Built-in Functions

Length(“Jamal”,3) = Jam

Right(“Jamal”, 3) = mal

Mid(“Jamal”, 2, 4) = amal

UCASE(“Abdullah”) = ABDULLAH

LCASE(“Javed”) = javed

CInt(“23”) = 23

CInt(23.6) = 24

Passing parameter by value/by reference


By Value

PROCEDURE Square(By Value Num1: Integer, By Value Num2: Integer)

Num1 ¬ Num1 * Num1

Num2 ¬ Num2 * Num2

OUTPUT Num1, Num2

END PROCEDURE

INPUT A, B

CALL Square(A, B)

OUTPUT A, B = Same as previous

By Reference
PROCEDURE Square(By Reference Num1: Integer, By Reference Num2: Integer)

Num1 ¬ Num1 * Num1

Num2 ¬ Num2 * Num2

OUTPUT Num1, Num2

END PROCEDURE

INPUT A, B

CALL Square(A, B)

OUTPUT A, B = updated value

Exam style Questions:

Q2.

// Sample UserID = ASD8765

INPUT UserID

IF LEFT(UserID, 3) > = “A” AND LEFT(UserID, 3) <=”Z” THEN

IF RIGHT(UserID, 4) >=0 AND RIGHT(UserID, 4) <=9999 THEN

OUTPUT “Input is valid”

ELSE

OUTPUT “There are not 4 digits used.”

END IF

ELSE

OUTPUT “There are not 3 letters or letters are not in uppercase”

END IF

IF ASC(LEFT(UserID, 1)) >= 65 AND <=90


Ch. 14 Exam style Questions

Q1.

OUTPUT “Ounces Grams”

FOR Ounces ¬ 1 TO 30

Grams ¬ Round(Ounces * 28.35)

OUTPUT Grams, Ounces

END FOR

Q2.

INPUT “Plz enter a User ID: “, UserID

IF LENGTH(UserID) = 7 THEN

IF UPPER(LEFT(UserID, 3)) > “A” AND UPPER(LEFT(UserID, 3)) < “Z” THEN

IF RIGHT(UserID, 4) > 0000 AND RIGHT(UserID, 4) < 9999 THEN

OUTPUT “Valid User ID”

ELSE

OUTPUT “Last Four digits are not Numbers”

END IF

ELSE

OUTPUT “First 3 Characters are not Letters or in Uppercase”

END IF

ELSE

OUTPUT “String is not 7 characters long”

END IF

Q3.
PROCEDURE OutputTimesTable(n : Integer)

FOR Count ¬ 1 TO 10

OUTPUT Count, “ x ”, n,” = ” , count * n

ENDFOR

END PROCEDURE

INPUT “Enter a number: “, Num

CALL OutputTimesTable(Num)

Q4.

FUNCTION IsDivisibleBy(x: Integer, y : Integer) RETURNS Boolean

DECLARE Result: Boolean ¬ False

IF x MOD y = 0 THEN

Result ¬ True

ELSE

Result ¬ False

END IF

RETURN Result

END FUNCTION

Main Program

INPUT a, b

Answer ¬ CALL IsDivisible(a, b)

OUTPUT Answer

Q6.

FUNCTION CheckRegistration(RegNo: Integer)

Valid ¬ False
IF LENGTH(RegNo) = 7 THEN

IF UPPER(LEFT(RegNo, 2)) > “A” AND UPPER(LEFT(RegNo, 2)) < “Z” OR

IF UPPER(LEFT(RegNo, 3)) > “A” AND UPPER(LEFT(RegNo, 3)) < “Z” THEN

IF UPPER(RIGHT(RegNo, 2)) > “A” AND UPPER(RIGHT(RegNo, 2)) < “Z” OR

IF UPPER(RIGHT(RegNo, 3)) > “A” AND UPPER(RIGHT(RegNo, 3)) < “Z”


THEN

IF MID(RegNo, 3,2) > 0 AND MID(RegNo, 3,2) < 99 OR

IF MID(RegNo, 4,2) > 0 AND MID(RegNo, 4,2) < 99 THEN

Valid ¬ True

RETURN Valid

ELSE

RETURN Valid

END IF

END IF

END IF

END IF

END IF

END IF

END IF
(Software Development)

System/Program Development Life Cycle

1. Problem identification/definition
2. Analysis
3. Design
4. Development
5. Testing
6. Implementation
7. Maintenance

Data Testing

1. Alpha Testing
2. Beta Testing

1. Alpha Testing
a. White-box testing (With source code) Dry running, Stub testing
b. Black-box testing (Without source code)
i. Normal testing 10, 50
ii. Abnormal testing (Erroneous testing) -234, 7654345, abc
iii. Extreme (border/margin testing) 0, -1, 100

Data range: 0 TO 100

Types of errors

1. Syntax error e.g. IF..THN..ELSE..ENDIF


2. Logic error e.g. Adding instead of multiplying
3. Run-time (Execution) error e.g. Device/File not ready
Maintenance

1. Corrective
2. Perfective
3. Adaptive

Structure Charts

State-transition diagram Press up

Start

ON OFF Press up
Press down

Dry running/Tracing a program


Press down

Initial State Input Final State


ON Press up OFF
OFF Press up OFF
OFF Press Down ON
ON Press Down ON
Searching Methods
1. Serial search (Linear)
2. Binary Search

INPUT Index

DECLARE List[Index]: Integer

Search number: 40

1. 6
2. 9
3. 12
4. 34
5. 39
6. 65
7. 97
8. 120
9. 233

INPUT Search_Number

First ¬ 1

Last ¬ Max

Found ¬ False

EndList ¬ False

WHILE Found = False AND EndList = False

Mid ¬ (First+Last)/2

IF List[Mid] = Search_Number THEN

Found ¬ True

ELSE

IF First > = Last THEN


EndList ¬ True

ELSE

IF Search_Number > List[Mid] THEN

First ¬ Mid + 1

ELSE

Last ¬ Mid – 1

END IF

END IF

END IF

END WHILE

IF Found = True THEN

OUTPUT “Number was matched”

ELSE

OUTPUT “Number not matched”

END IF
Object Oriented Programming (OOP) (Using Abstraction)
Adv. Of using Classes

1. Encapsulation
2. Reusability
3. Data Security

Parts of a Class

1. Attributes (Properties)
2. Methods (Procedures / Functions)

Defining a class

Class Product

PRIVATE IDNumber: Integer

PRIVATE Size: String

PRIVATE Price: Real

Defining Method of Class

PUBLIC PROCEDURE Update_Price(Unit_Price: Real)

Price ¬ Unit_Price

END PROCEDURE

End Class

Object of a Class

DECLARE Obj_Product: New Product(“00541”, 34.5)

Using a method (Execution)

CALL Obj_Product.Update_Price(100)
Features of a class
1. Inheritance
2. Polymorphism

Main / Super Class

Class Human

IQ: Real

Age: Integer

Eye_Colour: String

Overridable PROCEDURE Abc()

………

END PROCEDURE

End Class

Sub Class

Class Employee

Inherits Human

EmpID: Integer

Overrides PROCEDURE Abc()

………

END PROCEDURE

End Class
Pseudocode Java Visual Basic
DECLARE Num: Integer Int Num; DIM Num AS Integer
DECLARE Name: String String Name; DIM Name AS String
Count ¬ Count + 1 Count = Count + 1; Count = Count + 1
Count ++;
Total ¬ Total + Num Total = Total + Num; Total = Total + Num
OUTPUT Sum System.out.print(Sum); Console.writeline(Sum)
IF A > B THEN If (A > B); IF A > B THEN
OUTPUT “A is Bigger” { Console.Writeline(“A is
END IF system.out.println(“A is Bigger”
Bigger”); END IF
}
IF A > B THEN If (A > B); IF A > B THEN
OUTPUT “A is Bigger” { Console.Writeline(“A is
ELSE system.out.println(“A is Bigger”
OUTPUT “A is Bigger”; ELSE
smaller” } Console.writeline(“A is
END IF Else; smaller”
{ END IF
system.out.println(“A is
Smaller”;
}

FOR Count ¬ 1 TO 10 For(Count = 1; Count<=10; FOR Count = 1 TO 10


OUTPUT Count Count++) Console.Writeline(Count)
END FOR { NEXT
System.out.println(Count);
}
WHILE A <=10 WHILE (A<=10) WHILE A <=10
A¬A+1 { A = A +1
OUTPUT A A++ Console.Writeline(A)
ENDWHILE system.out.println(A) ENDWHILE
}

You might also like