Chapter 5

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 45

Chapter 5 Statements & Expressions

review
Constant

Variable
Type of variable

Conversion of variables type


Excises Practice

In this lesson you will learn how to


Visual Basic Statements

Visual Basic 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.

Assignment statements take the forms


Variable=Expression

Or Object.Property=expression

The expression can be another variable, a number, or a more complicated expression made up by using arithmetic operators, such as + and *, to combine variables and numbers.

Examples:

StartTime = Now Explorer.Caption = "Captain Spaulding" BitCount = ByteCount * 8 Energy = Mass * LIGHTSPEED ^2 NetWorth = Assets - Liabilities

The assignment statement stores

information. When the assignment statement is executed, the computer first evaluates the expression on the right-hand side of the expression to get the value of the expression. Then it uses that value to set the value of the variable on the left-hand side of the assignment operator (equal sign). You can think of the assignment operator (=) as saying, make the

Note that a variable can

meaningfully occur on both sides of the assignment operator, =, and can do so in ways that may at first seem a little strange. For example, consider

Count=count + 10

Combining Statements on One Line


Statements normally take up a

single line with no terminator. you can place two or more statements on a line if you use a colon (:) to separate
StartTime = Now : EndTime = StartTime + 10

Breaking a Line Into Multiple Lines


You can break a long statement into

multiple lines in the Code window using the line-continuation character (a space followed by an underscore). Using this character can make your code easier to read, both online and when printed. The following code is broken into two lines with linecontinuation characters ( _)

Adding Comments to Your Code


Usually in an project there are many

variables, most of them have special function what you should remember clearly from begin to the end. Some expression have important mean So, we give comments for them where they were defined;

VB provides an easy way to

