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

Quick Review Guide For Programming

This document provides a quick review guide of key programming concepts such as variables, data types, functions, procedures, and built-in functions. It defines terms like statements, lines, variables, constants, and identifiers. The document also lists common functions and their usage in pseudocode and Visual Basic examples to demonstrate programming fundamentals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Quick Review Guide For Programming

This document provides a quick review guide of key programming concepts such as variables, data types, functions, procedures, and built-in functions. It defines terms like statements, lines, variables, constants, and identifiers. The document also lists common functions and their usage in pseudocode and Visual Basic examples to demonstrate programming fundamentals.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

Statement: A program statement is a single instruction composed of its reserved words.

Line: In some languages, such as BASIC or COBOL, a program is divided up into numbered lines.
Each line has a line number & contains one or more statements.

Variable: A variable is a quantity named in a program & whose value can change.

Constant: A constant is a value that doesn’t change. In a program it may or may not be given a
name.

Identifier: An identifier is a name invented by the programmer for some data. An identifier can be a
name for a variable, a constant, a file, an array, etc.

Reserved Words: A reserved word is a name that has some special significance to the compiler or
interpreter. The programmer can only use it for its special purpose & can’t use it as an identifier.

Expression: An expression is a set of variables, constants & operators (such as +, -, etc) that is to be
evaluated by the computer.

Declaration: In computer programming, a declaration specifies properties of an identifier: it declares


what a word (identifier) means. Declarations are most commonly used for functions, variables,
constants, and classes.

Declaration of a variable serves two purposes: It associates a type and an identifier (or name) with
the variable. The type allows the compiler to interpret statements correctly.
Example: DIM num as integer

Initialization: In computer programming, initialization is the assignment of an initial value for a data
object or variable. The manner in which initialization is performed depends on programming
language, as well as type, storage class, etc., of an object to be initialized.
Example: x = 2

DATA EXAMPLE EXAMPLE


DESCRIPTION
TYPE PSEUDOCODE VB PROGRAM
Integer Numeric – Whole Number Set num to 0/integer Dim num as integer
Real Numeric – Decimal/Fraction Set per to real Dim per as single
Char Alphanumeric – single character set choice to char Dim choice as char
String Alphanumeric – multiple characters Set name to string Dim name as string
Boolean Logical – Single Byte (1/0), (Y/N), (T/F) Set confirm to boolean Dim confirm as boolean

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 1
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

Procedure: In computer programming, a procedure is an independent code module that fulfills


some concrete task and is referenced within a larger body of source code. This kind of code item
can also be called a function or a sub-routine. The fundamental role of a procedure is to offer a
single point of reference for some small goal or task that the developer or programmer can trigger
by invoking the procedure itself.

A procedure may also be referred to as a function, subroutine, routine, method or subprogram.

Function: a function is a type of procedure or routine. Some programming languages make a


distinction between:
 a function – which returns a value, and
 a procedure – which performs some operation but does not return a value.

Most programming languages come with a prewritten set of functions that are kept in a library.

The purpose of a function is to take in a number of values or arguments, do some calculations with
those arguments and then return a single result.

Sub-routine: In computer programming, a subroutine is a sequence of program instructions that


perform a specific task, packaged as a unit. This unit can then be used in programs wherever that
particular task should be performed. Subprograms may be defined within programs, or separately
in libraries that can be used by multiple programs. In different programming languages, a subroutine
may be called a procedure, a function, a routine, a method, or a subprogram.

Common Functions to be studied


