Tuesday, 16 June 2015

Baby Steps into Programming - 1


originally published on : 03/04/2015


If you are new to programming, and wonder what it's like to be able to tell your computer to do, what you want it to, then let me stop you right there, It's amazing. If you have no before hand knowledge of programming, then don't worry, we're here for you. In this first tutorial, I'll teach you to write you first Program in C, C++ and in Java. The three most expounded and compound programming languages. Although, it is recommended that a thorough knowledge of C-based languages aides a programmer to understand other languages easily, but I stand against it and say that Java Does too. You can Pick any Language you want to learn, from our tutorials, for we'll be teaching all three.


Let's Begin with every programmers first Program, the traditional "Hello World" program. First, let us try that out..

{Note : any line written in a code after the two slashes i.e // denote comments. These are given to explain specific parts of the program. They are not the part of code and are Optional.                     }

Let us try the program in C first. Though, there are a few prerequisites for programming that are needed to be fulfilled before you start coding, but we'll discuss those in the later part of this article. You try to understand whats, hows, who's and whens of the code first.

The Hello World Program in C, follows:

#include <stdio.h>                                            //This is a header file and including it gives you
                                                                          //access to predefined functions that you can                                                                                             //use without having to write them yourself,
                                                                          //stdio stands for standard input output, you need
                                                                          //it to take input and give output
int main ( )                                  // - (1)
{
        printf ("Hello World");      // - (2)
        return 0;                             // - (3)
}

We have labelled the different parts of the code to explian them.

(1) You're probably wondering what int main( ) is? Well, in every programming language, the tasks         that are to be performed by the program, are done by functions. Now this arises the question,               what is a function. Well, a function is a specialized segment in a code that performs a specific             task. And you'll find that, across most programming languages, the main function is what drives 
      the elements in a program. You put something in the main function, and it'll run, if you don't
      the equation is simple it wont. Now to define the scope or area of the main function, we need 
      something to create it's boundaries. Well, in C, it's done by these - { }, the braces, curly bracket
      , the third bracket, call 'em what you like. Anything after the int main( )  that falls within these            braces are considered to be a part of the main function. And that's what the computer will run.

(2) printf( ); is a function in the file stdio, that prints, anything on the screen that you write inside it
     in in quotation, like  printf ("Hello World"). It'll then print Hello World on the screen

(3) for this part, all you need to know, for now is that, you have to write it at the end of your code, 
      otherwise it wont work.

Now, let;s try that for C++.
For C++, I'm giving two versions of the program, specified according to it's changing editions. The first version of that code will be according to the ANSI 1989 C/C++ Standard. In other words, for those of you who use, the Turbo C++ 3.0, in the blue screen, that doesn't even run full screen on an O/S succeeding Windows XP. This version is taught even today in most of the schools, and that's why it's important that we discuss it.

So firstly, for the ANSI 1989 Standard












In the above code, you will recognize that we have included two header files, namely iostream.h and conio.h. The file iostream.h is the C++ equivalent of stdio.h and it defines the stream called cout<<;.
Anything that you place in quotation between cout << and the semicolon ; will be printed on the screen. And for the functions, clrscr( ) and getch( ), they are used to manipulate the console. For now all you need to know about clrscr ( ) is that it clears the output screen from any residue that remains from the previous runs of the program. If you don't use it, you might see Hello World from the previous run, when you run the program again. And for getch ( ), for this purpose, holds the output screen, until you press a key. The other aspects of this program are same as that of the one in C.

Now, the important prerequisites. For you to be able to run these programs, you need to have an IDE (Integrated Development Environment) or the software that runs this code. For the time being, you can try your hand at the Turbo C++ 3.0. The dowload link is given below :


Just remember that the functions getch( ) and clrscr( ) can be used in C as well, if you include the header file conio.h. You need to keep in mind that the names of your programs in Turbo C++ cannot exceed 8 Letters. And if you want a program to be in C, you'll have to save it with the extension (.c) e.g. Prgrm.c and for C++ you'll have to use the extension .cpp as prgrm.cpp.

Note : The header file iostream.h is for C++ only, it cannot be used in C. And Turbo C++ can run programs both in C as well as in C++.

That's all for this article. We'll discuss the Hello World program in ANSI C/C++ 2003 Standard and in Java, in the next article, Baby steps into Programming - 2. Also we'll learn some features of the Turbo C++ and about different compilers and IDEs.

No comments:

Post a Comment