0% found this document useful (0 votes)
176 views39 pages

Visual Basics : (Lecture 6)

The document discusses different types of decision structures in Visual Basic, including Select Case and decision structures using ranges. It provides examples of Select Case structures using different case values and ranges identified by "Is" and "To" keywords. The document also discusses loops in Visual Basic, including For-Next loops using counter variables, changing properties within loops, nested loops, and Do loops with conditional tests inside and outside the loop. It provides examples of each type of loop.

Uploaded by

bhumika_shah7
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
176 views39 pages

Visual Basics : (Lecture 6)

The document discusses different types of decision structures in Visual Basic, including Select Case and decision structures using ranges. It provides examples of Select Case structures using different case values and ranges identified by "Is" and "To" keywords. The document also discusses loops in Visual Basic, including For-Next loops using counter variables, changing properties within loops, nested loops, and Do loops with conditional tests inside and outside the loop. It provides examples of each type of loop.

Uploaded by

bhumika_shah7
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Visual Basics …

[Lecture 6 ]

Bhumika Shah
{Lecturer, Computer Engineering Department }
Select Case Decision Structure..
 A Select Case structure begins with the Select Case keywords
and ends with the End Select keywords.

Select Case variable


Case value1
statements executed if value1 matches variable
Case value2
statements executed if value2 matches variable
Case value3
statements executed if value3 matches variable
.
.
.
End Select
Example …
Select Case age
Case 16
Label1.Caption = "You can drive now!"
Case 18
Label1.Caption = "You can vote now!"
Case 21
Label1.Caption = “Time for career"
Case 65
Label1.Caption = "Time to retire and have fun!"
End Select
Using the Ranges of Test Case
Values…
 Visual Basic lets you use comparison operators
to include a range of test values in a Select
Case structure.
 The is and To Keywords ::
 To use the comparison operators, you need to
include the Is keyword or the To keyword in the
expression to identify the comparison you’re making.
The Is keyword instructs the compiler to compare the
test variable to the expression listed after the Is
keyword.
 The To keyword identifies a range of Values.
Example….
Select Case Age
Case Is < 13
Label1.Caption = "Enjoy your youth!"
Case 13 To 19
Label1.Caption = "Enjoy your teens!"
Case 21
Label1.Caption = “Time to build career!!! "
Case Is > 100
Label1.Caption = "Looking good!"
Case Else
Label1.Caption = "That's a nice age to be."
End Select
Finding and Correcting the Errors…
 Three Types of Errors…
1) Logic errors
2) Syntax errors
3) Run-time errors
Logic Errors…
 A logic error is a human error — a
programming mistake that makes the
program code produce the wrong results.
 Most debugging efforts focus on tracking

down programmer logic errors..


If Age > 13 And Age < 20 Then
Text2.Text = "You're a teenager."
Else
Text2.Text = "You're not a teenager."
End If
What for the AGE=13 ???
 This type of mistake is not a syntax error (the
statements follow the rules of Visual Basic);
 it is a mental mistake, or logic error.
 The correct decision structure contains a

greater than or equal to operator (>=) in the


first comparison after the If...Then statement:

If Age >= 13 And Age < 20 Then


Syntax Errors…
 A syntax error (compiler error) is a programming
mistake that violates the rules of Visual Basic,
such as a misspelled property or keyword.
 As you type program statements, Visual Basic

points out several types of syntax errors — and


you won’t be able to run your program until you
fix each one.
Runtime Errors…
 A run-time error is any error — usually an
outside event or an undiscovered syntax error
— that forces a program to stop running.
 example of conditions that can produce run-

time errors is a misspelled file name in a


LoadPicture function…
USING LOOPS….
 For…Next ::
Loop statement to run a block of code
a set number of times.
 Do… Loop ::

statement to run a block of code until a


specific condition is met
Writing FOR….NEXT Loops…
 With a For...Next loop, you can execute a specific group of program
statements in an event procedure a specific number of times.
 For...Next Loop Syntax ::
