Monday, February 16, 2015

Programming 101 - Part 2 - Working with Numbers and Words

Expressions and Data Types

There are six basic atomic data types in C++;

char, int, float, double, void, bool

Any other data type that we may come across are derived from these six. The first listed data type is a single character type, you can save an 'A' or an 'h' but not a complete sentence... we will get to that later on. The second type is of an integer or a number. This can hold a large numerical value... from -32,767 to 32,767. Of-course you can change it to an unsigned form by adding the "unsigned" keyword before "int", the numerical range than gets from 0 to 65,535 - unsigned positive range only.

The third data type is a decimal type with 6 digits of precision. The fourth increases decimal to 10 digits of precision. Both are almost the same but there applications can vary based on the requirement of a program.

The fifth data type is the most distinct one, its name is quite clear, its plain void, meaning the data represented by this type is not zero, not null, just nothing. Seems pretty weird but it isn't. There are many occasions when a program is not able to produce any result, but no result can not always be a zero, sometimes it is appropriate for it to be just nothing. This will get clear once experience is attained in programming for different scenarios and methods that only do something but return nothing.

The sixth type is a binary type with only 2 possible value; 1 or 0, and we prefer to use this type when checking for, or saving true and false conditions or applying checks similar in nature. Normally the value "true" or "false" is directly assigned to a bool variable.

With these data types, variables can be made and used as expressions in a program. It is simple to just say we need an integer "variable_1" and an integer "variable_2", both contain certain numeric values, and we require to perform an addition on the variables and get the answer to that.

int variable_1 = 5;
int variable_2 = 8;
cout<<variable_1 + variable_2;

The above example will display the sum of the two variables on the console screen. Similar expressions are use through out computer programs to calculate results and assign input from user onto variables. You should play around with such expressions to get familiar with them. Some sample expressions for each data type are as follows.

int variable_1 = 25;
char character_1 = 'F';
float variable_2 = 12.52;
bool check_1 = true;


Strings are a derived data type that is based on a series of character (or char) variables sewed together in a sequence. This data type is one of most widely used type and has as much application as the basic types. All programming languages come with String type pre-included for use. This is type is used to handle words, sentences, paragraphs, etc.

string sentence_1 = "This a string!";

As seen in the example, special characters can also be used in a string. These variables are used to store statements that need to be shown to the user when needed or to store information to and from the user and a storage device. We are familiar with error messages in difference computer programs, those are strings, the text in this article is a huge string as well.

There are many operations that can be performed on strings, like searching within a string or appending one string onto the other to form a new string.

string sentence_1 = "This is a string!";
string sentence_2 = "This is also a string!";
string sentence_3 = ""
sentence_3.append(" ");

The above example would result in "This is a string! This is also a string!" all saved and displayed by one string variable.

We have covered the basic data types now and will move towards how to use them in proper statements and explore how conditions are used in C++. Stay connected for the next part of Programming 101.

No comments :

Post a Comment

enter comment