Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

VB.

NET
PROGRAMMING
Program-Control
Statements
Chapter 5:

Objective

 Enumerate the different control structures and their uses.


 Create program that will use control structures.
Chapter 5:

Program-Control Statements

 Like any programming language, the control statements are the essence
of VB programming language because it governs the flow of program
execution. The way they are implemented in many ways defines the
personality of the VB language.
Chapter 5:

Program-Control Statements

 The program-control statements may be separated into three categories.


The first consists of the conditional instructions if and case. The second
are the loop-control statements such as while, for and do-while. The final
category is the unconditional branch instruction label.
Chapter 5:

The If Then Statement

 The If Then Statement – If statement executes a statement or a block of


codes if an expression is true.
General syntax:
If (Boolean expression) Then statement
End If
Chapter 5:

The If Then Else Statement

 If statement executes a statement or a block of codes if an expression is


true, otherwise the else statement will be executed.
General syntax:
If (Boolean expression) Then
statement
Else
statement
End If
Chapter 5:

The If Then ElseIf Statement


 Another variation of if statement is if then elseif statement to state multiple
outcomes for several different expressions.
General syntax:
If (Boolean expression) Then
statement 1
Else if
statement 2
Else
statement 3
End If
Chapter 5:

The Select Case Statement


 Another control statement that runs one of several groups of statements, depending on the
value of an expression.
General syntax:
Select Case TestExpression
Case ExpressionList1
Statement 1
Case ExpressionList2
Statement 2
Case Else
Statement 3
End Select
Chapter 5:

The Select Case Statement

 Example:
Chapter 5:

The Select Case Statement

 Example:
Chapter 5:

While loop

 While loop executes a body of statement continuously while some


condition continues to be true.
General syntax:
While condition
[statements]
[Exit While]
[statements]
End While
Chapter 5:

While loop

 Example:
Chapter 5:

While loop

 Example:
Chapter 5:

Do While loop

 The do-while loo[, like the repeat-until syntax of other languages, does
not check the condition until after the loop code block is executed, so the
code block will always be run at least once with do-while.
Chapter 5:

Do While loop
 General syntax:  The condition can be any valid VB
Do { While | Until } condition expression that evaluates to a Boolean
[statements] value.
[Exit Do]
[statements]
 The code block can be any valid VB
Loop code.
or
Do
[statements]
[Exit Do]
[statements]
Loop { While | Until } condition
Chapter 5:

Do While loop

 Example
Chapter 5:

Do While loop

 Example
Chapter 5:

Do While loop

 Example
Chapter 5:

For Loop

 For loop indicates the starting value for the loop control variable, the test
condition that controls loop entry, and the expression that alters the loop
control variable all in one convenient place.
Chapter 5:

For Loop
 General syntax:
For counter [ As datatype] = star To end [Step step]
[statements
[exit for]
[statements]
Next [counter]

For loop begins with the reserved word “for” followed by a st of parentheses, and the three sections
are:
Initializing the loop control variable
Testing the loop control variable
Updating the loop control variable
Chapter 5:

For Loop

 Example
Chapter 5:

For Loop

 Example

You might also like