FUNCTION DESCRIPTION EXAMPLE IN
VB(CONSOLE MODE)
LEN Returns the length of a string LEN(“THAKUR”) 6
Returns ASCII Code of a
ASCII ASCII(“a) 97
character
Returns n number of characters
LEFT LEFT(“PAKISTAN”,4) PAKI
from left
Returns n number of characters
RIGHT RIGHT(“PAKISTAN”, 4) STAN
from right
Returns n number of characters
MID MID(“PAKISTAN”, 4,2) IS
from a n character in a string
Returns the character of ASCII
CHR CHR(65) A
Code
Converts to lowercase(small)
LCASE LCASE(“AHMED”) ahmed
letters
Converts to uppercase
UCASE UCASE(“ahmed”) AHMED
(capital) letters
Trims the empty spaces at left
TRIM TRIM(“ PAKISTAN ”) PAKISTAN
and right from string
Trims the empty spaces of at
LTRIM LTRIM(“ PAKISTAN “) PAKISTAN .
left from string
Trims the empty spaces of at
RTRIM RTRIM(“ PAKISTAN ”) PAKISTAN
right from string

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 2
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

Built-in functions (Pseudocode)

In each function below, if the function call is not properly formed, the function returns an error.

MID(ThisString : STRING, x : INTEGER, y : INTEGER) RETURNS STRING

returns the string of length y starting at position x from ThisString


Example: MID("ABCDEFGH", 2, 3) will return string "BCD"

LEFT(ThisString : STRING, x : INTEGER) RETURNS STRING

returns the leftmost x characters from ThisString


Example: LEFT("ABCDEFGH", 3) will return string "ABC"

RIGHT(ThisString: STRING, x : INTEGER) RETURNS STRING

returns the rightmost x characters from ThisString


Example: RIGHT("ABCDEFGH", 3) will return string "FGH"

CHR(x : INTEGER) RETURNS CHAR

returns the character whose ASCII value is x


Example: CHR(87) will return 'W'

ASC(x : CHAR) RETURNS INTEGER

returns the ASCII value of character x


Example: ASC('W') will return 87

LCASE(x : CHAR) RETURNS CHAR

returns the lower case equivalent character of x


Example: LCASE('W') will return 'w'

UCASE(x : CHAR) RETURNS CHAR

returns the upper case equivalent character of x


Example: UCASE('h') will return 'H'

INT(x : REAL) RETURNS INTEGER

returns the integer part of x


Example: INT(27.5415) will return 27

LENGTH(ThisString : STRING) RETURNS INTEGER

returns the integer value representing the length of string ThisString


Example: LENGTH(“Happy Days”.5415) returns 10

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 3
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

MOD(ThisNum : INTEGER, ThisDiv : INTEGER) RETURNS INTEGER

returns the integer value representing the remainder when ThisNum is divided by ThisDiv.
Example: MOD(10, 3) returns 1

DIV(ThisNum : INTEGER, ThisDiv : INTEGER) RETURNS INTEGER

returns the integer value representing the whole number part of the result when ThisNum is divided by
ThisDiv.
Example: DIV(10, 3) returns 3

USER DEFINED FUNCTIONS


Programming extends the limits to create any user-defined function if it is not present in the built-in
library.

Example: Program to create function to produce square and cube of an input value.
Module Module1

Sub Main()
Dim num, sqr, cub As Integer
num = Console.ReadLine
sqr = square(num)
cub = cube(num)
Console.WriteLine("Square : " & sqr)
Console.WriteLine("Cube : " & cub)
Console.ReadLine()
End Sub

Function square(ByVal num1 As Integer) As Integer


Dim x As Integer
x = num1 * num1
Return x
End Function

Function cube(ByVal num1 As Integer) As Integer


Dim x As Integer
x = num1 * num1 * num1
Return x
End Function

End Module

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 4
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

PSEUDOCODE/PROGRAMMING STRUCTURES

IF … THEN … ELSE … ENDIF


It is used to check only one condition and act accordingly.

Syntax
IF <condition> THEN
<statement if condition is TRUE>
ELSE
<statement if condition is FALSE>
ENDIF

Example: Algorithm to check whether the input value is greater than100 and output the message
accordingly.

SET number to 0
INPUT number
IF number > 100 THEN
OUTPUT “The entered number is greater than 100”
ELSE
OUTPUT “The entered number is smaller than 100”
ENDIF
END

Visual Basic 2010 (Console Mode) Program:

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 5
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

