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
Baby Steps into Programming - 2
published: 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.
"Insights into Programming"
Baby Steps into Programming - 1
published : 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 a pleasant feeling. 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