Basic Syntax and Control
Structures
ICT 211
VB Syntax
Variables and Data Types
•Variables:
• Variables are used to store data that can be used and manipulated
within a program.
• In VB, variables must be declared with a specific data type before
they can be used.
Common Data Types in VB:
•Integer: Used to store whole numbers (e.g., Dim age As Integer).
•Double: Used to store floating-point numbers (e.g., Dim price As
Double).
•String: Used to store text (e.g., Dim name As String).
•Boolean: Used to store True or False values (e.g., Dim isActive
As Boolean).
Declaring and Initializing Variables
Declaring a Variable:
•Use the Dim keyword to declare a variable:
Dim variableName As DataType
Initializing a Variable:
Initializing a Variable:
•You can assign a value to a variable at the time of declaration:
•Dim age As Integer = 25
Input and Output Operations
Input Operations:
•You can use the InputBox function to prompt the user for input:
Dim userInput As String
userInput = InputBox("Enter your name:")
Output Operations:
•Use the [Link] method to display output to the user:
[Link]("Hello, " & userInput)
Control Structures
Conditional Statements
•If...Then:
• Executes a block of code if a specified condition is True:
If age >= 18 Then
[Link]("You are an adult.")
End If
If...Then...Else:
If age >= 18 Then
[Link]("You are an adult.")
Else
[Link]("You are a minor.")
End If
Select Case:
•An alternative to multiple If...Then...Else statements, used for
evaluating a single expression:
Select Case grade
Case "A"
[Link]("Excellent!")
Case "B"
[Link]("Good job!")
Case Else
[Link]("Keep trying!")
End Select
Loops
For Loop:
•Repeats a block of code a specified number of times:
For i As Integer = 1 To 10
[Link]("Number: " & i)
Next
While Loop:
While Loop:
•Repeats a block of code as long as a condition is True:
Dim count As Integer = 0
While
count < 5
[Link]("Count: " & count)
count = count + 1
End While
Do While Loop:
Do While Loop:
•Similar to the While loop, but the condition is evaluated after the
loop's body has been executed at least once:
Dim count As Integer = 0
Do
[Link]("Count: " & count)
count += 1
Loop While count < 5
Using Nested Control Structures
Using Nested Control Structures
•Nested If Statements:
•An If statement within another If statement:
If age >= 18 Then
If hasID Then
[Link]("Access granted.")
Else
[Link]("Please provide ID.")
End If
Else
[Link]("You are not old enough.")
End If
Nested Loops:
Nested Loops:
•A loop within another loop:
For i As Integer = 1 To 3
For j As Integer = 1 To 3
[Link]("i: " & i & ", j: " & j)
Next
Next
Exercise 1:Variables and Data Types
Create a program that asks the user to enter their name, age, and
favorite color. Display a message that includes this information.
Exercise 2: Conditional Statements
Create a program that asks the user to enter a number. If the
number is positive, display "Positive Number"; if it's negative, display
"Negative Number"; if it's zero, display "Zero".
Exercise 3: Loops
•Create a program that displays the numbers from 1 to 10 using a For
loop.
•Create a program that asks the user for a number and then counts
down from that number to zero using a While loop.
Exercise 4: Nested Control Structures
Create a program that uses nested loops to display a multiplication
table (1-10).
[Link](“Thank You for Attending !!
We have Test on Friday 27 Sept 2024")