visual basic notes

5
Visual Basic Operators The simplest operators carry out arithmetic operations. These operators in their order of precedence are: Operator Operation ^ Exponentiation * / Multiplication and division \ Integer division (truncates) Mod Modulus + - Addition and subutraction · 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” 4.1 RELATIONAL AND LOGICAL OPERATORS A condition is an expression involving relational operators (such as < and =) that is either true or false. Conditions also may incorporate logical operators (such as And, Or, and Not). The relational operator less than (<) can be applied to both numbers and strings. The number is said to be less than the number if a lies to the left of bon the number line. For instance, 2 < 5, –5 < –2, and 0 < 3.5. The string is said to be less than the string if precedes abalphabetically when using the ANSI (or ASCII) table to alphabetize their values. For instance, “cat” < “dog”, “cart” < “cat”, and “cat” < “catalog”. Digits precede uppercase letters, which precede lowercase letters. Two strings are compared working from left to right, character by character, to determine which one should precede the other. Therefore, “9W” < “bat”, “Dog” < “cat”, and “Sales-99” < “Sales-retail”. Table 4.1 shows the different mathematical relational operators, their representations in Visual Basic, and their meanings 1

Upload: pramodsoni0007

Post on 26-Dec-2015

11 views

Category:

Documents


0 download

DESCRIPTION

visual basic notes

TRANSCRIPT

Page 1: Visual Basic NOTES

Visual Basic Operators

The simplest operators carry out arithmetic operations. These operators in their order of precedence are:

Operator Operation ^ Exponentiation

* / Multiplication and division

\ Integer division (truncates)

Mod Modulus

+ - Addition and subutraction

· 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”

4.1 RELATIONAL AND LOGICAL OPERATORS

A condition is an expression involving relational operators (such as < and =) that is either true or false. Conditions also may incorporate logical operators (such as And, Or, and Not). The relational operator less than (<) can be applied to both numbers and strings. The number is said to be less than the number if a lies to the left of bon the number line. For instance, 2 < 5, –5 < –2, and 0 < 3.5.

The string is said to be less than the string if precedes abalphabetically when using the ANSI (or ASCII) table to alphabetize their values. For instance, “cat” < “dog”, “cart” < “cat”, and “cat” < “catalog”. Digits precede uppercase letters, which precede lowercase letters. Two strings are compared working from left to right, character by character, to determine which one should precede the other. Therefore, “9W” < “bat”, “Dog” < “cat”, and “Sales-99” < “Sales-retail”.

Table 4.1 shows the different mathematical relational operators, their representations in Visual Basic, and their meanings

4.2 IF BLOCKS

1

Page 2: Visual Basic NOTES

An If block allows a program to decide on a course of action based on whether a certain condition is true or false. A block of the form

If condition Then

action1

Else

action2

End If

Causes the program to take action1if condition is true and action2 if condition is false. Each action consists of one or more Visual Basic statements. After an action is taken, execution continues with the line after the If block. Figure 1 contains the pseudocode and flowchart for an If block.

DO LOOPS

A loop, one of the most important structures in Visual Basic, is used to repeat a sequence of statements a number of times. At each repetition, or pass, the statements act upon variables whose values are changing. The Do loop repeats a sequence of statements either as long as or until a certain condition is true. A Do statement precedes the sequence of statements, and a Loop statement follows the sequence of statements. The condition, along with either the word While or Until, follows the word Do or the word Loop. When Visual Basic executes a Do loop of the form

Do While condition

statement(s)

Loop

It first checks the truth value of condition. If condition is false, then the statements inside the loop are not executed, and the program continues with the line after the Loop statement. If condition is true, then the statements inside the loop are executed. When the statement Loop is encountered, the entire process is repeated, beginning with the testing of condition in

2

Page 3: Visual Basic NOTES

the Do While statement. In other words, the statements inside the loop are repeatedly executed only as long as (that is, while) the condition is true. Figure 5-1 contains the pseudocode and flow chart for this loop.

DO UNTILL LOOP

The condition can be checked at the bottom of the loop when the statement Loop is reached. When Visual Basic encounters a Do loop of the form

Do

Statement

Loop Until condition

it executes the statements inside the loop and then checks the truth value of condition. If condition is true, then the program continues with the line after the Loop statement. If condition is false, then the entire process is repeated beginning with the Do

3

Page 4: Visual Basic NOTES

statement. In other words, the statements inside the loop are executed at least once and then are repeatedly executed until the condition is true. Figure 5-2 shows the pseudocode and flowchart for this type of Do loop

FOR...NEXT LOOPS

When we know exactly how many times a loop should be executed, a special type of loop, called a For...Next loop, can be used. For...Next loops are easy to read and write, and have features that make them ideal for certain common tasks. The following code uses a For...Next loop to display a table.

PROGRAM TO PRINT THE NUMBERS

Private Sub cmdDisplayTable_Click()

Dim i As Integer

For i = 1 To 5

picTable.Print i; i ^ 2

Next i

End Sub

4