IF…THEN…ELSEIF…ELSE…ENDIF
This is used to check multiple conditions.

 Each ELSEIF works like IF, but must be used within the main structure of IF…ENDIF.
 There is no limit of ELSEIFs
 If any of the ELSEIF’s condition is met, the rest of the ELSEIFs are not checked.

Syntax
IF <condition> THEN
<statement if condition is TRUE>
ELSEIF <condition> THEN
<statement if condition is TRUE>
ELSEIF <condition> THEN
<statement if condition is TRUE>
ELSEIF <condition> THEN
<statement if condition is TRUE>
ELSEIF <condition> THEN
<statement if condition is TRUE>
.
.
.
ELSE
<statement if condition is FALSE>
ENDIF

SET eng, comp, isl, math, phy, chem, tot, per to 0


SET gr to “ ”
INPUT eng, comp, isl, math, phy, chem
Tot = eng + comp + isl + math + phy + chem
Per = (Tot/375)* 100

IF per >= 90THEN


Gr = “A+”
ELSEIF per >= 80 AND per < 90 THEN
Gr = “A”
ELSEIF per >= 70 AND per < 80 THEN
Gr = “B”
ELSEIF per >= 60 AND per < 70 THEN
Gr = “C”
ELSEIF per >= 50 AND per < 60 THEN
Gr = “D”
ELSE
Gr = “U”
ENDIF

OUTPUT tot, per, gr


END

Visual Basic 2010 (Console Mode) Program:

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 6
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 7
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

CASE … OF … OTHERWISE … ENDCASE


CASE is used to match multiple options in one condition.

Syntax
CASE <identifier (variable) OF
<constant>,<constant>,<constant> : <statement>
<constant>,<constant>,<constant> : <statement>
<constant>,<constant>,<constant> : <statement>
<constant>,<constant>,<constant> : <statement>
OTHERWISE
<constant> : <statement if identifier/variable is not matched>
ENDCASE

EXAMPLE
1. input a speed
2. whole = speed/100
3. case whole of
4. 0,1,2 : result = slow
5. 3, 4, 5, 6 : result = normal
6. 7, 8, 9 : result = high
7. otherwise whole = -1
8. endcase
9. if whole = -1 then
10. output “abnormal reading”
11. else output result, “speed”

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 8
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

ITERATION

WORD/PHRASE DESCRIPTION
Loop – (Unconditional) to repeat some task(s) a specific
FOR….TO….NEXT
number of times.
Loop – (Conditional) to repeat some task(s) till a condition
WHILE…DO…ENDWHILE
remains TRUE
Loop – (Conditional) to repeat some task(s) till a condition
REPEAT…UNTIL
remains FALSE

Loop: Repetition of task is called loop/iteration.

There are two basic types of loops:


 Unconditional Loop (FOR – TO – NEXT)
 Conditional Loop (WHILE – DO – ENDWHILE) and (REPEAT – UNTIL)

Unconditional Loop: Unconditional loop repeats the tasks unconditionally the specified number of
times.

FOR … TO … NEXT

