CHEAT SHEET (Copy)
1. 🔹 Basic Program Structure
Module Module1
Sub Main()
' Your code goes here
End Sub
End Module
2. 🔹 Output to Console
Console.Write("Text") ' Stays on same line
Console.WriteLine("Text") ' Moves to next line
3. 🔹 Input from Console
Dim userInput As String
userInput = Console.ReadLine()
To convert input to numeric types:
Dim number As Integer
number = Convert.ToInt32(Console.ReadLine())
Dim realNumber As Double
realNumber = Convert.ToDouble(Console.ReadLine())
4. 🔹 Variable Declaration and Data Types
Integer
Dim x As Integer
x = 10
Real (Float/Double)
Dim y As Double
y = 3.14
String
Dim name As String
name = "Alice"
Boolean
Dim flag As Boolean
flag = True
5. 🔹 Constants
Const PI As Double = 3.14159
6. 🔹 Operators
Arithmetic:
+ ' Addition
- ' Subtraction
* ' Multiplication
/ ' Division (result is Double)
' Integer division
Mod ' Modulus
^ ' Power
Comparison:
= '<equals>
<> '<not equals>
> '<greater than>
< '<less than>
>= '<greater than or equal>
<= '<less than or equal>
Logical:
And
Or
Not
7. 🔹 Conditional Statements
IF-ELSEIF-ELSE:
If condition Then
' statements
ElseIf anotherCondition Then
' statements
Else
' statements
End If
Nested IF:
If a > 0 Then
If b > 0 Then
Console.WriteLine("Both positive")
End If
End If
SELECT CASE:
Select Case grade
Case "A"
Console.WriteLine("Excellent")
Case "B", "C"
Console.WriteLine("Good")
Case Else
Console.WriteLine("Fail")
End Select
8. 🔹 Loops
FOR loop:
For i As Integer = 1 To 10
Console.WriteLine(i)
Next
With step:
For i As Integer = 10 To 1 Step -1
Console.WriteLine(i)
Next
WHILE loop:
While condition
' statements
End While
DO WHILE loop:
Do While condition
' statements
Loop
DO UNTIL loop:
Do Until condition
' statements
Loop
9. 🔹 Functions and Procedures
Function with Return:
Function Square(num As Integer) As Integer
Return num * num
End Function
Sub Procedure (No Return):
Sub Greet(name As String)
Console.WriteLine("Hello " & name)
End Sub
Calling:
Greet("Hamza")
Dim result As Integer = Square(5)
10. 🔹 Arrays
1D Array:
Dim numbers(4) As Integer ' Index from 0 to 4
numbers(0) = 10
Console.WriteLine(numbers(0))
2D Array:
Dim grid(2, 2) As Integer
grid(0, 0) = 5
Console.WriteLine(grid(0, 0))
Loop through 1D array:
For i As Integer = 0 To numbers.Length - 1
Console.WriteLine(numbers(i))
Next
11. 🔹 String Manipulation
Dim s As String = "Hello"
s.Length ' Returns number of characters
s.ToUpper() ' HELLO
s.ToLower() ' hello
s.Substring(0, 2) ' He
s.IndexOf("e") ' 1
s.Contains("el") ' True
s = s & " World" ' Hello World
12. 🔹 File Handling (Basic Level)
Writing to File:
My.Computer.FileSystem.WriteAllText("file.txt", "Hello World", False)
Reading from File:
Dim content As String
content = My.Computer.FileSystem.ReadAllText("file.txt")
13. 🔹 Input Validation Example
Dim age As Integer
Dim valid As Boolean = False
While Not valid
Console.Write("Enter age: ")
If Integer.TryParse(Console.ReadLine(), age) Then
valid = True
Else
Console.WriteLine("Invalid input. Try again.")
End If
End While
14. 🔹 Error Handling (Basic Try-Catch)
Try
Dim x As Integer = Convert.ToInt32(Console.ReadLine())
Catch ex As Exception
Console.WriteLine("Invalid input!")
End Try
15. 🔹 Comments
' This is a single-line comment
16. 🔹 Mathematical Functions
Math.Sqrt(25) ' 5
Math.Pow(2, 3) ' 8
Math.Abs(-10) ' 10
Math.Round(3.14159, 2) ' 3.14
✅ Exam-Oriented Tips
| Concept | Pseudocode | Visual Basic Equivalent |
|---|---|---|
x ← 10 |
← | x = 10 |
REPEAT UNTIL loop |
Repeat | Do...Loop Until condition |
FOR i ← 1 TO N |
For loop | For i As Integer = 1 To N |
PROCEDURE name() |
Procedure | Sub name() |
FUNCTION name(): INT |
Function | Function name() As Integer |
OUTPUT x |
Output | Console.WriteLine(x) |
INPUT x |
Input | x = Console.ReadLine() → Convert as needed |
