0% found this document useful (0 votes)
6 views

Control Statements

The document provides an overview of control statements in VB.NET, focusing on decision-making and looping structures. It explains various control structures such as If...Then, Select Case, and different types of loops including Do Loop, For...Next, and While loops, along with their syntax and examples. The document serves as a guide for programmers to understand how to control the flow of execution in their VB.NET applications.

Uploaded by

oimarigobert
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Control Statements

The document provides an overview of control statements in VB.NET, focusing on decision-making and looping structures. It explains various control structures such as If...Then, Select Case, and different types of loops including Do Loop, For...Next, and While loops, along with their syntax and examples. The document serves as a guide for programmers to understand how to control the flow of execution in their VB.NET applications.

Uploaded by

oimarigobert
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 15

Control Statements in

VB.NET
Introduction
• Control structures are statements or group of
statements in a programming language which
determine the sequence of execution of other
instructions or statements.
– The 3 main control structures used in programming are:
Selection/branching
Looping/iteration
Counting/sequence

These are classified in two main categories


1. VB.Net Decision Making
2. VB.Net Loops
• In decision-making VB.NET Decision the
structures, Making
programmer must
define one or more conditions to be evaluated or tested by
the program.
• A statement or statements are to be executed if the
condition is determined to be true, and optionally, further
statements to be executed if the condition is determined to
be false.
Statement Description
• Below are the decision-making statements
“An If...Then statement available
consists of a in
If ...VB.Net:
Then statement boolean expression followed by one or
more statements”.
An If...Then statement is followed by an
If...Then...Else optional Else statement, which gets
statement executed when the Boolean expression is
false.
If or Else if statement can be used inside
nested If statements
another If or Else if statement(s).
A ‘Select Case' statement is used to test a
Select Case statement variable for equality against a list of
values.
Nested Select Case One select case statement can be used
statements inside another select case statement(s).
VB.NET Decision Making
If ... Then statement
~ If conditional expression is one of the most useful control structures
which allows us to execute a expression if a condition is true and
execute a different expression if it is False. The syntax looks like this:
If(boolean_expression)Then
'statement(s) will execute if the Boolean expression is true
Else
'statement(s) will execute if the Boolean expression is false
End If
Example
Private Sub TextBox2_LostFocus(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox2.LostFocus
Dim TextBoxData As String
If Not IsNumeric(TextBox2.Text) Then
MsgBox("this text box must contain an numeric value")
TextBox2.Focus()
'End If
End Sub
Example 2
Private Sub OK_Click(sender As Object, e As EventArgs)
Handles OK.Click
Dim myNumber, MyAge As Integer
myNumber = TxtNum.Text
MyAge = TxtAge.Text

If myNumber > 100 And MyAge > 60 Then


MsgBox(" Congratulation! You win a lucky prize")
Else
MsgBox("Sorry, You did not win any prize")
End If
End Sub
IF/THEN......ELSE/END IF STATEMENT
Control structure in which a selection or given program code is executed if a given condition is
met otherwise next code are executed
Syntax:
If condition Then
[statements]
Else If condition Then
[statements]
-
Else
[statements]
End If
Example
Module Module1
Sub Main()
Dim x As Integer = 5
If (x > 0) Then
Console.WriteLine("A is positive")
ElseIf (x < 0) Then
Console.WriteLine("B is positive")
Else
Console.WriteLine("A is zero")
End If

End Sub
End Module
Example 2
Private Sub OK_Click(sender As Object, e As EventArgs) Handles OK.Click
Dim Mark As Integer
Dim Grade As String
Mark = TxtMark.Text
If Mark >= 80 And Mark <= 100 Then
Grade = "A"

ElseIf Mark >= 60 And Mark < 80 Then


Grade = "B"

ElseIf Mark >= 40 And Mark < 60


Grade = "C"

ElseIf Mark >= 0 And Mark < 40


Grade = "D"

Else
Grade = "Out of Range"

End If
MsgBox("You Grade is " & Grade)
End Sub
• NESTED IF/THEN......ELSE/END IF STATEMENT
If( boolean_expression 1)Then
'Executes when the boolean expression 1 is true
If(boolean_expression 2)Then
'Executes when the boolean expression 2 is true
End If
End If
Example:
Module decisions
Sub Main()
'local variable definition
Dim a As Integer = 100
Dim b As Integer = 200
' check the boolean condition
If (a = 100) Then
' if condition is true then check the following
If (b = 200) Then
' if condition is true then print the following
Console.WriteLine("Value of a is 100 and b is 200")
End If
End If
Console.WriteLine("Exact value of a is : {0}", a)
Console.WriteLine("Exact value of b is : {0}", b)
Console.ReadLine()
End Sub
End Module
SELECT....CASE STATEMENT
The Select Case statement executes one of several groups of statements depending on the value of an
expression
Syntax
Select [ Case ] expression
[ Case expressionlist
[ statements ] ]
[ Case Else
[ elsestatements ] ]
End Select
Example
Module Module1
Sub Main()
Dim keyIn As Integer
WriteLine("Enter a number between 1 and 4")
keyIn = Val(ReadLine())
Select Case keyIn
Case 1
WriteLine("You entered 1")
Case 2
WriteLine("You entered 2")
Case 3
WriteLine("You entered 3")
Case 4
WriteLine("You entered 4")
End Select
End Sub
End Module
VB.NET Loops
A loop statement allows us to run a statement or group of
statements many times.
Following are the types of looping statements available in VB.Net

Loop Type Description


It loops through the enclosing block of statements as long as a
Boolean condition is True or until the condition becomes
Do Loop
True. It might be ended at any time by using the Exit Do
statement.
It repeats a set number of statements, and a loop index counts
For...Next
the number of loop iterations while the loop executes.
It iteratively repeats a set of statements for each element in a
For Each...Next collection. This loop is used to access and manipulate all
elements in an array or a VB.Net collection.
While... End As long as a specified condition is True, it will execute a
While series of statements.
You can nest one or more loops within another While, For, or
Nested loops
Do loop.
Do Loop
•The Do loop can be used to execute a fixed block of statements indefinite number of times. The
Do loop keeps executing it's statements while or until the condition is true.
•Two keywords, while and until can be used with the do loop. The Do loop also supports an Exit
Do statement which makes the loop to exit at any moment. The syntax of Do loop looks like this:
Do[{while | Until} condition]
[statements]
[Exit Do]
[statements]
Loop
Example
Module Module1
Sub Main()
Dim str As String
Do Until str = "Cool"
System.Console.WriteLine("What to do?")
str = System.Console.ReadLine()
Loop
End Sub
End Module
WHILE LOOP
While loop keeps executing until the condition against which it tests remain
true. The syntax of while loop looks like this:
While condition
[statements]
End While

Example
Module Module1
Sub Main()
Dim x As Integer
While (x < 5)
Console.WriteLine(x)
x=x+1
Console.ReadLine()
End While
End Sub
End Module
For Each...Next
For loops enable us to execute a series of expressions multiple numbers of
times
Syntax
For index=start to end [Step step]
[statements]
[Exit For]
[statements]
Next[index]
Example
Module Module1
Sub Main()
For count As Integer = 1 To 10 Step 2
Console.WriteLine(count)
Next
End Sub
End Module
SELECTED EXAMPLES:
A program required to calculate the sum of the first 5
positive integers.
(i) Using the Do while Loop Using the Do..Until loop.

Dim counter as Integer Dim counter as Integer


Dim sum as Integer Dim sum as Integer
Counter = 1 Counter = 1
Do While counter < 6 Do Until counter > 5
sum = sum + counter sum = sum + counter
counter = counter +1 counter = counter +1
Loop Loop
Lblsum.Text = sum Lblsum.Text = sum
…… Questions??…..

You might also like