Tuesday, 23 June 2015

Using Operators, Variables and Comments

First of all, let us take a look at our next program:


/*
Author: Nikhil Sardana
Program to find sum of two numbers:
This program calculates the sum of two numbers and displays it on the screen

*/

#include<iostream>

int main()
{

int a,b,c; //declaring variables
a=10; //assigning value to a
b=5; //assigning value to b
c=a+b; //assigning value to c
std::cout<<"c="<<c;
//displays the value of variable c on screen

return 0;
}

Save the above code as add.cpp. Compile and Run this program.

The output will be:
c=15

Let us take a closer look.

The program begins with a comment. As said earlier, comments are just for the information of the programmer and are ignored by the compiler. C++ provides two types of comments:

->Multi line comments that begin with /* and end with */. Everything between them is ignored by compiler and considered as a comment

->Single line comments that begin with //. Everything written to the right of these symbols is ignored by compiler and considered as a comment.

It is a good practice to begin your program with a short description about what it does. You can add your own name as the author for the programs that you make yourself.

The first statement in main() function:
int a,b,c;
is a declaration of variables. It declares three variables a,b and c. It also tells the compiler that the variables are of type int or INTEGER. Of course, they cannot store any of the infinite integers, and the range of integers that can be represented varies from compiler to compiler. Typically, for new compilers it is from 2,147,483,648 to 2,147,483,647. For now, it is more thann enough. We will get to what data types are in a little while.

The three variables are separated by commas. The statement can also be broken down as:
int a;
int b;
int c;

The '=' symbol is called ASSIGNMENT OPERATOR and is used to assign or store a value in a variable. As the name suggests, values of variables can be changed as often as we want in our program.
For example:
int a;
a=10;
a=2000;

is completely fine. After execution, variable a will have the value assigned by the last statement, 2000 here.

The statement:
c=a+b;
assigns the sum of a and b to c. This means that, you can store a variable in another variable, or perform some calculation on one or more of them and store the result in a variable. For example,
c=a;
b=a+15;
are valid statements.

The next thing to notice here is the statement:
std::cout<<"c="<<c;
It has two << operators. This is called "Cascading of Operators". This means that two statements:
cout<<"c=";
cout<<c;
Can be combined in a single statement as:
cout<<"c="<<c;

Notice that, if you write c in quotes, it displays the alphabet 'c'. If you write c without quotes in cout statement, it displays the value of c. In the first case, c is considered to be a part of STRING, or text that you want to display. In the second case, c is treated as the name of a variable.

Now, let us see what a data type is.

DATA TYPE is a means to identify the type of data and associated operations of handling it. Simply put, data type tells the compiler that what kind of variable is being declared. C++ provides the following data types:
char: for character values. There are 256 valid characters that C++ can understand. Remember, uppercase and lowercase letters or characters are considered different.
int: for integer values i.e. numbers without any fractional part.
float: for numbers with fractional part. They can be expressed as;
->Fractional form: 3.14159265
->Exponential Form: 3.0E08, which is equivalent t 3.0 X 100000000 i.e. 3.0 multiplied with 10 to the power 8.
double: similar to float, but has a higher range (or "precision").

It is important to note that char is considered to be a subset of integer type. This is because characters are represented in computer memory with their respective number codes, Each character has a specific number code that is used to store it in memory. This code is called ASCII Code. ASCII stands for American Standard Code for Information Interchange.

The next thing to know is the set of rules for variable naming. Following rules govern the construction of valid variable names in C++:
->It must be atleast one character and can be arbitrarily long.
->The first character must be a letter (a-z or A-Z) or an underscore (_)
->It can contain only alphabets,underscore or digits (a-z,A-Z,0-9)
->Special characters and spaces are NOT allowed. Ex: nikhil@123,nikhil sardana are invalid variable names.
->Lowercase and Uppeercase letters are considered different. So a and A are different.

No comments:

Post a Comment