Quick Review Guide For Programming
Quick Review Guide For Programming
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 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
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.
In each function below, if the function call is not properly formed, the function returns an error.
returns the integer value representing the remainder when ThisNum is divided by ThisDiv.
Example: MOD(10, 3) returns 1
returns the integer value representing the whole number part of the result when ThisNum is divided by
ThisDiv.
Example: DIV(10, 3) returns 3
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
End Module
PSEUDOCODE/PROGRAMMING STRUCTURES
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
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
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”
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
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.
Syntax
WHILE <condition> DO
<statement>
<statement>
<statement>
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
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
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
Console.ReadLine()
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
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
Any pseudocode can be simply converted into a Visual Basic Program if we only replace the
pseudocode words with the VB commands.
ARRAY
1D ARRAY
Structure of a One-dimensional Array Variable:
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.
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
2D ARRAY
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
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.
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
LINEAR SEARCH
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()
End If
Console.ReadLine()
End Sub
End Module
Module Module1
Sub Main()
Dim random As New Random()
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
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.
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
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
Module Module1
Sub Main()
Dim path As String = "D:\AhmedThakur.txt"
FileAppend.WriteLine(nam)
End Using
Console.ReadLine()
End Sub
End Module
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.ReadLine()
End Sub
Sub createfile()
'=========================== CREATE FILE
For a = 1 To 3
Console.Write("Enter Member's Name: ")
memName(a) = Console.ReadLine()
End Sub
Sub appendfile()
'========== APPEND DATA
For a = 1 To 3
Console.Write("Enter Member's Name: ")
memName(a) = Console.ReadLine()
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