Syntax
FOR <identifier(variable> = <number> TO <number>
<statement>
<statement>
<statement>
NEXT

Example:
Algorithm to output name 10 times. Algorithm to output numbers from 1- 10.
SET a to 0 SET a to 0
FOR a = 1 TO 10 FOR a = 1 TO 10
OUTPUT “Ahmed Thakur” OUTPUT a
NEXT NEXT
END END

Program

WHILE … DO … ENDWHILE
 It is a conditional loop.

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 9
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

 Loop continues till the condition remains TRUE.


 Condition is check first and the process comes next to it.

Syntax
WHILE <condition> DO
<statement>
<statement>
<statement>

<identifier> = <identifier> + <number>


ENDWHILE

Example:
Algorithm to output name 10 times. Algorithm to output numbers from 1- 10.
SET a to 0 SET a to 0
WHILE a < 10 DO WHILE a < 10 DO
a = a + 1 a = a + 1
Output “Ahmed Thakur” OUTPUT a
ENDWHILE ENDWHILE
END END

Program

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 10
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

REPEAT… UNTIL
 It is a conditional loop.
 Loop continues till the condition remains FALSE.
 The process comes first, condition is checked next.

Syntax
REPEAT
<statement>
<statement>
<statement>
<identifier> = <identifier> + <number>
UNTIL <condition>

Example:
Algorithm to output name 10 times. Algorithm to output numbers from 1- 10.
SET a to 0 SET a to 0
REPAT REPAT
a = a + 1 a = a + 1
Output “Ahmed Thakur” Output a
UNTIL a = 10 UNTIL a = 10
END END

Program

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 11
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

COUNTING
Counting in 1s is quite simple; use of the statement count = count + 1 will enable counting to
be done (e.g. in controlling a repeat loop). The statement literally means:
the (new) count = the (old) count + 1

It is possible to count in any increments just by altering the numerical value in the statement (e.g.
count = count – 1 counts backwards)

Example: Algorithm to count total number of positive and negative entries in 100 inputs.

Algorithm:
SET num, pos, neg to 0
FOR a = 1 TO 100
INPUT num
IF num >= 0 THEN
pos = pos + 1
ELSE
neg = neg + 1
ENDIF
NEXT
OUPUT pos, neg
END

Program in Visual Basic 2010 (Console Mode)


Dim num, pos, neg As Integer
For a = 1 To 100
num = Console.ReadLine
If num >= 0 Then
pos = pos + 1
Else
neg = neg + 1
End If
Next
Console.WriteLine(pos & ", " & neg)

Console.ReadLine()

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 12
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

TOTALING
To add up a series numbers the following type of statement should be used:
total = total + number
This literally means (new) total = (old) total + value of number

Example: June 2014, Paper 12 – Q5


The following algorithm should:
 input ten numbers
 output the largest number input
 output the average value of the input data

Algorithm
10 largest = 0
20 sum = 0, num = 0
30 FOR x = 1 TO 10
40 INPUT num
50 IF num > largest THEN largest = num
60 OUTPUT largest
70 sum = sum + num
80 NEXT x
90 average = sum / 10
100 output average

Program in Visual Basic 2010 (Console Mode)

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 13
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

PROGRAMMING IN VISUAL BASIC 2010 (CONSOLE MODE)

Any pseudocode can be simply converted into a Visual Basic Program if we only replace the
pseudocode words with the VB commands.

PSEUDOCODE VB COMMAND EXAMPLE


Dim num As Integer
SET/DECLARE/INITIALIZE DIM
Dim name As String
INPUT/READ Console.readline() num = Console.ReadLine()
OUTPUT/PRINT Console.writeline Console.WriteLine(num)
IF-ELSE-ENDIF (same as pseudocode)
FOR-TO-NEXT (same as pseudocode)
WHILE-DO-ENDWHILE WHILE – END WHILE
REPEAT – UNTIL DO – LOOP UNTIL
Rest all the pseudocodes are same in VB Programming

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 14
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

ARRAY

1D ARRAY
Structure of a One-dimensional Array Variable:

Array variable: num(4)


Variable: num
Array: 4
Indexes: 0 till 4 (total 5)

Index: 0
Index: 1
Index: 2
Index: 3
Index: 4

To call a particular element we need to call variable with its index number i.e.

num(0) or num(1) or num(2) etc.


OR
Input num(0) or input num(1) or input num(2) etc

To get array variables such as We may alternatively use:

Input num(1) num[1:10]


Input num(2) FOR a = 1 to 10
Input num(3) Input num(a)
Input num(4) NEXT
Input num(5)
Input num(6) To call a particular variable we
Input num(7) will use its index such as:
Input num(8)
Input num(9) OUTPUT num(2), num(3), num(10)
Input num(10)
This techniques is even convenient for multiple variables of same attribute.

Array is used in two ways:


1. Declaration
2. Manipulation (Initialization/input/calculations/output)

1. Array Declaration:
Pseudocode: Set num[1:5] OR num[1:5] 1 is for dimension
VB 2010 Program: Dim num(5) as integer 5 is number of values

2. Use/Manipulation:
FOR a = 1 to 5
Input num(a)
NEXT

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 15
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

2D ARRAY

To get array variables such as We may alternatively use:

Input num(1,1) Set num[1:3, 1:5]


Input num(1,2) FOR a = 1 to 3
Input num(1,3) FOR b = 1 to 5
Input num(1,4) Input num(a,b)
Input num(1,5) NEXT
NEXT
Input num(2,1)
Input num(2,2) Num(a,b) = Num(row,column)
Input num(2,3)
Input num(2,4)
Input num(2,5)

Input num(3,1)
Input num(3,2)
Input num(3,3)
Input num(3,4)
Input num(3,5)

Example
COLUMNS

0 1 2 3 4 5

0
R
O 1 1,1 1,2 1,3 1,4 1,5
W 2 2,1 2,2 2,3 2,4 2,5
S
3 3,1 3,2 3,3 3,4 3,5

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 16
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

BUBBLE SORT

Note: Visual Basic can compare string as well as numeric; using their ASCII Codes.

Bubble sort technique keeps comparing two serial values and swapping, them until sorting is done.

Program to Input 5 Names in a 1D Array and sort them.


Module Module1
Sub Main()
Dim nam(5), temp As String
Dim n, i As Integer
Dim s As Boolean
For a = 1 To 5
Console.Write("Enter name " & a & " : ")
nam(a) = Console.ReadLine()
Next

While s = False
s = True
i = 1
While i < 5
If nam(i) > nam(i + 1) Then
temp = nam(i)
nam(i) = nam(i + 1)
nam(i + 1) = temp
s = False
End If
i = i + 1
End While
End While

For b = 1 To 5
Console.WriteLine(nam(b))
Next
Console.ReadLine()
End Sub
End Module

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 17
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

LINEAR SEARCH

Program to input 5 numbers in a 1D Array and search a value in them.

Module Module1

Sub Main()
Dim num(5), srch As Integer
Dim x As Integer
Dim found As Boolean

For a = 1 To 5
num(a) = Console.ReadLine()
Next
srch = Console.ReadLine()

While x < 5 And found = False


x = x + 1
If srch = num(x) Then
found = True
End If
End While

If found = False Then


Console.WriteLine("Value not found!")
Else
Console.WriteLine("Search value found!")
Console.WriteLine(x)

End If
Console.ReadLine()
End Sub

End Module

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 18
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

FLIP VALUES IN 2D ARRAY

Module Module1

Sub Main()
Dim random As New Random()

Dim box(5, 8) As String


For r = 1 To 5
For c = 1 To 8
box(r, c) = Chr(random.Next(65, 70))
'box(r, c) = Chr(Rnd() * 90)
Next
Next

For x = 1 To 5
For y = 1 To 8
Console.Write(box(x, y) & " ")
Next
Console.WriteLine()
Next

Console.WriteLine()
Console.WriteLine()

For a = 1 To 5
For b = 8 To 1 Step -1
Console.Write(box(a, b) & " ")
Next
Console.WriteLine()
Next

Console.ReadLine()
End Sub

End Module

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 19
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

FILE HANDLING

File Handling is part of programming in which data produced as output can be stored in TEXT (.txt)
file using operating system libraries.

CREATE FILE AND STORE DATA IN FILE


Imports System
Imports System.IO

Module Module1

Sub Main()
Dim path As String = "D:\AhmedThakur.txt"
Dim nam As String
Using FileCreate As StreamWriter = File.CreateText(path)
Console.WriteLine("ENTER NAME TO STORE IN FILE")
nam = Console.ReadLine()
FileCreate.WriteLine(nam)
End Using
Console.ReadLine()
End Sub

End Module

READ AND DISPLAY FILE CONTENTS ON SCREEN


Imports System
Imports System.IO

Module Module1

Sub Main()
Dim path As String = "D:\AhmedThakur.txt"
For Each Line As String In File.ReadLines(path)
Console.WriteLine(Line)
Next
Console.ReadLine()
End Sub

End Module

APPEND DATA IN EXISTING FILE


Imports System
Imports System.IO

Module Module1

Sub Main()
Dim path As String = "D:\AhmedThakur.txt"

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 20
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

Dim nam As String


Using FileAppend As StreamWriter = File.AppendText(path)
Console.WriteLine("ENTER NAME TO STORE IN FILE")
nam = Console.ReadLine()

FileAppend.WriteLine(nam)
End Using
Console.ReadLine()
End Sub

End Module

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 21
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

JUNE 2017 PRE-RELEASE MATERIAL TASK 4 (PROGRAM)

Imports System
Imports System.IO

Module Module1
Dim path As String = "D:\MembersData.txt"
Dim srch As String
Dim memID(10), memName(10) As String
Dim x As Integer
Dim fl As Boolean

Sub Main()
Dim ch As Integer
Dim stch As String

Console.Clear()
Console.WriteLine("PRESS 1 TO CREATE FILE")
Console.WriteLine("PRESS 2 TO APPEND DATA IN FILE")
Console.WriteLine("PRESS 3 TO READ WHOLE FILE")
Console.WriteLine("PRESS 4 TO SEARCH IN FILE")
Console.WriteLine("PRESS 0 TO EXIT PROGRAM")
Console.Write("ENTER YOUR CHOICE 0 - 4: ")

Do
ch = Console.ReadLine()
Loop Until ch >= 0 And ch <= 4

If ch = 0 Then
End
ElseIf ch = 1 Then
Call createfile()

ElseIf ch = 2 Then
Call appendfile()

ElseIf ch = 3 Then
Call readfile()

ElseIf ch = 4 Then
Call searchfile()
End If

Console.Write("Do you want to continue this program Y/N ")


stch = Console.ReadLine()

If stch = "Y" Or stch = "y" Then


Call Main()
End If

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 22
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

'Console.ReadLine()
End Sub

Sub createfile()
'=========================== CREATE FILE
For a = 1 To 3
Console.Write("Enter Member's Name: ")
memName(a) = Console.ReadLine()

Console.Write("Enter Member ID: ")


memID(a) = Console.ReadLine()
Next

Using fc As StreamWriter = File.CreateText(path)


For b = 1 To 3
fc.Write(memName(b))
x = 20 - Len(memName(b))
For sp = 1 To x
fc.Write(" ")
Next
fc.WriteLine(memID(b))
Next
End Using
Call Main()

End Sub

Sub appendfile()
'========== APPEND DATA
For a = 1 To 3
Console.Write("Enter Member's Name: ")
memName(a) = Console.ReadLine()

Console.Write("Enter Member ID: ")


memID(a) = Console.ReadLine()
Next

Using fc As StreamWriter = File.AppendText(path)


For b = 1 To 3
fc.Write(memName(b))
x = 20 - Len(memName(b))
For sp = 1 To x
fc.Write(" ")
Next
fc.WriteLine(memID(b))
Next
End Using
Call Main()
End Sub

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 23
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

Sub readfile()
'============ READ WHOLE FILE
Console.WriteLine("MEMBER NAME MEMBER ID")
For Each ln As String In File.ReadLines(Path)

Console.WriteLine(ln)
Next
Console.ReadLine()
Call Main()
End Sub

Sub searchfile()
'============SEARCH IN FILE
Console.Write("Enter Member ID to search: ")
srch = Console.ReadLine()
For Each ln As String In File.ReadLines(path)
If LCase(ln).Contains(LCase(srch)) Then
Console.WriteLine(Right(ln, 6))
fl = True
End If
Next
If fl = False Then
Console.WriteLine("Name not found!")
End If
Call Main()
End Sub

End Module

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 24
9608 [email protected], +92 300 8268885
COMPUTER SCIENCE – AS LEVEL AHMED THAKUR

QUICK REVIEW GUIDE FOR PROGRAMMING

COMPUTER SCIENCE https://www.facebook.com/computerscience.oalevel


Page 25
9608 [email protected], +92 300 8268885

You might also like