Topic :Basic syntax of C++ Program - Our first C++ Program. "Hello World":

C++ Basic Syntax:

When we Consider a C++ Program, it can be defined as a collection of Objects that communicate Via invoking each Other's methods. Let us now briefly look into what do class, Object, Methods, and Instance variables mean.



C++ basic syntax:

#include < filename > ;
using namespace std;
main() 
{
    /*this is the body of main() function, also known as block. 
      Everything in between these two curly braces
      will be executed; */
}


Hello World:

Let's create our first C++ Program "Hello World" this program will output only one string. Look at the given code, execute it yourself and next we will discuss each part of this code.



#include<iostream> 
using namespace std;
main() 
{ 
     cout<<"Hello World"; //Simple statement to be executed
     return 0; 
}


Above, we write the program and then execute it. it outputs a message "Hello world". now it time to deep dive into the code to learn how this code work.



#include<iostream>

The first thing In our program that we add is, include statement. this statement tells the compiler to include the header file called Iostream into our program before creating the exacutable. The Iostream header file come with our compiler and allows us to perform input and output operations.



We will discuss about "header Files" in our Libraries tutorial.


We know that our computer is very stupid. It doesn't know how to exactly execute the program and what does ( >> (Output) ) its mean and how to show executable statement. For this things, we have Header file Iostream that define how to use >> (Output) statement and how to show the written code on screen.



Using Namespace std;

This is boilerplate code that almost all C++ Programs will include. For now, We will just use it at the top of Our all Programs. right under the include statements. And We'll discuss this topic in NameSpace Tutorial. for now just don't forget to include it. Notice that this line ends with a Semicolon. The Semicolon is a part of the syntax of C++. it tells the compiler that you're at the end of a statement. The semicolon is used to end most statements in C++. Not putting in semicolon is one of the most common Problems for new programmers. so if your Program doesn't work for some reason, make sure that you didn't leave out a semicolon. Make your habit to terminate each statement with semicolon.





Don't forget to end your statement with ; (semicolon).


Main()
class myclass
{
       /*Not executable*/
}
 
main() 
{ 
     /*This is the only block of code that will execute 
       when we will run our program*/       
     /*we will include class here to execute*/
     /*we will include function here to execute*/
}
 
function()
{
       /*Not executable*/
}

The Main is Special function, that actually the one which will run when we execute our program. The code inside this block will only execute when we will run our program. We will create some function and classes outside of the main function. and will include these function and classes inside the main method to execute. The Section of a C++ Program that begins with Main(), followed by an Opening Brace {, is Called the Main Function. A C++ Program is actually a collection of Functions (small sections of code). The Function called Main() is always required and always the first Function that executed.

In above simple Program, the entire program is Main(). because the Matching closing brace } that follows Main()'s Opening brace { at the end of the Program. Everything between two Matching braces is called a Block.


We will learn more about braces in our Function tutorials.


Cout<<"Hello world";

This is known as output stream in C and C++. Stream is a complicated thing, we will learn about it in Input and Output tutorials. For the time being, just think stream as a door. The data is transferred through stream, cout and it takes data from computer and sends it to the output. For the moment it is a screen of the monitor, hence we use cout for output.



<<

This sign indicates the direction of data. Here it is towards cout and the function of cout is to show data on the screen.

"Hello world", The Thing between the double Quotes (“ ”) is known as character string. In C and C++ Programming character strings are written in double Quotes. Whatever is Written after << and within Quotation marks will be direct it to cout, cout will display it on the screen. there is an semicolon at the end of this string. because its statement. and keep in mind that every C++ Statements will be Terminate by semicolon.



Comments:

There is one more line starting form //(double slash) is known as single line comment. we will discuss about comment in our Comments Tutorial.



return 0;

Terminates the Function Main() and also the Program, returning a value of 0 as an exit code to the calling program. it is standard Practice to use the exit code 0 to indicate that a Program has terminated correctly. Note that statements are followed by a semicolon. by the way, the shortest statements comprises only a semicolon and does nothing.



Pay Attention to detail

In Programming the Details Matter. This is a very important skill. A good Programmer always analyzes the Problem statement very carefully and in detail. you should pay attention to all the aspects of the problem. you can't be vague. you can't describe your Program 3/4th of the way, they say, you know what I mean?. and have the compiler figure out the rest.

Furthermore you should pay attention to the calculations involved in the program, its flow, and most importantly, the logic of the Program, sometimes, a grammatically correct sentence does not make any sense. For example: here is a verse from poem
Though the Looking Glass" Written by Lewis Carol.


Twas brillig, and the slithy toves Did gyre and gamble in the wabe


The grammar is correct but there is no meaning, Similarly, the sentence, "Mr. ABC sleeps thirty hours every day", is grammatically correct but it an illogical.

So it may happen that a program is Grammatically correct. it compiler and runs but Produces incorrect or absurd results and does not solve the Problem. it is very important to pay attention to the logic of the Program.









I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement