Jump to content
xisto Community
Sign in to follow this  

C++ Lesson 2: Variables Tutorial for C++

Recommended Posts

As i said on other tutorial. This are tutorials from Gaianlurker on the New Engin3 forum.
Alright, this tutorial was going to be Variables and Functions, but that would just be massive so I broke it down. Let's start with a basic program;

#include <iostream.h> //cin and cout functions are hereint main(){ int a, b;   cout<<"Welcome to GaianLurker's basic math thingy."<<endl;   cout<<"Please enter a number."<<endl;   cin>>a;   cout<<".. and another?"<<endl;   cin>>b;   cout<<a<<"+"<<b<<"="<<a+b<<endl;   cout<<a<<"x"<<b<<"="<<a*b<<endl;   cout<<"The remainder of "<<a<<"/"<<b<<" is "<<a%b<<endl;   return 0;}
int a, b;

This is a declaration line of variables "a" and "b". Both of these variables are integers (for now), so they are given the type "int". Other types include char(acter), string, double, and there are modifiers as well. For instance, you may want to use an "unsigned short integer", which would limit the size of the variable, and not allow it to hold a negative number. Since this is the variable tutorial, any questions regarding variable types can go here, but please don't rush with modifiers and such as we're not there yet, by far.

Alright, anyway.. I rant a lot so.. I'll try to be more on-track as we go along.

To declare a variable, the syntax is:

type name;

You can declare several variables of the same type as such:

type name, name, name, name;

So for example, we may have:

int i, j;
string username, password;
char favletter;

all within the same .. declaration area (trying to avoid the word scope for now).
You can see some basic manipulation of variables in my example, but let's get more in-depth.

Math things:
There is a math.h header file that includes functions such as sqrt() (for square roots.. duh?) but for now, we're sticking to the basics.

To add variables, simply type "a+b" (no quotes)
To subtract variables, type "a-b" (no quotes)
To multiply, a*b
To divide, a/b
To get the remainder of division (no long division is done): a%b (5%2 = 1)

Order of Operations are followed, and you can use ()s to give priority, as PEMDAS dictates.
Variables may be mixed with numbers, as long as the math makes sense.
ex: if a = 5.. a/5 = 1, and 5/a = 1
New concepts (stuff you didn't use in algebra.. exactly..):
C++ actually means "C +1"

You can take a variable, and "increment" or "decrement" it with ++ or -- respectively.
For example;
a++; //a now equals 6
++a; //a now equals 7
a--; //a now equals 6
--a; //a now equals 5

Also, you can assign variables on the fly.

c=a; //c now equals 5
c=c+b //c now equals a+b.. or 5+3.. or 8
You can always make something equal to itself, plus another variable in that format.
x = x+y <--- will make x now whatever x+y was at the time.

Another syntax for this is x += y.. This is easily read as x additionally equals y (I made that up.. you may not see that anywhere else or it may be how it is said.. xD)

.. but What else does this work for?

x = x-y .. does this equal x -= y? .. Sure it does.

What about x = x*y? .. Not necessarily, and as far as I know, not at all. I suggest sticking to x = x*y as opposed to x*=y. Only use the shorthand for + and -. Don't forget that to declare something, the left side of the equal sign must be a variable ALONE. No coefficient or exponent is allowed on the left side.

5*x = y is NOT allowed. y = 5*x is, however.

I'm running out of things to remember, if you think I've missed something, please let me know.. or I'll explain it if/when it comes up.
Typecasting. >.> What's that?

Well, sometimes you're going to need to make a variable hold a different type.

Say you want to use an operation that only double/float (numbers with a decimal) can perform... yet you want to perform this action on an integer value.

Well, that's simple. An example:

int a=5; //Yes, you can give them initial values when declaring variables

double(a).. blah blah.

That's typecasting. We may or may not see it later, but in case you ever find a need for it, simply put the new (temporary) type, and the variable in the syntax type(variable). This will behave exactly like the variable in the ()'s as if it was that particular type.

Oops, when I wrote this up I forgot about constants. Constants are variables that never change.

For example, you want variable c to = a*b, no matter what a and b are.

int a, b;
const int c=a*b;

a=5; b=3; //c=15
a=3; //c=9 now
b=5; //c=15 again.

Get the idea? It's not a hard concept to grasp, just something I forgot about until the last second.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.