Example for ArithmeticOperators Module Module1 Sub Main() Dim a As Integer = 10 Dim b As Integer = 3 Console.WriteLine("Addition: " & (a + b)) ' 13 Console.WriteLine("Subtraction: " & (a - b)) ' 7 Console.WriteLine("Multiplication: " & (a * b)) ' 30 Console.WriteLine("Division: " & (a / b)) ' 3.333... Console.WriteLine("Integer Division: " & (a b)) ' 3 Console.WriteLine("Modulus: " & (a Mod b)) ' 1 Console.WriteLine("Power: " & (a ^ b)) ' 1000 End Sub End Module
EXECUTABLE STATEMENTS 1. AssignmentStatements 2. Conditional Statements 3. Looping Statements 4. Control Transfer Statements Exception Handling Statements 5.
12.
Assignment statement Assigns avalue to a variable or property. Syntax: variable = expression Example1: Dim arr(2) As Integer arr(0) = 10 arr(1) = 20 arr(2) = 30 Example 2: Dim result As Double result = Math.Sqrt(16)
IF-THEN-ELSE STATEMENT Check 2conditions If marks >= 50 Then Console.WriteLine("Pass") Else Console.WriteLine("Fail") End If
16.
Check multiple conditions Ifscore >= 90 Then Console.WriteLine("Grade A") ElseIf score >= 75 Then Console.WriteLine("Grade B") ElseIf score >= 50 Then Console.WriteLine("Grade C") Else Console.WriteLine("Fail") End If If...Then...ElseIf...Else
18.
Check Eligibility Write aVB.NET program to input the age of a person and print whether they are eligible to vote (age >= 18). • Grading System • Input marks from the user and assign grades: • 90 and above: A • 75–89: B • 50–74: C • Below 50: Fail
19.
LOOPING STATEMENTS • Loopingstatements are used to repeat a block of code multiple times until a condition is met. a) For…Next Loop b) For Each…Next Loop c) Do…Loop
20.
FOR LOOP For counterAs DataType = startValue To endValue [Step stepValue] ' Code to execute Next Example: For i As Integer = 1 To 5 Console.WriteLine("Hello " & i) Next Output Hello 1 Hello 2 Hello 3 Hello 4 Hello 5
21.
B) FOR EACH…NEXTLOOP For Each element As DataType In collection ' Code to execute Next Example Dim fruits() As String = {"Apple", "Banana", "Mango"} For Each fruit As String In fruits Console.WriteLine(fruit) Next Output Apple Banana Mango
22.
C) DO…LOOP Syntax: Do Whilecondition ' Code to execute Loop Do Until condition ' Code to execute Loop Example: Dim i As Integer = 1 Do While i <= 5 Console.WriteLine("Number: " & i) i += 1 Loop Output: Number: 1 Number: 2 Number: 3 Number: 4 Number: 5
23.
Example Dim i AsInteger = 1 Do Until i > 5 Console.WriteLine(i) i += 1 Loop Output: 1 2 3 4 5
24.
CONTROL TRANSFER STATEMENTS •A control transfer statement in VB.NET is a statement that changes the normal flow of execution in a program. • It can stop a loop, skip to the next iteration, or exit a procedure or function. • It does not perform any calculations itself — it only controls how the program moves. a) Exit Statement b) Continue Statement c) Return Statement
25.
•Exit For /Exit Do immediately exits a loop. → •Continue For / Continue Do skips the rest of the current loop → iteration and goes to the next one. •Return exits a function or subroutine immediately. →
26.
EXAMPLES For i AsInteger = 1 To 10 If i = 5 Then Exit For End If Console.WriteLine(i) Next Output: 1 2 3 4
27.
For i AsInteger = 1 To 5 If i = 3 Then Continue For End If Console.WriteLine(i) Next Output: 1 2 4 5
LOGICAL ‘AND’ Dim aAs Boolean = True Dim b As Boolean = False Console.WriteLine(a And b) ' Output: False Logical ‘Or’ Dim a As Boolean = True Dim b As Boolean = False Console.WriteLine(a Or b) ' Output:True
30.
NOT (LOGICAL NOT) Dima As Boolean = True Console.WriteLine(Not a) ' Output: False Dim a As Boolean = True Dim b As Boolean = False Console.WriteLine(a Xor b) ' Output:True Xor (Logical Exclusive OR)
31.
Dim a AsBoolean = False Dim b As Boolean = True Console.WriteLine(a AndAlso b) ' Output: False Dim a As Boolean = True Dim b As Boolean = False Console.WriteLine(a OrElse b) ' Output:True OrElse (Short-circuit OR) AndAlso (Short-circuit AND)
32.
ASSIGNMENT OPERATORS = equal +=Add and assign -= Subtract and assign *= Multiply and assign /= Floating point division = Integer divide and assign ^= Exponentiate and assign &= string concatenation (and also works with numbers as bitwise AND). Mod=
33.
OBJECT COMPARISON USINGIS OPERATOR Dim str1 As Object = "hello" Dim str2 As Object = "hello" ' Create one more reference pointing to str1 Dim str3 As Object = str1 ' Comparison using Is (checks if they refer to same object in memory) Console.WriteLine(str1 Is str2) ' False Console.WriteLine(str1 Is str3) ' True ' Comparison using = (compares values for most built-in types) Console.WriteLine(str1 = str2) ' True ' Comparison using .Equals() Console.WriteLine("str1.Equals(str2): " & str1.Equals(str2)) ' True
34.
PATTERN MATCHING USINGLIKE Module Module1 Sub Main() Dim input As String Console.Write("Enter a word: ") input = Console.ReadLine() ' Pattern 1: Starts with “an" and ends with "o" If input Like “an*" Then Console.WriteLine(“Name starts with ‘an’” ) Else Console.WriteLine("No match for pattern”) End If End Sub End Module
35.
Module Module1 Sub Main() Diminput As String Console.Write("Enter a word: ") input = Console.ReadLine() ' Pattern 1: Starts with “an" and ends with "o" If input Like “an*" Then Console.WriteLine("Pattern matched: starts with 'he' and ends with 'o'") Else Console.WriteLine("No match for pattern: he*o") End If Console.WriteLine("Press any key to exit...") Console.ReadKey() End Sub End Module Pattern matching using Like
36.
PROGRAM USING LOGICALOPERATOR ‘AND’ OPERATOR TO CHECK WHETHER ELIGIBLE FOR ADMISSION OR NOT Module Module1 Sub Main() Dim age As Integer Dim marks As Integer ' Get input Console.Write("Enter your age: ") age = Convert.ToInt32(Console.ReadLine()) Console.Write("Enter your marks out of 100: ") marks = Convert.ToInt32(Console.ReadLine()) ' Logical AND: both conditions must be true If age >= 18 And marks >= 50 Then Console.WriteLine("You are eligible for admission.") Else Console.WriteLine("You are not eligible for admission.") End If
37.
VAL IN VB.NET •The Val function in VB.NET converts a string into a numeric value. • reads numbers from the start of the string until it finds something that’s not numeric (except space, decimal point, +, -). • If the string doesn’t start with a number, it returns 0. Syntax: Dim result As Double = Val(StringExpression)
38.
Example Dim str1 AsString = "1234abc" Dim str2 As String = "abc1234" Console.WriteLine(Val(str1)) ' Output: 1234 Console.WriteLine(Val(str2)) ' Output: 0
39.
STRUCTURE IN VB.NET •A Structure in VB.NET is a value type that can hold multiple related variables (fields, properties, methods) under one name. • Similar to a class, but stored on the stack (faster, lightweight). • Good for small data objects. • A Function defined inside a Structure is called structure function
40.
Syntax of Structurewith structure function Structure StructureName ' Fields Dim Field1 As DataType Dim Field2 As DataType ' Method Sub Display() Console.WriteLine("Field1 = " & Field1 & ", Field2 = " & Field2) End Sub End Structure ‘ Continuation in next page….
Structure Student Public NameAs String Public Age As Integer End Structure Module Program Sub Main() Dim s1 As New Student s1.Name = "Bineeta" s1.Age = 6 Dim s2 As Student = s1 ' s2 is a copy of s1 s2.Age = 7 Console.WriteLine(s1.Age) ' Output: 6 (different copy) End Sub End Module EXAMPLE
43.
Public Class Person PublicFirstName As String Public LastName As String Public Age As Integer End Class Dim myPersonAs New Person() myPerson.FirstName = "John“ myPerson.LastName = "Doe" myPerson.Age = 30 Class Example
44.
SAMPLE PROGRAMS • Oddor Even Accept a number and print whether it is even or odd. • Greatest of Two Numbers Write a program that accepts two numbers and displays the greater one.
45.
Public Class Person PublicFirstName As String Public LastName As String Public Age As Integer End Class Module Module1 Sub Main() Dim myPerson As New Person() With myPerson .FirstName = "John" .LastName = "Doe" .Age = 30 End With End Module Assigning values to class members using ‘With’
46.
ARRAYS • An arrayis a collection of variables of the same type, stored under one name, and accessed using an index. • Indexes in VB.NET start from 0. • The size of an array must be defined when it is created (unless it’s a dynamic array). Syntax of declaring 1-D array Dim arrayName(size) As DataType
47.
ARRAYS – 1DARRAYS Dim a(4) As Integer ' length = 5, elements {0,0,0,0,0} Dim b As Integer() = New Integer(4) ' length = 5, all zeros Dim c() As Integer = {1, 2, 3, 4, 5} ' length = 5 Dim d As Integer() = New Integer() {1, 2, 3, 4, 5} ' length = 5 Dim e As Integer() = New Integer(4) {1, 2, 3, 4, 5} ' length = 5
48.
Different methods ofDeclararing 1-D array 1. Classic VB style: specify upper bound Dim a(4) As Integer ' 5 elements, all default 0 ' 2. Explicit with New Dim b As Integer() = New Integer(4) {} ' also 5 elements ' 3.With values (initializer) Dim c As Integer() = {1, 2, 3, 4, 5} ' length = 5 ' 4. Equivalent explicit initializer Dim d As Integer() = New Integer(4) {1, 2, 3, 4, 5}
49.
PROGRAM TO INPUTVALUES TO 1-D ARRAY Module Module1 Sub Main() Dim n As Integer Dim sum As Integer = 0 ' Ask user for size of array Console.Write("Enter number of elements: ") n = CInt(Console.ReadLine()) ' Declare array Dim numbers(n - 1) As Integer ‘continuation in next page………
50.
‘Input values fromuser For i As Integer = 0 To n - 1 Console.Write("Enter value {0}: ", i + 1) numbers(i) = Convert.ToInt32(Console.ReadLine()) sum += numbers(i) Next ' Output result Console.WriteLine("Sum of elements = " & sum) Console.WriteLine("Press any key to exit...") Console.ReadKey() End Sub End Module
51.
Dim a() asInteger Size is not given, But Valid, This only declares an array variable No memory is allocated yet