For variable = start To end
statements to be repeated
Next variable
{For, To, and Next are required keywords}
= is a required operator.
 First, you replace variable with the name of a numeric variable that
keeps track of the current loop count.
 Next, you replace start and end with numeric values that represent
the starting and stopping points for the loop.
 The line or lines between the For and Next statements are the
instructions that repeat each time the loop executes.
For i = 1 To 4
Beep
Next I
 This loop is the functional equivalent of

writing the Beep statement four times in a


procedure
1. Beep
2. Beep
3. Beep
4. Beep
Using the Counter Variable in a
Loop….

 A counter variable is just like any other


variable.
 You can assign counter variables to

properties, use them in calculations, or


display them in a program.
Print Method Counter Variable..
 For example, you could use the Print method in a For…Next
loop to display output on a form:
For i = 1 To 10
Print "Line“ ; i
Next i
 The Print method displays the word Line, followed by the loop
counter, 10 times on the form.

 comma (,) Places the elements one tab field apart.


 semicolon (;) Places elements side by side. (Visual Basic
displays the counter variable next to the string with no
additional spaces in between.)
Sample example :: CODE
Private Sub Command1_Click()
For i = 1 To 4
Print "value “ ; i
Label1.Caption = i
Next i
End Sub
Private Sub Command1_Click()
For i = 1 To 4
Print "value ", i
Label1.Caption = i
Next i
End Sub
Changing Properties with a Loop…
 In Visual Basic, you can use loops to change properties and
update key variables that:
1) Open bitmaps with the Picture property.
2) Keep a running total with a variable.
3) Change the text size on your form updating the form's
FontSize property.
 The example below shows how a For…Next loop can change
the text size on a form by changing the form's FontSize
property..
 The FontSize property adjusts the point size of the text on a
form.
 You can use it as an alternative to changing the point size
with the Font property
Private Sub Command1_Click()
For i = 1 To 20
Print "line “ ; I
FontSize = 10 + I
Label1.Caption = I
Next i

End Sub
Complex FOR…NEXT Loops..
 Creating Loops with a Custom Counter…
 You can create a loop with a counter pattern other than 1, 2, 3, 4,
and so on.
 First, you specify a different start value in the loop.
 Then, you use the Step keyword to increment the counter at different
intervals.
 For example, following loop controls the print sequence of numbers
on a form:
5
10
15
20
25
……
100
Private Sub Command1_Click()
For i = 5 To 100 Step 5
Print i
Next i

End Sub
Specifying Decimal Values…

For i = 1 To 2.5 Step 0.5


Print i
Next i
 The program prints this sequence of numbers:

1
1.5
2
2.5
Nested For...Next Loops ::
Private Sub Command1_Click()
For i = 1 To 5
For j = 1 To 5
Print i + j
Next j
Next I
End Sub
Using the EXIT FOR Statement…
In Visual Basic, you can use an Exit For statement to exit
a For...Next loop before the loop has finished executing.
Private Sub Command1_Click()
For i = 1 To 5
For j = 1 To 5
If i + j = 8 Then Exit For
Print i + j
Next j
Next i

End Sub
Writing Do Loops…
 Do loops are valuable because occasionally
you can’t know in advance how many times a
loop should repeat.
 As an alternative to a For...Next loop, you can

write a Do loop that executes statements


until a certain condition in the loop is true.
 A Standard Do Loop::

Do While condition
block of statements to be executed
Loop
Private Sub Command1_Click()
i=0
Do While i < 25
'block of statements to be executed
Print "value is :: "; i
i=i+1
Loop
End Sub
 NOTE ::
This code brings up an interesting fact about Do
Loops:
if the condition at the top of the loop Is
not True when the Do statement is first evaluated,
Visual Basic never executes the Do loop.
Conditional Test at the Bottom of the
Loop …
 If you want the loop to always run at least once in a
program, put the conditional test at the bottom of
the loop.
Private Sub Command1_Click()
i=0
Do
Print "value is :: "; i
i=i+1
Loop While i < 25
End Sub
 The advantage of this syntax is that you can update the

variable before the conditional test in the loop


GOOD DAY !!

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy