Hello, this is a small post before i start posting examples of well written code here; that you might understand easily. This blog is a spot for the students who are new to the programming world, we will be writing code first in the C++ language and when we are done we will move on to the same piece of code in Java, both are amazingly beneficial languages. I will try my utmost to deliver examples of code here that you will enjoy reading and will hopefully understand very well. Lets take our first example of C++ code,
Jumping into the compiler........
#include <iostream>
using namespace std;
int main(){
cout<<"I love cookies";
// or we can write:
cout<<"Anything we want :D"<<endl;
return 0;
}
That's a very short example for now. Lets discuss a few terms written in the code above ,
"#include" This small piece of code does a lot of work for us, it tells the computer through the compiler that we are about to 'include' something in our program, the sign '#' is the Pre-procession sign, it conveys to the computer that "Do this before anything else you idiot." Since the computer highly depends on us.
"<iostream>" This is called a header file, its actually a class, we haven't discussed what classes are but still, we may discuss them soon. This header file has all the methods, okay, i'll just keep it simpler. All this header file does is give our program "The input and output functionality."
So, together, #include<iostream> means: 'Include the input output functionality in the program.'
"using namespace std;" For now we will avoid the background knowledge because that will take the post to a long journey. Lets just say that it means: "use the standard library that is built inn in C++," Libraries are classes built inn already for us, for better functionality and ease and of course, to save time; we can't reinvent the wheel every time we program.
You must be familiar with integers, ints are simply numbers without fractions, this is an integer '10,' while this '12.33' is not. The code 'int main()' is the main method of the program, a method is usually a way by which we perform a certain task, like i want to open someones head, i would do that with a hammer by slamming it on their head. I think we should discuss methods in another post but for now consider it as a 'way of achieving a certain goal.' In our case we want to output something on the screen and for that we have a 'main method,' which is 'int main().' I will mention some things we must remember as we move on to the next pieces of code:
One: A method (in our case 'int main') is a way by which we perform a certain task.
Two: A method has a body, this body contains all the things that the method will achieve and how it will achieve them.
Three: We will always make a body for a method, so that it may define what the method will do.
This how the body of a method looks like:
int main(){
We will put our commands in this empty space, spaces don't matter much in programming, they are just used for better readability of the program.
}The body ends here.
If you look at the program we built above, you would see the command 'cout<<,' the long form is 'Console out,' its used for outputting something on the computer screen. In the program we output a string, a string is a set of alphabets or characters, in the next post we will discuss variables, strings and data types and some benefits of programming, also how we can apply it to the real world or can take it from the real world and much more. For now this much is enough, i will post more examples soon, to get you fired up a bit. We will discuss every end and if you want to run a quick program, run it online here : /http://cpp.sh/.
No comments:
Post a Comment