x = 2 + 2The answer to this equation should be apparent but it is still represented by the letter x. It is basically a 'holding' spot for when you want to write an equation down and your really do not know the answer to. Say for instance we change the above equation to the following:
456 / 5 * 6 + 99 *46 / .09 = ?Now the answer to that equation isn't very evident. Mixing grammar and math is bad practice and they do not go well together. Instead of using the ? symbol you would use the letter X. Regardless if you know the answer to the above question or not, you do know that the answer IS x, whatever x may be. Variables in math work similar to variables in almost any programming language. The best part of using variables in a programming language is that they can be made up of whatever you can think up. However there are a few rules you have to follow.
- Has to start with a letter
-
- Can not use reserved keywords (we will get into this later)
-
- Can not use mathematical or foreign characters (nothing like ¿ ( á + -↓+? )
-
- Can not be the same as a previous variable.
-
If you abide by those rules you can name your variables anything you want. Be careful though, loosing track of which variables belong to which 'equation' could be a problem. You typically want to name your variables something that will remind you of what it was for. For example, you can use the following equation just fine, x = 2+2. But really that isn't the best way to do it. It would be a lot simpler for everyone if you used the word 'sum' instead of 'x'. Try to keep variables named something that resembles what you are trying to do with it. This is especially helpful when you find yourself using quite a bit of variables, if you just give all your variables random names you may have problems finding which ones you need to reuse.
Declaring a variable
Declaring a variable is the process of informing your compiler that you are going to assign space, on your computer RAM, for that variable. Different variable types will take up different amounts of memory; it varies on different computers as well. Before we get into declaring a variable we will need to decide on a variable type.
A variable type can be anything from an integer to a character. The C++ programming language offers a wide range of variable types. We will not go into them all, some of them are pretty complex and there are just too many to go over. The two types we will cover for now are integers and characters. But before we go over them we need to talk briefly about keywords.
Keywords are words that are reserved by the C++ language. These keywords define certain parts of the language. You can not use these keywords as variable names. You can easily tell when you use a keyword because all compilers will change them to bold text. The keyword for an integer is int and the keyword for a character is char. Most compilers do not allow you to format text in anyway so you will not be able to make any words in bold, the compiler will automatically change them to bold text.
When you declare a variable you will first need to list its type and then whatever words or letters you want to use. For example…
int sum;The above is how you will declare all of your integer variables. Take special note of the semi-colon after the declaration. Just about every line of code you use will end with a semi-colon. This informs the compiler that you are finished with that specific line of code. Now that you know how to declare a variable, let's go into the basic code setup. This basic code setup is what you will typically use when creating small programs. Go ahead and enter the following into a new source code document in your compiler. In most basic compliers opening a new source code document is as easy as going to File > New > Source Code Document. It will vary depending on which compiler you are using.
#includeWhen you are done just hit compile and run, or something similar, and you should see a command prompt come up with the words "Hello World" printed to it. This is the most basic starting point for learning to program in C++. In the next article we will go over what all that code above means after we talk shortly about what a function is and how it works.
using namespace std;
int main()
{
cout <<"Hello World";
return 0;
}
Published by Bulletbutter
http://twitter.com/bulletbutter Learning has to be my most favorite thing to do. Computers come in a close second, mostly because that is the tool I use to do most of my learning. Trying to get my website... View profile
Beginners Guide to HTML: Getting StartedSo you want to make a website, but have no idea where to start? This tutorial is for you. Learn the basics of web programming, how to get started, and how to make your first page.
RAPTOR Programming Language OverviewAn overview of the RAPTOR programming language that is used by the United States Air Force Academy and other colleges as an introduction into programming.- A Short Introduction to Java Programming LanguageJava is word that is all over the Internet today. Java is a programming language back by Sun Microsystems.
Pure Data: A Graphical Audio Programming LanguageAn introduction to the Pure Data programming language with resources for aspiring programmers.- The Semi-colon -- How to Use a Semi-colonMany people are afraid of the semi-colon. They don't know how or when to use it, so they rarely use it at all. Here are two practical tips for using a semi-colon.
- Beginners Guide to C++ Programming - Part 3
- Why Choose a Variable Annuity for Retirement
- The Difference Between Fixed and Variable Resistors
- Beginners Guide to C++ Programming - Part 1
- Beginners Guide to C++ Programming - Part 4
- A Beginner's Guide to Programming IPhone Applications
- How To: Remove Programs Compiled from Source Code in Linux
- Variables add an immense amount of power to the C++ programming language
- Keywords are identified by bold words.
- Part 3 Coming Soon!

1 Comments
Post a CommentExcellent!