The Basics of Logic and Data






The Basics of Logic and Data

Lest you forget it, let me remind you again: Computers are not really very smart. They only know how to do the simplest of tasks. If you want them to do anything remotely complex, you have to give precise, step-by-step instructions down to moving individual bits of dataonly 1s and 0s, rememberaround in memory. Fortunately, most of the code you would ever need at that low level has already been written for you, and incorporated into the Windows operating system and the .NET Framework. Microsoft- and third-party-supplied code libraries give you a lot of pre-written functionality that's available for use in your own programs. And that's good, because you would rather be hurtled into space on a giant bungee cord than have to write business applications at the machine code level all day long.

Even though you have all of this great pre-written code in your arsenal, you still have to tell the computer precisely what you want it to do, in fine detail, or it won't do it. And that's where high-level languages like Visual Basic come in. They provide the grammar you need to communicate with the computer. For any given tasks that the computer needs to perform, your job as a programmer is to determine the individual steps to accomplish that taskthe logicand translate those steps into computer-ese using the programming language.

As an example, let's say you receive a request from the sales department for a program that will reverse all of the letters in any chunk of text provided to the program. "Our customers are clamoring for this; we need it by Tuesday," they say. Okay, so first you figure out the logic, and then you implement it in Visual Basic. Using pseudo-code, an artificial programming language that you make up yourself to help you write programs, you can sketch out the basics of this task (with leading line numbers).

01 Obtain the original text (or string) from the user.
02 If the user didn't supply any content, then quit now.
03 Prepare a destination for the reversed string, empty for now.
04 Repeat the following until the original string is empty:
05    Copy the last character from the remaining original string.
06    Put that character onto the end of the destination string.
07    Shorten the original string, dropping the last character.
08 [End of repeat section]
09 Show the user the destination string.

There are many ways that this logic could be written; this is just one example. This pseudo-code can now be converted into your language of choice; in this case, Visual Basic (don't worry about the syntax details for now).

01 originalText = InputBox("Enter text to reverse.")
02 If (Len(originalText) = 0) Then End
03 finalText = ""
04 Do While (originalText <> "")
05    oneCharacter = Right(originalText, 1)
06    finalText = finalText & oneCharacter
07    originalText = Left(originalText, _
         Len(originalText) - 1)
08 Loop
09 MsgBox("The reverse is: " & finalText)

This source code is now ready to be used in a Visual Basic program. And it also demonstrates several essential aspects of coding.

  • The individual steps of the step-by-step instructions are called statements. In Visual Basic, each statement appears on a line by itself. Long statements can be broken into multiple lines by connecting the lines with a space-underscore pair, as shown in line 07 of the code. When a single statement is spread across multiple lines in this manner, the entire statement is sometimes called a logical line. Because a single logical line often includes only a single primary Visual Basic action (such as the If or Do actions, or the various assignment actions using the = sign), these actions are also referred to as statements.

  • The statements of the code are processed one at a time, from top to bottom. However, certain statements alter the normal top-to-bottom flow of the program, as is done with the Do While . . . Loop block on lines 04 and 08 of the sample code. Such statements are called flow control statements, and include loops (repeating a block of code), conditions (optionally processing a block of code based on a comparison or calculated result), and jumps (moving immediately to some other section of the code).

  • Data can be stored in variables, which are named containers for data values. The sample code block includes three variables: originalText, oneCharacter, and finalText, all of which store text (string) data. The .NET Common Type System allows you to create variables for four primary types of basic data values: text (both single characters and longer strings), numbers (both integer and decimal values), dates (and times), and Booleans (true or false values). You can also build more complex types of data by grouping the basic types.

  • Data is stored in a variable through an assignment. Generally, this involves placing a variable name on the left side of an "=" assignment operator, and putting the data or calculation to store in that variable on the right side of that same equals sign. The statement finalText = "" on line 03 stores an empty string ("") in the variable finalText.

  • Statements can include function calls, blocks of pre-written functionality, all squished down into a single name. Function calls do a bunch of work, and then return a final result, a data value. Function names are always followed by a set of parentheses, which may include zero or more arguments, additional data values supplied by the calling code that the function uses to generate its result.

    The sample code includes many examples of function calls, including the Right function on line 05. This function returns a copy of the right-most characters from another string. It accepts two parameters: the original string from which to extract the right-most characters, and an integer value indicating the number of characters to return. The code Right(originalText, 1) returns a copy of the right-most single (1) character from originalText.

    When using a function in your source code, it acts a little like a variable; all the text of the function call, from the start of its name to the end of its closing parenthesis, could be replaced by a variable that contained the same resulting data. Function calls cannot appear on the left-hand side of an assignment statement, but they can appear almost anywhere else that a variable can appear. For example, the following two lines could be used to replace line 02 in the sample.

    ' Replacing --> If (Len(originalText) = 0) Then End
    lengthOfText = Len(originalText)
    If (lengthOfText = 0) Then End
    
  • In addition to functions, Visual Basic also includes procedures. Procedures bundle up pre-written code in a named package, just like functions, but they don't return a value. They must be used as stand-alone statements; you cannot use them where you would use a variable or a function call. The call to MsgBox on line 09 is a typical example of a procedure call in use.

This sample code listed previously could be made a little more efficient. In fact, it's entirely possible that Microsoft obtained an early draft of this book, because they included a string-reversal feature right in Visual Basic, and called it StrReverse.

originalText = InputBox("Enter text to reverse.")
If (Len(originalText) = 0) Then End
finalText = StrReverse(originalText)
MsgBox("The reverse is: " & finalText)

That's right, Visual Basic already includes a string reverse feature, some of that pre-written library code I keep taking about. Visual Basic includes many such intrinsic functions that are considered part of the language, and that bundle up useful pre-written functionality. Many of these functions appear in the Microsoft.VisualBasic namespace, which is automatically made available to your Visual Basic source code when you create a new VB project.



 Python   SQL   Java   php   Perl 
 game development   web development   internet   *nix   graphics   hardware 
 telecommunications   C++ 
 Flash   Active Directory   Windows