Tuesday, 16 June 2015

Baby Steps into Programming - 2

Please click directly on the link. The link on the thumnail has been broken....The Second Part of Baby steps into...
Posted by Code for School on Wednesday, June 3, 2015
originally published on : 4/06/2015



In the previous tutorial, we learnt to write the Hello World program in C and C++, in the ANSI C/C++ 1989 standard. Even though, the standard is taught extensively in schools and colleges of this country, today, the standard stands obsolete. If you want to code in C or C++, you need to be familiar with the latest standard in use i.e the ANSI C/C++ 2003 standard. And the purpose of this article is to acquaint you with those standards. It is only slightly different from it's predecessor, if you've a keen eye, you'll catch onto it quickly. But we'll try our best to explain to you the key aspects of this standard in the following articles. For now, you just focus on the code.

The Hello World program in the ANSI C++ 2003 standard follows:

#include <iostream>                         // - (1)

using namespace std;                         // - (2)

int main (int argc, char **argv)         // - (3)
{
        cout << "Hello World";
        return 0;
}

(1) In this standard, the file iostream lacks the extension .h. For now, all you need to know is that for this standard, the header file is iostream, not  iostream.h.

(2) using namespace std, is a must when programming in this standard. In this context, you need to write this at the top of your program, just after the header files, if you want to use cin and cout. Otherwise, you'll need to use the input output streams as:

       std :: cout << "\n Enter a number ";
       int n;
       std :: cin >> n;

P.S. cin takes input.

(3) int argc and char **argv are called command line arguments. These are the parameters given by the operating system to the code, if there are any. Generally, they are optional, but in some compilers, like Cygwin, they are mandatory. And it's a good practice to have them in your program.

Now, we'll move to the program in Java. but before we do, we need to discuss something important. Java is a complete Object Oriented Programming Language and that is why, everything in java happens in a coding structure called class, even the main function. Follow the program below to understand better :

package myFirstJavaProgram;                                           // - (1)

public class MyFirstJavaProgram {                                    // - (2)

        public static void main(String[] args) {                         // - (3)
           
               System.out.Print("Hello World");                         // - (4)
        }
}

(1) Considering that I'll advise you to program Java in the Net Beans IDE, this will be provided by the IDE

(2) This will also be provided by the IDE, but it requires some explanation. Every Structure in Java is bounded by { }, braces, like in C or C++. This is the class I was speaking of, in which all the code is run. In Java, the class that contains the main function is the public class i.e it is accesible to all the other components of the code. No other component expect this can be public. In java, this class is a must.

(3) Net Beans, will also supply this, but this is the main function or the driver function, as you might have recognised by the literal main. It also accepts command line arguments.

(4) System.out.Print( ) is the java equivalent of printf( ). In java, no header file is required to be included to use this function, it works without them, and also in java, we don't have header files.
Anything placed between the tow brackets ( ) in quotation appears on the output screen.

Now, how to code in these different Standards and languages.  For both of the codes taught today, The Netbeans IDE will work the best. A download link is provided below:


Also, to use C++ on NetBeans, you'll need to setup a C/C++ compiler. A video tutorial on that is available on youtube, the link is given below.


That's all for this article. Try all the codes own your own and share the experience in the comments section. Also, for any queries, inbox our Facebook page. If you want to thoroughly understand what happens in a code and why and learn the theoretical aspects of these languages, follow our Series    

"Insights into Programming"

No comments:

Post a Comment