CE 311 K - Introduction to Computer Methods

Lab 6: String Variables & Loops in VB


Contents


String Variables in VB

String variables are used to process user input in Visual Basic.  As you have seen in previous labs and examples, you can use a VB "textbox" for users to enter data to your programs.  The following window shows a project with a form containing a label asking the user to enter some words, a text-box (named "txtInput") for entering the words, a picture-box for displaying the results of running the program and a command-button to initiate the processing of the users entered words. 

We can capture the words that the user types into the text-box by using the text-box.TEXT property, as shown in the following VB code which is associated with the "Go" command-button:

In this code, two variables are declared: x is a string and y is an integer.  The variable x is used to store the user entered text from the "txtInput" text-box through the command

x = txtInput.Text
 

Next, the length of the string x is computed using the function Len( ).

y = Len(x)
 

Now, if we want to print out the last character of the string stored in x, we can use the command

picOutput.Print Mid(x, y, 1)
 

The "Mid(i, j, k)" function picks out the k characters of the string variable i starting at character j.

Now lets add in the rest of the characters of the text that we entered.  We can do this using the code in the following window:

The output looks like:

Well, that works well as long as you know exactly the length of the text that the user is going to enter.  However, what if you don't?  A loop would help!

Loops in VB

One of the most significant advantages of using computers is to do a great many repetitive operations in a fraction of the time it would take a human to do the same thing.  It is “loop” structures that enable us to perform those repetitive operations in the programming language.  In many cases, it is necessary to exit from a loop when or if a certain condition is satisfied.  This exiting process is analogous to the decision-making and branching with “If…Then” statements.  The concept of controlling loops and exiting from loops is critical for most programming languages.  This concept is often called “Flow of Control” in the programming language.  After this assignment, you will have a basic idea about the two main looping techniques that are most likely to be encountered in computer programs for engineering application. 

There are several ways to create the “loop” structure in Visual Basic.  Let’s try out some fundamentals of looping as follows: 

1.  “For…Next” Loops

For n = first_n To final_n

...

Next

 

2.  “Do” Loops

 

The “Do” loop is an alternative way to repeat a block of code.  You can achieve the same results using the “For…Next” loop, but using “Do” loop sometimes makes your code more elegant and intuitive due to its inclusion of an exiting function.

 

Do while (n <= final_n)

...

Loop

The previous program can be rewritten and generalized using a "For" loop:

 

 

Now the input text can be any length up to the length of a string variable.

 

Assignment

 

1.  Implement the reverse text program shown above with the loop structure for output.  Enter some example text in the program to ensure that it works.  (Palindromes are not very good for demonstrating this code, but they are fun.  For example, try:  "Stanley Yelnats")

2.  Using the Taylor’s (or Maclaurin’s) expansion theorem, the function sin(x) can be rewritten as:

 

   

The accuracy of the approximation increases as the number of terms in the series, n, increases.  In principle, the summation results in an exact answer when n becomes infinite.  As a practical matter, the summation is sufficiently accurate for modestly large values of n, say, n = 5 or n = 6). 

 

Write a VB program to determine sin(x) using the first n terms of the series expression and then compare this value with the more accurate value returned from the VB "sin" function.  The values of x and n will be input values.  WHen evaluating the series expansion, make use of the factorial function discussed in class.  The Graphic User Interface should look like the following:

 

 

 

Object Property Setting
Form1 Caption
Name
Sine of x
frmSin
Label1 Caption
Name
Font
Series approximation for sin(x)
lblLabel1
MS Sans Serif, 12-point
Label2 Caption
Name
Font
x =
lblLabel2
MS Sans Serif, 10-point
Label3 Caption
Name
Font
n =
lblLabel3
MS Sans Serif, 10-point
Label4 Caption
Name
Font
Sin(x) =
lblLabel4
MS Sans Serif, 10-point
Label5 Caption
Name
Font
Series approximation
lblLabel5
MS Sans Serif, 10-point
Label6 Caption
Name
Font
Correct value
lblLabel6
MS Sans Serif, 10-point
Text1 Caption
Name
Font
none
txtText1
MS Sans Serif, 10-point
Text2 Caption
Name
Font
none
txtText2
MS Sans Serif, 10-point
Text3 Caption
Name
Font
none
txtText3
MS Sans Serif, 10-point
Text4 Caption
Name
Font
none
txtText4
MS Sans Serif, 10-point
Command1 Caption
Name
Font
Go
cmdGo
MS Sans Serif, 10-point
Command2 Caption
Name
Font
Clear
cmdClear
MS Sans Serif, 10-point
Command3 Caption
Name
Font
Quit
cmdQuit
MS Sans Serif, 10-point


To Be Turned In

1.  String code: 

2.  Sine expansion


McKinney | CE311K Civil Engineering | UT Austin