Tuesday, 23 June 2015

Some Basic Concepts, C++

C++, Some Basic Concepts


Before we proceed further, I think it is important to know some basic concepts.

Firstly,let's talk about what Binary Number System is. It has only two numbers, 0 and 1. As such,any computer or other electronic device can have only two possible states, either ON(1) or OFF(0). At the lowest level, a machine can understand only these two values, nothing else. So, the input and output voltages to electronic devices are either HIGH(1),or LOW(0).

So,in order to tell the machine what we want to do,we must convert the program (or "code") written in C++ (or almost any language) into machine readable code, it's binary equivalent. C++ provides two tools or this purpose: Compiler and Interpreter.

When we pressed Ctrl+Alt+F9 key combination in our first program,our program was COMPILED. This means that the code you wrote (called "Source Code") was converted into assembly instruction text (or "Object Code"). If you open up the folder where you saved "helloworld.cpp" you will see another file named "helloworld.obj". This is like an intermediate stage of your program.

Another purpose of the compiler is to check for Errors in your program,typically called "Syntactical Errors". These errors occur when rules of a programming language are violated. The Syntax of a language is like it's grammar. To know a language, you must be familiar with its grammar. :)

The next thing we did was INTERPRETING the code. The interpreter is similar to the compiler, in the sense that it converts the Object Code generated by the compiler into machine code, by converting and executing the code line by line. A file named "helloworld.exe" is created in the folder, which is an executable file.

OPERATORS are some "Special Symbols" that perform a specific task. For instance, in maths, The '+' operator means addition. Operators in C++ have meanings similar to their meaning in the real world, except for a few cases.

KEYWORDS of a language are the "Special Words" that convey a special meaning to the Compiler. The words int, main ad return are examples of keywords. (NOTE: cout is NOT a keyword! It's an ostream class Object.)

VARIABLES are just the opposite of KEYWORDS. They are used to name different parts of a program. They are not reserved by C++ and don't have any meaning unless we specify it. Of course, not anything can be a variable name. There are rules that govern the formation of variables. Some examples of valid Variable names are: Alice,Bob,TomCat,_coder123 etc.

Statements written inside { } curly braces are considered a "Block of Code",meaning they are considered to be a single unit.

More than one statement can be written in one line. For example,

cout<<"Hello World"; return 0;

will produce just the same result as before. Remember, they are written in the same line, but they are TWO independent statements, each ending with a semicolon.

COMMENTS are the lines that are ignored by the compiler. They are there in the code, but are not executed. The main purpose of comments is to make the program more readable and understandable. Comments can make the purpose of your program and specific parts of your code more clear, and are extremely helpful for large programs, during further development and debugging. Like they say, "A stitch in time saves nine!"

DEBUGGING is the process of finding and removing errors (or bugs or incorrect parts) of the program. For instance, imagine you want your program to arrange some numbers in ascending order (it is called SORTING and is used a lot). But, your program arranges them in descending order i.e. it does not do what it is intended to. The "bug" or error needs to be found and corrected. (Here the error that comes up is a LOGICAL ERROR i.e. the logical structure of the program is not what it should be. Remember that logical errors are NOT detected by compilers, and are generally the hardest to detect.)

Our next program uses basic Operators,Variables and Comments.

No comments:

Post a Comment