lunes, 7 de abril de 2014

String Variables

String Variables

A sequence of characters is called a string. So far we have been using strings for labeling the output of programs. This chapter will discuss more ways to use strings.

Chapter Topics:

  • String variables
  • Names for string variables
  • What characters can be in strings
  • Changing the contents of string variables
  • INPUT with strings
  • Relational expressions and logical expressions with strings

QUESTION:

In all our programs so far, what does a variable hold?

The sequence of characters "Miles per Gallon" in the following program is a string:
LET MILES = 200
LET GALLONS = 10
PRINT MILES / GALLONS, "Miles per Gallon"
END
So far you have been using strings for labeling the output of your programs or for prompting the user to enter a number. This chapter will discuss more ways to use strings.
Computer programs that work with strings are very common. For example, word processing programs manipulate character data. These programs use string variables to hold the character data.
Remember what a variable is:
A variable is a small amount of computer memory that has been given a name. You (the programmer) think of the name you want to use. The QBasic system finds some memory for that name.
So far in this course you have been using numerical variables to store numbers in the computer's memory. For example, the variables GALLONS and MILES in the previous program are numerical variables.
A string variable is a variable that holds a character string. It is a section of memory that has been given a name by the programmer. The name looks like those variable names you have seen so far, except that the name of a string variable ends with a dollar sign, $.
The $ is part of the name. If you forget to put it at the end of a name, the QBasic system will think you want a numerical variable.

QUESTION:

Which of the following are correct names for string variables?
  • NAME$
  • WORDS
  • person$
  • $sum
  • VAL $

Here is a program that uses a string variable.
LET NAME$ = "Sherlock Holmes"
PRINT NAME$
END
When this program is run the following happens:
  1. Execution starts with the first statement:
    • Since the variable NAME$ has not been seen before, the system finds memory for it.
    • The variable starts out empty—there are no characters stored in it.
    • Now the characters from right of the = are copied into the variable.
    • (The quote marks "" are not copied since they are not part of the string.)
  2. Execution continues with the second statement:
    • The characters stored in memory in the variable NAME$ are printed out.
The following will appear on the monitor screen:
Sherlock Holmes

QUESTION:

Here is a slightly different program:
LET NAME$ = Sherlock Holmes
PRINT NAME$
END
What do you think will happen with this new program? (Hint: this is a trick question.)

The correct program is:
LET NAME$ = "Sherlock Holmes"
PRINT NAME$
END
The quote marks are needed to show what characters are part of the string. Without the quote marks it is not clear what the programmer wants.

QUESTION:

Here is the program again, with a slight change:
LET NAME$ = "Sherlock Holmes"
PRINT NAME
END
What do you think will happen with this new program? (Hint: this is another trick question.)


When a string variable starts out (the first time it is seen in a program) it starts out empty. It contains no characters. This is like numerical variables starting out containing a zero. If a string variable is seen for the first time in a LET statement, then it will immediately filled with characters:
LET NAME$ = "Sherlock Holmes"
     |       ---------------
     |              |
     |              |
     |              +--- 2. but the LET statement will immediately
     |                      put these characters in it.
     |
     +---- 1. when NAME$ is created it will be empty.
Usually you don't need to think about this. However if you misspell a variable name, what your program might do is explained by this rule.

QUESTION:

Here is another program:
LET NAME$ = "Professor Moriarty"
PRINT NAM$
END
What do you think will be printed on the screen? (Hint: this is yet another trick question.)

String variables contain strings of characters. The characters can be any printable character on the keyboard, including spaces and punctuation (but not function keys, the enter key, and some others.) In a LET statement you show what characters you want in the string by starting with a quote (  "  ) then typing in whatever you want, then ending with a quote (  "  ) . The quote marks will not be part of the string stored in memory.
Space is a character. It takes up as much memory as any other character. Here is a program that puts a long string into the variable NAME$ .
LET NAME$ = "Dr. John H. Watson, M.D."
PRINT NAME$
END
All the characters between the first and last " are put into NAME$. The spaces, comma, periods, and upper and lower case letters are copied to the variable exactly as in the LET statement.
A string can have very many characters in it. There is a limit to how big it can be, but you won't need to worry about that.

QUESTION:

How many characters are in the following string? (Remember that the quote marks are not part of the string.)

"A B C D "
 
Look at the following program:
' Assignment with numerical variables LET CASH = 100 LET WORTH = CASH PRINT WORTH ' END The first LET statement creates the variable CASH then puts the number 100 into it:
CASH
100
The second LET statement creates the variable WORTH, then gets a number from CASH to copy into WORTH:
WORTH
100
The number in CASH does not change. The program prints 100 to the screen.

QUESTION:

Could the programmer use another LET statement to change the number that is in WORTH?

Just as with numeric variables, string variables can be changed as the program runs. String variables can be changed:
  • In LET statements using string literals.
  • In LET statements using other string variables.
  • In INPUT statements.
Here is an example of the first two:
' Assignment with string variables
'
LET NAME$ = "Luke Skywalker"
PRINT NAME$
'
LET NAME$ = "Darth Vader"
PRINT NAME$
'
LET PERSON$ = NAME$
PRINT PERSON$
'
END
  1. The first LET statement creates NAME$ and puts "Luke Skywalker" into it.
  2. The PRINT statement prints that out.
  3. The next LET statement copies "Darth Vader" into NAME$, replacing what was there before.
  4. The next PRINT statement prints "Darth Vader"
  5. The last LET statement creates PERSON$. Then it gets characters from NAME$ and copies them into PERSON$.
  6. The last PRINT statement prints "Darth Vader" because that is what is in PERSON$
So the screen will look like:
Luke Skywalker
Darth Vader
Darth Vader

QUESTION:

Do you think you can change a string variable by copying a number into it, like:
LET NAME$ = 45.08
 
 

 


No hay comentarios:

Publicar un comentario