document a program internally. You simply place an apostrophe (') before the statement you want VB to treat as a comment. VB ignores everything after the apostrophe on that line. There are two kinds of remarks:

Comment statements begin with

the keyword Rem or a single quote (') or apostrophe

example
Rem This is a remark
' This is also a remark x = 2 * y

' another way to write a remark or comment

Comments can follow a statement

on the same line or can occupy an entire line. Both are illustrated in the preceding code. Remember that comments can't follow a linecontinuation character on the same line. As a programmer, you should decide how to comment your code. Consider such factors as reuse, your audience, and the legacy of

Print Method
The object.Print examples so far

have been quite simple object.Print followed by a message statementbut the syntax of the message can be somewhat more elaborate than simple text. The syntax of the Print method to print information on a form is Print [outputlist]

For one thing, the position in which the

message appears can be specified by preceding the message text with VBs Spc() or Tab() functions. If you want to indent a message by 10 spaces, you could do so in either of two ways object.Print Spc(10) This message is preceded by ten spaces. or: object.Print Tab(11) This message begins on column eleven, which is functionally identical.

Tab()&Spc()
Obviously, Spc() inserts spaces

in the output Tab() sets the position where the next message will appear.

It is also possible to use more than

one text expression on the same line When you need to combine multiple text expressions for use with a single object.Print statement, the syntax starts to get a bit cluttered. After each text expression, you can tell VB where to put the next expression. There are three ways to do this

First, you can place a semicolon after a

text expression. This puts the insertion point immediately after the last character displayed.That is, the first character in the next expression that prints will be immediately after the last character in the expression preceding the semicolon. For all practical purposes, this behavior makes the semicolon act just like the & concatenation operator. In fact, the last line of the preceding example could have been written like this

object.Print This message; sMsg If you place a semicolon after

number, this will add a space between the preceding number and the next number

Private Sub Form_Load()

Print 34; 45
Print Spc(1); 34; 35

Show
Print "hello" Print Spc(1); "hello" End Sub

Second, you can use the Tab() function to

move the insertion point to a specific column. If you want some space between your messages, you might try something like this: object.Print Thats ; Tab(20); one mighty leap for VB What happens if you specify a Tab position that would cause part of the previous text expression to be overwritten? The first text expression in the preceding example is 6 characters long, for example. What

Finally, you can use Tab with no

argument (remember to omit the parentheses too; otherwise, VB will generate a syntax error) or use a comma (,) to position the insertion point at the beginning of the next print zone. (On average, a print zone occurs about every 14 columns.)

Print a;Tab;b,c

Print 1,2,3
Remember that all the semicolons

and Tabs are optional. If you dont specify where to place the next character, it will print on the next line. You can use these formatting rules to produce output in a variety of ways

The difference between semicolon and comma seperator


Visual Basic provides two charactors a

comma and a semicolon that you can use to align the output listed in the Print methods outputlist. The comma tells VB to tab to the next print zone before printing the next item. In VB, a new print zone begins after every 14 print positions. The semicolon separator tells VB to advance the next print position before

Visual Basic Expressions


A combination of keywords,

operators, variables, and constants that yields a string, number, or object. An expression can be used to perform a calculation, manipulate characters, or test data

Numeric expression
Mathematical Operator Operator Operation

Precedence Rank ^ Exponentiation Negation 2 */ Multiplication and division 3 \ Integer division (truncates) 4 Mod Remainder of division 5

For example, in the expression

3+9/3*5

Operator

Used to raise a number to the

power of an exponent. Syntax result = number^exponent

* Operator
Used to multiply two numbers.

Syntax
result = number1*number2

Dim MyValue
MyValue = 2 * 2

' '

Returns 4. MyValue = 459.35 * 334.90 Returns 153836.315.

/ Operator
Used to divide two numbers and return a

floating-point result. Syntax result = number1/number2 Dim MyValue MyValue = 10 / 4 ' Returns 2.5. MyValue = 10 / 3 ' Returns 3.333333. Two of the mathematical operators might be less familiar to you; they are the integer division operator (\) and the

\ Operator
The integer division operator (\), like the

standard division operator (/), is used to divide two numbers. Unlike the standard division operator, however, the integer division operator returns an integer (whole number) result. Syntax: result = number1 \ number2 For example, the expression 211\4 results in 52, whereas the expression 211 /4 results in 52.75. Dim MyValue MyValue = 11 \ 4 ' Returns 2.

Mod Operator
The modulus arithmetic operator is

also used to divide two numbers, but it results in the remainder of the division. Syntax: result = number1 Mod number2

Remarks: The modulus, or remainder, operator divides number1 by number2 (rounding floating-point numbers to integers) and returns only the remainder as result. For example, in the following expression, A (result) equals 5. A = 19 Mod 6.7

String Expressions
Concatenating Strings

A string is a group of characters

enclosed in quotation marks. A zerolength string also is called empty string. Connecting (or linking) strings together is called concatenating. In VB you concatenate strings with the concatenation operator the ampersand (&).

To concatenate two strings, use

the & symbol or the + symbol. Syntax result = expression1 & expression2

lblTime.Caption = "The current

time is" & Format(Now, hh:mm) txtSample.Text = "Hook this + to this

When concatenating strings, you must be

sure to include a space before and after the concatenation operator the ampersand (&). If you do not enter a space before and after the ampersand, VB will not recognize the ampersand as the concatenation operator. Dim MyStr MyStr = "Hello" & " World" ' Returns "Hello World". MyStr = "Check " & 123 & " Check" ' Returns "Check 123 Check".

Comparison Operators
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

Unlike the mathematical operators, the

comparison operators (relational operators) do not have an order of precedence. If an expression contains more that one relational operator, VB evaluates the relational operators from the left to right in the expression. Keep in mind, however, that relational operators are evaluated after any mathematical operators in the expression. In other words, in the expression 5 2 > 1 + 2, the two mathematical operators (-, +) will be

All expression containing a

relational operator will result in either a True or False answer only. That is, the result of a comparison operation is a Boolean value (True or False)

If the data type of the operands in

relational expression is String, VB will compare with the ASCII of each character in the strings. For example, in the expression "A" > "B The ASCII of the first character of left-hand side string "A" is 65; on the other hand the ASCII of the fist character of right-hand side string "B" is 66.

logical operators
We will use three logical operators

Operator

Not And Or

Operation Logical not Logical and Logical or

ASCII
The American Standard Code for

Information Interchange (acronym: ASCII; pronounced /ski/, ASS-kee)is a character-encoding scheme based on the ordering of the English alphabet. ASCII codes represent text in computers, communications equipment, and other devices that use text. Most modern character-encoding schemes, which support many more characters than did the original, are based on ASCII.

You might also like