COSC226 Module4
COSC226 Module4
NET
COSC 226
TOPIC: Control Structures in Visual Basic .NET
Outline:
Note:
The rationales for using both selection and iteration control statements and their
functions remain the same with the previous programming languages you have
learned.
It’s only the syntax that differs.
So, we shall go through the syntax, examples and exercises.
Selection Control structures:
• Selection control statements are the statements that controls the
execution of the program on the basis of the specified condition.
• It is useful for determining whether a condition is true or not.
• VB.NET provides the following conditional or decision-making
statements.
• If-Then Statement
• If-Then Else Statement
• If-Then ElseIf Statement
• Select Case Statement
Selection Control structures: If-Then Statement
• Syntax • Example
If condition Then Module Module1
Sub Main()
[Statement or block of Dim monthNum As Integer
Statement]
Console.Writeline(“Enter a number of a month:”)
End If monthNum = Console.Readline()
If monthNum = 12 Then
Console.WriteLine("Welcome to Christmas
• This statement executes the Period")
statements depending upon the End If
given condition. Console.ReadKey()
• If the condition is evaluated to be End Sub
true it execute the statement(s), End Module
and exit.
Selection Control structures: If-Then Statement
• Class Exercise
Given the following tax table information, write VB.NET program to print the taxRate appropriate to a particular
pay.
If pay is more than 100,000, tax rate is 40%
If pay is more than 60,000 and less than or equal to 100,000, tax rate is 30%
If pay is more than 30,000 and less than or equal to 60,000, tax rate is 20%
If pay is more than 15,000 and less than or equal to 30,000, tax rate is 10%
If pay is more than 5,000 and less than or equal to 15,000, tax rate is 5%
If pay is less than or equal to 5,000, tax rate is 0%
Selection Control structures: Nested If
• Relational operator
• Use to make comparison in logical expression
Selection cont.
• The selection statement use in VB.NET include:
• If Then statement
• If Then Else statement
• Select Case statement
Selection cont.
• Writing one-way Selection statements
• Single-line If syntax:
If (condition) Then statement
• Multi-line If syntax:
If ( condition) Then
statement
.
statement
End If
Selection cont.
• Multi-line if:
• Nested If
• If statement written inside another If statement
• Can replace compound expression with nested If
If/Then/Else Selection Structure
•
Selection Control structures: Example
• You can use the operators =, <>, <, <=, >, and >= in an Is
clause
Select Case Statement cont.
•Generally, the Select Case structure follows the following rules
•Each value list contains one or more of the following types of items separated by
commas.
1. a literal
2. a variable
3. an expression
4. an inequality sign preceded by Is and followed by a literal, variable, or expression
5. a range given in the form a To b, where a and b are literals, variables, or expressions.
Selection Control structures:
Select Case Statement
• Class Exercise
• Using Babcock grading system, write a VB.NET application to read a
test score, determine the letter grade and prints the grade using
Select Case statement.
• Hint:
Test Score Grade
80 – 100 A
60 – 79 B
50 – 59 C
45 – 49 D
40 – 44 E
0 – 39 F
• Branching statement interrupts the sequential flow
of the program execution and allow it to jump from
one portion of a program to another. It isused to
cause certain actions within a program if a certain
condition is met.e.g an example of branching
statements are Exit, Go to, If, Return, Select Case
• Iteration Statement are called looping statements. It
allows a group of statements to be executed more
than once e.g Do, For and For Each
Selection cont.
• Write a VB.NET console application to read a test score, calculates the
letter grade and prints the grade using
• If … THEN statement
• Select Case statement
Examples
• If then statement
• If Balance-Check< 0 Then
• Print “You are overdrawn”
• If /Then/ End If
• If Balance-Check< 0 Then
• Print “You are overdrawn”
• Print “ Authorities have been notified”
• End If
• If/ Then/ Else/ End If
• If Balance-Check< 0 Then
• Print “You are overdrawn”
• Print “ Authorities have been notified”
• Else
• Balance = Balance- Check
• End If