Ok now we have set up our compiler, we have even created an hello-world program really life is becoming awesome. But that is not where we want to stop we want to create real life program, yes that is what this blog is all about and we ourselves can not wait to get you there, so we will continue by learning about variable yes variable.
What is a variable?
Remember elementary mathematics where they will give you an equation like this "x+1=2" find x
well this mathematical question make us know that x is used to represent something, or maybe say a place holder for a value which in the case of that equation is 1, so in the equation "x+1=2" x is used as a place holder for the value one. x is the variable in the equation it is simply a placeholder or storage location for your data or say value.
So Variable in C++?
Variable in c++ are used to store values so you can use them later in your code
Features of a C++ Variable
A c++ variable has a name also called an identifier, a type which specify the type of data or value that can be stored in the variable, maybe a number, characters, etc trust we will talk further on this, and sometimes a scope specifier which specify where you variable is visible or alive.
Example of a c++ Variable
int x;
"x" is the variable name "int " is the data type you intend to store in x.
Creating variables in c++
To create a c++ variable you need to write a c++ statement which is a line of c++ expression
In the statement you will specify the type and the identifier which you will use to refer to you variable... This is know as variable declaration.
Note. all c++ statement must ends with a semicolon ";" which tells the compiler that you have finish a statement and to take the next codes as another statement. and also white spaces are ignored
Variable initialization
In the above code
int x;
we created a variable or let us say in a more scientific way we declared a variable but this variable has nothing inside it it is just an empty variable that can store an integer(counting numbers)
how do we store a value in our variable? it very simple!
since you have declared your variable you can easily assigned value to it just by using the variable name
x=5;
the above statement just assign the value 5 to variable x so where you use the variable x in your code the compiler understand it as 5 unless you assign another value to it.
you can also assign value to your variable during declaration it is the same thing!!!
int x=5;
Variable naming rules
The name you use for your variable can be anything and can of any length, but i know you want to keep it short and descriptive. like int myFirstNumber;
But it must not be any of the c++ reserved keywords i.e the words that are defined in c++ language itself like " int, char" etc
You should not let you variable contain symbols such as =+-()*&^%$#@! or else you may get an error, be careful my friend.
Your variable can begin or contain or end with an underscore _ e.g int _x;
An example Program
#include<iostream>
int main(){
int x;
x=1;
std::cout<<x<<std::endl;
x=x+1;
std::cout<<x;
}
that is a an simple program that creates a variable x assign the value 1 to it output it, and add 1 to the value stored in it and the output the resulting value!!! we are getting there little by little!!!
note the std::endl means end line which make the console break the line before continuing on the output just like the way we use the enter key!!!
Next tutorial we will be talking about data type because numbers are not the only thing in c++ there is more!!!!!!!!!!!!!!!!!!!!! just stay tuned....
Never miss out any of this blog articles, Receive our regular updates by joining our mailing list, connect with us on facebook, follow us on Google+ connect with us on Pinterest, add us on Tumblr, and follow us on Twitter.
Never forget to promote and recommend this post to others, Remember sharing is caring.
For other suggestions and queries that are not related to this post should be forwarded to us through the contact form or better still you can get in touch with us on WhatsApp with this number +2348163702703.
Enjoy and have fun!
©2017, copyright Ogunleye Peter Opeoluwa
Admin @Idea4novice : Tech Weblog On How Tos, Tips And Tricks
- Blogger Comment
- Facebook Comment
Subscribe to:
Post Comments
(
Atom
)
0 Post a Comment/Comments:
Post a Comment