Command Group: Variables, Constants & Arrays Command DIM Purpose Declares a variable Syntax DIM <variable_name> AS <data_type> [= <value>] Example Dim StudentAge As Integer or Dim Num As Integer = 45 Command Const Purpose Declares a constant Syntax Const <constant_name> = <value> Example Const MAX = 25 Command DIM Purpose Declares an array of variables. Syntax Dim <array_name>(dimension1[,dimension2]) As <data_type> Example Dim Students(20) As String Dim Temperature(1,10) As Single
Command Group: Decisions
Command Single Line IF…THEN…ELSE Purpose Provides decision making ability based on the Boolean condition. Syntax If <condition> Then <Statement> Else <Statement> Example If age >= 5 Then Admission = True Else Admission = False Command Block IF…ENDIF Purpose Provides decision making ability based on the Boolean condition with multiple statements. Syntax If <condition> Then <statements> [Else <statements>] End If Example If age >= 5 Then If num = 10 then Admission = True Num = num + 1 Fee = True End If Else Admission = False End If Command IF…ELSEIF…END IF Purpose Provides decision making ability based on multiple Boolean conditions with multiple statements. Syntax If <condition> Then <statements> [Elseif <condition> Then <statements> … Else <statements>] End If Example If average >= 90 Then grade = “Distinction” Elseif average >= 70 And average <= 89.9 Then grade = “Excellent” Elseif average >= 40 And Average <= 69.9 Then grade = “Pass” Else grade = “Ungraded” End If Command SELECT…END SELECT Purpose Provides decision making based on an expression with multiple statements. Syntax Select Case <expression> Case <value> <statements> [Case <value> <statements> Case Else <statements>] End Select Example Select Case average Case Is > 90 Grade = “Distinction” Case 70 to 89.9 Grade = “Excellent” Case 40 to 69.9 Grade = “Pass” Case Else Grade = “Ungraded” End Select
Command Group: Iteration (Loops)
Command FOR…NEXT Purpose Provides a loop based on the counter variable. Syntax For <variable> = <start_value> To <end_value> [Step <step_value>] <statements> Next Example For count = 1 to 10 Console.WriteLine(“Hello There!”) Next For count = 1 to 100 Step 2 Console.WriteLine(count) Next For count = 10 to 1 Step -1 Console.WriteLine(count) Next Command DO WHILE…LOOP Purpose Provides a Top-testing loop based on the Boolean condition and runs while condition remains True. Syntax Do While <condition> <statements> Loop Example answer = “Y” Do While answer = “Y” or answer = “y” Console.WriteLine(“Enter your choice.”) answer = Console.ReadLine Loop Command DO…LOOP WHILE Purpose Provides a Bottom-testing loop based on the Boolean condition and runs while condition remains True. Syntax Do <statements> Loop While <condition> Example Do Console.WriteLine(“Enter your choice.”) answer = Console.ReadLine Loop While answer = “Y” or answer = “y” Command DO UNTIL…LOOP Purpose Provides a Top-testing loop based on the Boolean condition and runs while condition remains False. Syntax Do Until <condition> <statements> Loop Example answer = “Y” Do Until answer = “N” or answer = “n” Console.WriteLine(“Enter your choice.”) answer = Console.ReadLine Loop
Command DO…LOOP UNTIL
Purpose Provides a Bottom-testing loop based on the Boolean condition and runs while condition remains False. Syntax Do <statements> Loop Until <condition> Example Do Console.WriteLine(“Enter your choice.”) answer = Console.ReadLine Loop While answer = “N” or answer = “n”
Command Group: Subroutines
Command Procedure Purpose Provides a subroutine that doesn’t return a value after completion. Syntax Sub <identifier>[([ByVal | ByRef] [Optional] <parameter_name> As <data_type>[,…])] End Sub Example Sub MyProc Console.WriteLine(“MyProc has run successfully.”) End Sub
Sub AddNums(ByVal Num1 As Integer, ByVal Num2 As Integer)
Console.WriteLine(“Sum of {0} and {1} is {2}.”,Num1, Num2, Num1+Num2) End Sub Command Function Purpose Provides a subroutine that returns a value after completion. Syntax Function <identifier>[([ByVal|ByRef] [Optional] <param_name> As <data_type>[,…])] As <return_type> Example Function Add(ByVal Num1 As Integer, ByVal Num2 As Integer) As Integer Return Num1+Num2 End Function Command Calling a Procedure Purpose Calls a procedure with or without parameters. Syntax Call <procedure_name> Example Call AddNum or Call AddNum(num1, num2) Command Calling a Function Purpose Calls a function with or without parameters. Example 1. sum = Add(10,20) 2. Console.WriteLine(Add(10,20)) (Note: function call within a function call) 3. sum = sum + Add(10,20) (Note: function Add will be executed first) 4. Do While AverageMarks(10,20,30) < 20 5. If Add(10,20) = 30 Then Command Group: Built-in Functions & Operators Command Len() Purpose Returns the length of a string. Syntax Len(<source_string>) Example Console.WriteLine(Len(“Pakistan”)) or s = Len(“Pakistan”) Command Left() Purpose Returns a specific number of characters from a source string. Syntax Left(<source_string>,<No_of_characters>) Example S = Left(“Pakistan”, 3) >>> This will return “Pak” Command Mid() Purpose Returns a specific number of characters from a source string starting from a specific position. Syntax Mid(<source_string>,<starting_position>,<no_of_characters>) Example S = Mid(“Pakistan”, 6, 3) >>> This will return “tan” Command Right() Purpose Returns a specific number of characters from a source string. Syntax Right(<source_string>, <no._of_characters>) Example S = Right(“Pakistan”,4) >>> This will return “stan” Command ASC() Purpose Returns the ASCII code of a character. Syntax ASC(<character>) Example C = ASC(“A”) >>> This will return 65 Command Chr() Purpose Returns the character representation of an ASCII code. Syntax Chr(<ASCII_code>) Example S = Chr(65) >>> This will return “A” Command MOD Purpose Arithmetic operator that returns the remainder of a division. Syntax <variable1> MOD <variable2> Example C = 5 MOD 2 >>> This will return 1 Command RND() Purpose Returns a random number between 0 and 1. Syntax RND() * <number> Example C = INT(RND() * 5) + 1 >>> This will return a random integer between 0 and 5 Command INT() Purpose Returns the integer portion of real number. Syntax INT(<number>) Example C = INT(345.88) >>> This will return 345 Command ROUND() Purpose Rounds off a real number, optionally to the number of digits. Syntax Round(<real_number>[,<no_of_digits>]) Example C = Round(345.6) >>> 346 or c = Round(345.678,2) >>> 345.68 Command Format() Purpose Formats source data as the given format. For details of formats, see the VB reference manual. Syntax Format(<source>, <format>) Example Format(time(), “HH:MM:ss”) >>> This will return 14:30:45 (24 hour time format)