Monday, February 24, 2014

[Tutorial C++] Basics of Programming Part 2

In this tutorial I will teach you about variables and how to do calculations using them. If you haven't read the first part then I recommend that you do go to the below link and read the first tutorial as it is an essential one.

http://www.cppfuzz.com/2013/11/tutorial-c-basics-of-programming-part-1.html

Variables

Variables are used to store data in every programming language. It can then be used to perform calculations or to read the stored data. Before you can use a variable you to declare/initialize it. The below example shows how to initialize and store data in a variable (remember its not a program).
int variable; //declaration of a variable

variable = 2; //intialization of a variable

You would have noticed these "//" and would want to know what they are for. Well these double backslashes are called comments and whatever is put after them would not be printed though it would be on available for a single line. If you want to do multiple lines you need to start your comment with "/*" at the beginning and at the end put "*/" and do use them without the double quotes.
Okay so by now you now how to initialize and declare a variable but what to do you need them for and what can you do with them. In simple words you use variable to store data and then later perform calculations on them. You shall understand it more easily with the next example in which we shall make a program.
Suppose you want to calculate a user's age and you also want to know their year of birth. So instead of asking the user his age and year of birth you can only ask him his year of birth and then calculate his age. Let the magic begin then.
#include<iostream>



#include<conio.h>







using namespace std;







int main()



{



int birthyear, age, currentyear=2014; /*You can declare multiple variables by putting commas at the end of each and can also initialize it together with declaration and look that we are writing a multiple line comment here*/







cout<<"Enter your birth year"<<endl; /*We ask the user to enter his birth year and endl is used to end a line*/







cin>>birthyear; /*cin command is used to take input from user and then store it which in this case will be stored to birth year*/







age = currentyear - birthyear; /*Here we are calculating age which is much like algebra*/







cout<<"Your age is "<<age; /*Age is being displayed here, notice that variable is written after the text and the space given at the end of the text, try removing the space and then see what happens*/







getch();

}


Here the end result.


Well I am using a new style here that is explaining each piece of code using comments so if you do not like it please tell and if there is majority then I might revert to the old style. I hope you do understand everything and a piece of advice that every professional programmer will give to a beginner don't copy paste this instead type everything and do experiment with it. Well that's all for now. See you in the next tutorial.

 

Copyright @ 2014 CPP Fuzz.