Showing posts with label 1. Show all posts
Showing posts with label 1. Show all posts

Tuesday, 8 January 2013

WHAT I NEED TO KNOW BEFORE I START

INTRO 


C++ (read as c plus plus) is one of the oldest programming languages. It was developed by Bjarne Stroustrup in the 1980s, in the AT&T Bell Labs. He found C lacking for simluations and decided to extend the language by adding features from his favourite languages simula 67.

CHARACTER SET IN C++:

Letters:     A-Z, a-z
Digits:       0-9
Symbols:  !, @, #, $, _, -, =, (), [], {}, ^, &, etc
Spaces:    blank space, vertical space, horizontal space

Important terms that you must know:-
 Tokens: 
  The smallest individual units of a C++ program are called tokens
   e.g. main, void, {} .etc
 
    C++ has the following tokens:
    (i) Keywords:
        Keywords are those reserved words which convey a special meaning to the compiler. These cannot be
        used by a programmer for other purposes than for the one it is already reserved.
        e.g. void, main, int, float, if, else, include, getch, cout, cin,.etc

    (ii) Identifiers: 
        An arbitrarily long sequence of letters and digits,  which are used for general terminology for given
        names to different parts of program like variables, objects, classes,.etc are called identifiers.

    (iii) Literals: 
         Literals are those data items that never change there value under any circumstances(or during the
         program run).
       
         Types Of Literals:
          1. Integer-Constants:
              Whole numbers (without fractional parts) are integer constants.
               e.g. 12, 30098, -96, -147
          2. Floating-Constants:
              Numbers having fractional parts are called floating point constants. They are also called real
              constants.
              e.g. 12.03, -99.99, 125.0
          3. Character-Constants:
              One character enclosed in single quotes ( ' ' ) is called a character constant.
              e.g. 'a', 'Q', 'Z'
              ESCAPE SEQUENCE:
              A backslash followed by one or more character contstants are called escape sequences.
              e.g. '\n' (used for new line), '\t' (used for horizontal tab), etc.
              These are non-graphic characters.
          4. String-literals:
              Multiple character constants enclosed within double quotes are called string-literals.
              e.g. "QWERTY", "Hello World"
 
     (iv) Punctuators:
           Characters used as separators are called punctuators.
           e.g.     (  )  {  }   ,    ;    :     etc
         
     (v) Operators: 
          Tokens that trigger some computation when used are called operators.
           e.g.  the + operator triggers addition, the * triggers division

          Types of operators: 
          1. Binary Operators:
              These operators are used with two operands.
               e.g.  + (addition), -(subtraction), *(multiplication), / (division)
          2. Unary operators:
              These operators are used with two operands.
               e.g. ++ (increment operator), -- (decrement operator), & (address operator) etc
          3. Ternary Operators:
              These are used with three operands.
              There is only one ternary operator-the conditional operator.
                     (condition1)?(condition if true):(condition if false)

So, thats all the basic stuff anyone has to know before taking a first look at a C++ program. (•‿•)
           











FIRST C++ PROGRAM

INTRO

Now that we know the basic details  about tokens, the building blocks of a program, let us go on and build a program ourselves!
But wait, first let's take a lookat  the C++ IDE (IDE is the acronym for Integrated Development Environment).


To open a new file click on File and then New.



Now, lets go ahead creating our first program.

//PROGRAM : 1
//To print a string (remember that strings are character constants enclosed in double quotes.) 



type the above code in your IDE. Then compile it. 


Then similarly click on Run>Run to run the program. After that click Window>Output.


the output window appears, with the output of our program.


Click on the small green square on the top left corner to close it and return to the IDE.

So, what's happening here? How did our program run and what made it run?

The iostream.h is a header file (which explains the .h file extension) which is a pre-written code file conatining many functions and their definitions. So, we included iostream with the keyword 'include'  to enable input/output facilities in our program (though we don't take any input, the string we displayed is considered as an output). So, we tell the compiler that first you include the required header file(s) and then you begin the program execution.

Next, the program execution begins from the main() part. The general syntax of the main() function is:

Firstly, the type of the main function is first written. In this case it is void, which tells the compiler than the main() function doesn't return any value. Next, the program body is written inside curly braces{}.The main() is where program execution starts. You can also define your functions (we will come to that later).

Then comes the function cout. cout stands for "console output" whose definitions are written in the iostream header file (so you now see why it is important to include them. with the increasing complexity of the programs, you'll appreciate their importance), which we have already included.
The "put to" operator << is used to indicate what we want to print followed by our string "MY FIRST C++ PROGRAM". Here, the task of  >> is to print whatever string succeeds it.

So, you see the codes that you've written follows some 'logic'. So, in programming what matters is the logic. Hence, if your logic is correct, the programs you design are correct, if anywhere you become illogical, the program too becomes incorrect.

So, I hope you got how to print a string in C++. If you have any queries, please feel free to contact me at
lostsoulofthegeek@gmail.com.
:)


POINTS FOR YOU TO PONDER ON:-
1. Why do you think we included the header file called iostream.h in the beginning and not in the end?
    (HINT: Use the notion of logic I just mentioned.)

2. Why is are pre-written header files provided to us so that we may use them even though we can write codes for almost anything?

[{ ANSWERS IN NEXT POST }]