Chapter 3 of Visual BASIC
Comments
Comments are just as important in Visual BASIC as they are in any other language. Without them, code that you wrote when you were up until 5AM may not make any sense. With the comments you can leave notes as to why you put a line or block of code there. Comments are always ignored by the compiler and will not affect your program in any way. To leave comment in Visual BASIC we use the apostrophe (‘).
Variables
In Visual BASIC before you use a variable, you must declare it. To declare a variable you must first decide of which type it will be. Like the components, every variable type has a prefix that you should add to each variable. Here is a list of VIsual BASIC variable types and their prefixes:
| Name | Description | Prefix | Range |
| Boolean | True or False | bln | true or false |
| Byte | Single byte of data | byt | 0 to 255 |
| Char | Character | char | 0 to 65535 |
| Currency | Money | cur | -922 337 203 685 477.5808 to 922 337 203 685 477.5807 |
| Date | Date/Time | dtm | 0:00:00 (midnight) on January 1, 0001 through 11:59:59 PM on December 31, 9999 |
| Decimal | Decimal Data | dec | 0 through 79 228 162 514 264 337 593 543 950 335 |
| Double | Decimal | dbl | 4.94065645841247E-324 to 1.79769313486232E308 |
| Integer | Whole number | int | -32,768 to 32,767 |
| Long | Whole Number | lng | -2 147 483 648 to 2 147 483 647 |
| Object | When declaring something as a component | Same as component | Object |
| Single | Decimal | sng | 1.401298E-45 to 3.402823E38 |
| String | What’s in the quotation marks | str | Any writable character |
The Char datatype is actually an integer because the number coincides with a character. For example 65 is A, 66 is B, and so on. Object is just a place holder for the components included (and ones you make yourself). You can have a segment of code that creates a new object or component. We will cover this more in later areas as it deals with the objectpart of Visual BASIC.
To declare a variable, we use the Dim statement. What the Dim statement does is tells the compiler that what comes next will be variables. After the word Dim, we put the name of a variable. There are some rules other than using the prefixes that we follow when naming variables, they cannot be more than 255 characters long, must begin with a letter, and should be meaningful. You should also keep in mind that variables can only hold one value at a time. This doesn’t mean we can’t use x as a counter, it is common to use a one letter variables for counters. After the variable name, we put the word as and then the variable type. Here are some examples of variable declaration:
Dim charLetterGrade As Char Dim decGrade As Decimal Dim blnChoice As Boolean |
If you do not declare a variable before you use it, you will get an error. Unlike other languages, Visual BASIC automatically sets your variables to 0 so there’s no need to initialize them.
Constants
Say you wanted to write a program that had the salestax variable to 6%. You wouldn’t want that to ever change, so you could make that a constant. A constant works the same way as a variable, but it uses the keyword Const before the data type, instead of Dim and must be set in the same line of code. The same rules apply to constants as they do with normal variables, but with constants it is a good idea to capitalize them.
| const DBLSALESTAX as decimal = .06 |
Math
In order to make variables appealing, we need to be able to do mathematic operations in them. In programming the computer does mathematical operations in the order of operations (PEMDAS).
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| Mod | Modulus Division |
| ^ | To the power of |
| += | Variable is equal to itself plus another |
| -= | Variable is equal to itself minues another |
| *= | Variable is equal to itself times another |
| /= | Variable is equal to itself divided by another |
Modulus division is taking a number dividing it by the number after the modulus sign (Mod) and then taking the remainder of that would be modulus. In other words 56 % 10 would be 6 because 56 divided by 10 if 5 remainder 6. You’ll see more of this in later sections dealing with functions.
It’s easy to use these other operators in a variable:
| const DBLSALESTAX as Double = .06 | ‘defines a constant and sets salestax to .06 |
| Dim intAge as integer = 1 | ‘Sets dblAge to 1 |
| charGrade = “D” | ‘Sets charGrade to D |
| strName = “Sam” | ‘Sets strName to Sam |
| decNum = txtNum.Text | ‘Sets decNum to what a user enters in the TextBox |
| intAge += 5 | ‘Makes intAge equal to itself plus 5 |
By now you should have an understanding of how variables work. Remember that variables declared in a submodule are native to that submodule and therefore cannot be used in other modules. Characters are enclosed in double quotes (“), not single quotes. If you don’t have a good idea on how they work then reread the tutorial. In the next section we will be getting input from the user and printing out that data.
Next Section: Input, Output, and Closing
Chapter 3 of C++
Comments
Comments are pivotal for C++ and every other programming language. They allow you to leave remarks to yourself and anyone else who may read your code. Without them, you may be wondering why you did something that looks perculiar to you. They are also good to see how your program would work without a line of code. There are two types of comments, single line and multiline. A single line begins with // and will continue from that point until the end of that line to be a comment. Multiline comments start with a /* and end with */. Comments are always ignored by the compiler and will not affect your program in any way.
// This is a single line comment/* This is a multiline comment */ |
Variables
In C++ before you use a variable, you must declare it. To declare a variable you must first decide of which type it will be. The range of the variable will vary depending on your system:
| Name | Description | Range |
| char | Character | -128 to 127 or 0 to 255 |
| int | Integer | -2147483648 to 2147483647 or 0 to 4294967295 |
short int short | Short Integer | -32768 to 32767 or 0 to 65535 |
long int long | Long integer | -2147483648 to 2147483647 or 0 to 4294967295 |
| bool | True or False | true or false |
| float | Floating point number | 7 digits |
| double | Double floating point number | 15 digits |
| long double | Long double floating point number | 15 digits |
| wchar_t | Wide character | 1 wide character |
Look at the char data type. The reason it has numbers listed for the range is because all ascii characters are numbers. Say you wanted the letter A, it could be represented as the char A or as a 65 because 65 is the equivalent to A. They go in order from there, 65 is A, 66 is B, etc. A char is not just for printable characters, some won’t be able to be printed on the screen. Normally you won’t need to know the whole ASCII chart, but as long as you know that not all ASCII characters can be printed, then you should be okay.
String is not a built in data type, but actually consists of zero or more characters put together. If we need to we can add the string data type by including the string library up at the top where we include the iostream:
Now that we have all the data types that are commonly used, let’s make some variables. The first thing you’ll want to do is to declare the variable. First pick a name for your variable, remember to give it a meaningful name, no spaces in the variable name, no special characters just letters and numbers, and the first character has to be a letter. Also remember that variables can only hold one value at a time. Now that we have selected the name and the datatype let’s put it to use:
double x; x=5; //This will only work if you have the #include <string> at the top string name; name=”Bob”; |
If you do not declare a variable before you use it, you will get an error. If you do not initialize a variable (set a default value to it) and use it then you will get garbage. What will be in your variable is what is left over from somewhere else on your machine. Take note of the semicolons. Sometimes the error messages will be specific and say expected semicolon, sometimes they will not and it might take you hours to figure out that all you were missing is a simple semicolon.
Constants
Say you wanted to write a program that had the salestax variable to 6%. You wouldn’t want that to ever change, so you could make that a constant. A constant works the same way as a variable, but it uses the keyword const before the data type and must be set in the same line of code.
| const double salestax = .06; |
Math
In order to make variables appealing, we need to be able to do mathematic operations in them. In programming the computer does mathematical operations in the order of operations (PEMDAS). The operators are part of the C++ language, and there are many more inside of the math and cmath libraries.
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| % | Modulus Division |
| += | Variable is equal to itself plus another |
| -= | Variable is equal to itself minues another |
| *= | Variable is equal to itself times another |
| /= | Variable is equal to itself divided by another |
| ++ | Variable is equal to itself plus 1 |
| — | Variable is equal to itself minus 1 |
Modulus division is taking a number dividing it by the number after the modulus sign (%) and then taking the remainder of that would be modulus. In other words 56 % 10 would be 6 because 50 56 divided by 10 if 5 remainder 6. You’ll see more of this in later sections dealing with functions.
It’s easy to use these other operators in a variable:
| const double salestax = .06; | //defines a constant and sets salestax to .06 |
| int x = 1; | //Sets x to 1 |
| int y, f; | //Declares y and f |
| char grade=’D’; | //Declares grade and sets it to D |
| string name; | //declares a string called name |
| name=”Bob” | //Sets name to Bob |
| y = f = 0; | //Sets y and f to 0 |
| double t = 2.5 * 2; | //Sets t to 5 |
| int c = 0; | //Makes c equal to 0 |
| c++; | // Makes c equal to 1 |
| c–; | //Makes c equal to 0 |
| c–; | //Makes c equal to -1 |
| ++c; | //Makes c equal to 0 |
| –c; | //Makes c equal to -1 |
By now you should have an understanding of how variables work. A few things I need to point out is that the variable c can only be declare with the int statement oncein that block of code. What I mean by that is that if you declare a variable c in an if statement then that c is native to that if statement and cannot be seen anywhere else in your code. Every time you declare something in a block of code, you can’t declare it again, you must use it withour the leading data type. Also notice the variable called name. When setting a string variable you must enclose it in quotation marks (“). For chars we need to use single quotation marks (‘), otherwise it will not work If you don’t have a good idea on how they work then reread the tutorial. In the next section we will be using the variables to print out data and take input from a user.
Next Section: Input and Output
Chapter 3 of Fundamentals of Programming
DO LOOPS
Up until this point if we wanted to print out a variable, or do a calculation we would need to write it over and over again. Loops allow us to do the same calculation until a condtion is met. The dowhile loop is common to many languages and its algorithm usually goes something like:
X = 0 DOWHILE X <> 5 X=X+1 LOOP |
What that does is takes X and initializes it to 0. Then we make the loop run until X = 5. The X=X+1 is called a counter. What this does is count the number of times the loop goes around. If X was initialized to 5 off the bat, then the program would skip to the line right past LOOP, because it kind of works like an IF statement in that it will check to see if X=5 and if it isn’t then it will loop. The 5 is actually called a sentinel value, or value that causes the loop to stop.
REPEAT UNTIL
Similarly we can use a different form of loop. The REPEAT UNTIL loop checks for the sentinel value at the bottom of the loop. So if we did something like:
X = 5 Repeat X=X+1 Until X <>5 |
The result would be an infinite loop. Why you ask? Because X is being initialized to 5. It runs through the loop once because it only checks at the bottom of the loop, and because we increment X to 6, the loop will continue to run indefinitely because 5 will never be reached since all we are doing is incrementing it.
FOR NEXT
FOR NEXT loops are used when you know how many times you want the loop to run. Say you want the loop to run only 3 times, you would use a FOR NEXT loop, over the others because FOR NEXT loops have built in capabilities that don’t require outside counting. The algorithm for FOR NEXT loops is:
| Name=”John Smith”FOR X = 1 TO 5 print Name Next X |
Again the print is commonly used for displaying information to the screen, in this case it will print out John Smith 5 times. The 1 TO 5 is saying that X, a normal variable will be initialized to 1 and everytime it hits the NEXT command, will increase by 1.
ENDS
Now say you are getting information from a user, and if the user enters a -1 as a value, you want the loop to end, but if not then you want it to keep going. To do this you will need the END and the form of loop. END DO, END FOR, and END REPEAT are all perfectly fine algorithms for popping out of loops when the sentinel value is entered. For example:
| DO INPUT “Enter the student grade” INTO X IF X = -1 THEN END DO ENDIF LOOP |
TOTALLING
It isn’t uncommon for programs to use totalling techniques. The easiest way to total in a loop is to use the variable1 = variable1 + Variable2. Let’s use the previous example and look at it:
| DO INPUT “Enter the student grade” INTO X IF X = -1 THEN END DO ELSE Total = Total + X ENDIF LOOP |
Next Section: Arrays