Chapter 2
Chapter Two: Control Structures in [Link]
1. Introduction to Control Structures
Control structures in [Link] are fundamental programming elements that allow
developers to manage the flow of execution in a program. They dictate the order in
which instructions are executed, enabling a program to make decisions, repeat
actions, and branch to different parts of the code. Mastering these structures is
essential for writing efficient, logical, and robust [Link] applications. They primarily
include conditional statements, loops, and branching mechanisms.
2. Selection Structures (Conditional Statements)
Selection structures, also known as conditional statements, are used to execute
different blocks of code based on whether a specified condition evaluates to True or
False. This allows the program to choose a path of execution from several alternatives.
2.1 IF…THEN Statement
The simplest form of a conditional statement, the If...Then statement, executes a
block of code only if a single, specified condition is True. If the condition is False, the
code block is skipped entirely.
Dim age As Integer = 18
If age >= 18 Then
[Link]("You are eligible to vote.")
End If
In this example, the message “You are eligible to vote.” will only be displayed if the
variable age is greater than or equal to 18.
2.2 IF…THEN…ELSE Statement
The If...Then...Else statement provides a two-way decision path. It executes one
block of code if the condition is True, and a different block of code if the condition is
False. This ensures that one of the two code blocks will always be executed.
Dim score As Integer = 50
If score >= 50 Then
[Link]("You passed!")
Else
[Link]("You failed.")
End If
Here, if the score is 50 or greater, the program outputs “You passed!”. Otherwise,
the Else block is executed, and “You failed.” is displayed.
2.3 ELSEIF Statement
When there are multiple conditions to check, the ElseIf statement is used to chain
together several conditional tests. The program checks the conditions sequentially,
and the code block corresponding to the first condition that evaluates to True is
executed. All subsequent ElseIf and the final Else blocks are then skipped.
Dim marks As Integer = 75
If marks >= 80 Then
[Link]("Grade A")
ElseIf marks >= 60 Then
[Link]("Grade B")
Else
[Link]("Grade C")
End If
Since marks is 75, the first condition ( marks >= 80 ) is False. The program proceeds
to the ElseIf marks >= 60 condition, which is True, and outputs “Grade B”.
2.4 SELECT CASE Statement
The Select Case statement is an efficient and cleaner alternative to a long series of
If...ElseIf statements, especially when checking a single variable against multiple
possible values. It evaluates an expression once and compares its result against a list
of Case values.
Dim day As Integer = 3
Select Case day
Case 1
[Link]("Monday")
Case 2
[Link]("Tuesday")
Case 3
[Link]("Wednesday")
Case Else
[Link]("Invalid day")
End Select
In this example, the value of the day variable (which is 3) is matched against the Case
values, resulting in the output “Wednesday”. The optional Case Else block
handles any value that does not match the preceding cases.
3. Repetition Structures (Loops)
Repetition structures, commonly known as loops, are used to execute a block of
code multiple times. This is crucial for tasks that involve processing lists of data,
iterating through collections, or repeating an action until a specific condition is met.
3.1 FOR…NEXT Loop
The For...Next loop is ideal for situations where the number of iterations is known
beforehand. It executes a block of code a specified number of times, controlled by a
counter variable that increments or decrements with each pass.
For i As Integer = 1 To 5
[Link]("Iteration: " & i)
Next
This loop will execute five times, with the counter variable i taking values from 1 to 5.
The Next keyword marks the end of the loop block and causes the counter to update.
3.2 DO WHILE Loop
The Do While loop executes a block of code while a specified condition remains
True. The condition is checked before the loop body is executed. If the condition is
initially False, the loop body will never run.
Dim counter As Integer = 1
Do While counter <= 5
[Link]("Count: " & counter)
counter += 1
Loop
The loop continues as long as counter is less than or equal to 5. The line counter +=
1 is essential to ensure the condition eventually becomes False, preventing an
infinite loop.
3.3 DO UNTIL Loop
The Do Until loop executes a block of code until a specified condition becomes
True. It is the logical inverse of the Do While loop. Like Do While , the condition is
checked before the loop body, meaning the code may not execute at all if the
condition is initially True.
Dim number As Integer = 1
Do Until number > 5
[Link]("Number: " & number)
number += 1
Loop
The loop will continue to run until the number variable is greater than 5. The output
will be the same as the Do While example, demonstrating two ways to achieve the
same repetitive task.
4. Practical Session: Implementing Control Structures
4.1 Creating a Grade Calculator
To demonstrate the practical application of selection structures, we will develop a
[Link] program that takes user input for marks and displays the corresponding letter
grade using the If...ElseIf structure. This is a common scenario in application
development where multiple criteria must be evaluated sequentially.
Module GradeCalculator
Sub Main()
[Link]("Enter marks: ")
Dim marks As Integer = Convert.ToInt32([Link]())
If marks >= 80 Then
[Link]("Grade: A")
ElseIf marks >= 60 Then
[Link]("Grade: B")
ElseIf marks >= 40 Then
[Link]("Grade: C")
Else
[Link]("Grade: F")
End If
End Sub
End Module
This program first prompts the user for input. The series of If...ElseIf statements
then efficiently determines the correct grade. For instance, a score of 75 will fail the
first If test but pass the first ElseIf test, resulting in “Grade: B”.
5. Summary and Conclusion
Control structures are the backbone of program logic in [Link]. They provide the
necessary tools to manage execution flow:
Conditional statements ( If...Then , If...Then...Else , ElseIf , Select
Case ) control program execution based on conditions, allowing for decision-
making.
Loops ( For...Next , Do While , Do Until ) allow code to be executed
repeatedly, which is essential for automation and data processing.
The choice of structure depends on the specific requirement: use For...Next
for a known number of iterations, and Do While / Do Until for condition-based
repetition.