Monday, February 16, 2015

Programming 101 - Part 1 - The Very Basic in C++ and Java


Welcome to Programming 101. This topic will consist of several parts all in different posts of-course. This post will include the a Hello World for C++ and Java. More posts will follow to explain how the languages work, but don't expect to master the languages by just reading through the content or watching any videos, if and where provided. Experimenting and working out of the box and over what you have learnt leads to becoming a good programmer. I am only writing these to provide very basic understanding of how to write a program.

Lets begin; to start programming you'll need an IDE which is an Integrated Development Environment. I'd recommend an easy simple one: Bloodshed DevC++ for C++, which can be downloaded for free from the Bloodshed website (search it on Google). Or if you wish to pick up the big guns right away then you can get Microsoft Visual Studio and install it for Visual C++. As for Java, I would recommend an open source IDE known as Eclipse, but you are welcome to try Netbeans as well. This topic will feature only console based coding for now.


A basic C++ console program looks like the following so lets say "Hello".

#include <iostream>

using namespace std;

int main()
{
   cout<<"Hello World";
   return 0;
}


The same can be written in Java as (save as Example.java to run):

class Example
{
   public static void main(String args[])
   {
      System.out.println("Hello World");
   }
}


This was easy enough, isn't it? Now let me break the whole thing down for you. The #include and the namespace are telling the IDE in C++ to look for input output codes that are implemented in the rest of the code.


The int main( ) or public static void main(String args[]) is a declaration of the body of the program, everything that is coded in a program is triggered in there. The cout or System.out.println belongs to the output code that tells the computer to print a given string onto the console screen. Notice that the printing has been done inside the {    } right below the main( ), this means main( ) contains this instruction. Everything that has to be structured to execute is written or called inside main( ) of a program.


As for the term class Example used in the Java code, we will get to that in later parts of the Programming 101 set.


Now that you are familiar with the structure of syntax, we should move on to how data is represented and worked with in C++. Stay connected for the next part of Programming 101.

No comments :

Post a Comment

enter comment