0% found this document useful (0 votes)
20 views19 pages

VB 6 1

Uploaded by

Young Uther
Copyright
© © All Rights Reserved
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)
20 views19 pages

VB 6 1

Uploaded by

Young Uther
Copyright
© © All Rights Reserved
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/ 19

Visual Basic

Statements and
Expressions
 The simplest statement is the assignment statement. It consists of a
variable name, followed by the assignment operator (=), followed by
some sort of expression
 Examples:
StartTime = Now
Explorer.Caption = "Captain Spaulding"
BitCount = ByteCount * 8
Energy = Mass * LIGHTSPEED ^ 2
NetWorth = Assets – Liabilities
The assignment statement stores information.
 Statements normally take up a single line with no terminator.
Statements can be stacked by using a colon (:) to separate them.
Example:
StartTime = Now : EndTime = StartTime + 10

 (Be careful stacking statements, especially with If/End If structures.


You may not get the response you desire.)
 If a statement is very long, it may be continued to the next line using
the continuation character, an underscore ( _). Example:
Months = Log(Final * IntRate / Deposit + 1) _
/ Log(1 + IntRate)
 Comment statements begin with the keyword Rem or a single quote
('). For example:
Rem This is a remark
' This is also a remark
x = 2 * y ' another way to write a remark or comment
 You, as a programmer, should decide how much to comment your
code. Consider such factors as reuse, your audience, and the legacy
of your code
Visual Basic Operators
 The simplest operators carry out arithmetic operations. These
operators in their order of precedence are:
 Parentheses around expressions can change precedence.
 To concatenate two strings, use the & symbol or the + symbol:
lblTime.Caption = "The current time is" & Format(Now, “hh:mm”)
txtSample.Text = "Hook this “ + “to this”
 Operator Operation
^ Exponentiation
*/ Multiplication and division
\ Integer division (truncates)
Mod Modulus
+- Addition and subtraction
 There are six comparison operators in Visual Basic:
 Operator Comparison
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
= Equal to
<> Not equal to
 The result of a comparison operation is a Boolean value (True or
False).
 We will use three logical operators
 Operator Operation
Not Logical not
And Logical and
Or Logical or

 The Not operator simply negates an operand.


 The And operator returns a True if both operands are True.
Else, it returns a False.
 The Or operator returns a True if either of its operands is True,
else it returns a False.
 Logical operators follow arithmetic operators in precedence.
Visual Basic Functions
 Visual Basic offers a rich assortment of built-in functions. The on-line
help utility will give you information on any or all of these functions
and their use. Some examples are:
 Function Value Returned
Abs Absolute value of a number
Asc ASCII or ANSI code of a character
Chr Character corresponding to a given ASCII or ANSI code
Cos Cosine of an angle
Date Current date as a text string
Format Date or number converted to a text string
Left Selected left side of a text string
Len Number of characters in a text string
Mid Selected portion of a text string
Now Current time and date
Right Selected right end of a text string
Rnd Random number
Sin Sine of an angle
Sqr Square root of a number
Str Number converted to a text string
Time Current time as a text string
Timer Number of seconds elapsed since midnight
Val Numeric value of a given text string
Visual Basic Symbolic Constants
 Many times in Visual Basic, functions and objects require data
arguments that affect their operation and return values you want to
read and interpret
 These arguments and values are constant numerical data and difficult
to interpret based on just the numerical value
 To make these constants more understandable, Visual Basic assigns
names to the most widely used values - these are called symbolic
constants.
 As an example, to set the background color of a form named
frmExample to blue, we could type:
 frmExample.BackColor = 0xFF0000
 or, we could use the symbolic constant for the blue color (vbBlue):
 frmExample.BackColor = vbBlue
 Defining Your Own Constants
 You can also define your own constants for use in Visual Basic. The format
for defining a constant named PI with a value 3.14159 is:
Const PI = 3.14159
Visual Basic Branching - If
Statements
 Branching statements are used to cause certain actions within a
program if a certain condition is met.
 You can also have If/Then/End If blocks to allow multiple
statements:
If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
End If
 In this case, if Balance - Check is less than zero, two lines of information
are printed
 Or, If/Then/Else/End If blocks:
If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
Else
Balance = Balance - Check
End If
 Here, the same two lines are printed if you are overdrawn (Balance - Check
< 0),but, if you are not overdrawn (Else), your new Balance is
computed.
 Or, we can add the ElseIf statement:
If Balance - Check < 0 Then
Print "You are overdrawn"
Print "Authorities have been notified"
ElseIf Balance - Check = 0 Then
Print "Whew! You barely made it"
Balance = 0
Else
Balance = Balance - Check
End If
 Now, one more condition is added. If your Balance equals the Check
amount (ElseIf Balance - Check = 0), a different message appears.
Select Case - Another Way to
Branch
 In addition to If/Then/Else type statements, the Select Case format can be
used when there are multiple selection possibilities.
 Say we've written this code using the If statement:
If Age = 5 Then
Category = "Five Year Old"
ElseIf Age >= 13 and Age <= 19 Then
Category = "Teenager"
ElseIf (Age >= 20 and Age <= 35) Or Age = 50 Or (Age >= 60 and Age <= 65)
Then
Category = "Special Adult"
ElseIf Age > 65 Then
Category = "Senior Citizen"
Else
Category = "Everyone Else"
End If
 The corresponding code with Select Case would be:
Select Case Age
Case 5
Category = "Five Year Old"
Case 13 To 19
Category = "Teenager"
Case 20 To 35, 50, 60 To 65
Category = "Special Adult"
Case Is > 65
Category = "Senior Citizen"
Case Else
Category = "Everyone Else"
End Select
 The GoTo Statement
 Visual Basic Looping
 Visual Basic Counting

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