Monday, October 12, 2015

Learning C++ Breaking down a simple program

We'll dissect a simple program made in C++ today. This program asks user for an input and then tells whether the number is even or odd.

The complete code for an even odd detection program is here

#include <iostream>
using namespace std;

int main()
{
   int a;
   cout<<"Please enter a number"<<"\n";
   cin>>a;
   if (a % 2 ==0)
   {
      cout <<" The number is even" <<"\n";
   }
   else
   {
      cout <<" The number is odd" <<"\n";
   }
   system("pause");
   return 0;
}



Now let us dissect the program and discuss each line briefly

#include <iostream>

This is built in library shiiped with most compilers of C++,  to input output data in our program we use it.

using namespace std;

Input output data in program is really written as “std::cout”. To avoid putting “std::” we use “using

namespace std;  statement.. where std is a namespace

int main(void)

This is the main function of program, without main function we cannot develop C++ code. Basically in function's body(which will be discussed in detail later on) we can use many statements and conditions to complete a program, main function is the most important function in a program and there is only one main function in C++ program,  main function is the starting point of C++program. When we use statements and conditions in “main” function we will write every thing inside the body of function and the body of main function starts from left brace “{” and ends with right brace “}”.

The body of “main” function starts from below left brace, “{”

{

The “main” function now starts.

    int a;

In a program u have to declare a variable before using it and also its data type, data type tells the computer that how much memory should be reserved for variable. Data type may consist of “int,
float,double,char” etc. Different data type consist of different sizes. The syntax of declaring a variable is:
first u have to declare data type and then its variable name. Here we are using “int” data type and which reserve 4 bytes of memory in computer and its variable is “a”. The value of “a” will define later when we will take it from user. After any statement or declaring a variable “;” semicololon is used. The semicolon tells the compiler that the statement has ended.

        cout << "Enter the number" <<"\n";

Now this statement shows the “cout” stream data. The “cout” means this statement sends data to
screen. Here we write in “cout” statement “Enter the number” it will show as it is written in double
quotations. Whenever we write anything inside double quotation in “cout” statement it will show on
screen as is it is. In the “cout” statement we see “<<” operators. These operators
are use for “cout” stream data.  After  writing  “cout” it's must to write “<<” operators.
Now we can see “\n” in cout data stream. This “\n” is called escape sequence. Basically C++
contains many escape sequences, but this escape sequence is used to pass the cursor to the next line
that’s why it is used here. At the end of the statement we use “;” semicolon. It tells to compiler that the statement is end.

        cin >> a ;

First we use “cout” stream data. It displays the text from compiler to screen(which is a way to interact with user). Now we are using “cin” stream data. It collects the data from user and saves it in the program. In cout we use “<<” operators, here we are using “>>” operators. Here we are using variable a with the data type of int to save user input, which was declare in the start of program. As it is int data type that’s why it contains size of 4 bytes and only will take 4 bytes of data from user.

    if (a % 2 ==0)

Now here we are using a condition. If the user gives input in odd numbers the program will tell the
number is odd but if the user enter even number then compiler will tell that the number is even, but
how is this possible, that’s why we use conditional statement called “if-else” statement. Now in if-else statement  if the first statement which is “if” is true then the program will end by reading “if” statement because it is relational statement. In “if” statement we used brackets both left and write and the condition which we are using is if the int variable which is given by the user is such number which is divided by 2 and the remainder is 0 then the output will be as below.

          { cout <<" The number is even" <<"\n";

Left brace is the start of “if” statement’s body. The body of statement will end just before right brace.

          }

Now if the relational statement is false then the conditional statement will execute which is “else” statement. Relational statement is wrong means that the remainder is not equal to 0 and the number is not even.

So if the number is not even then automatically it is odd.

          else 
         {
           cout <<" The number is odd" <<"\n";
          }
         
system ("pause");

system function takes a command as input parameter and sends it to console. "pause" puts console on hold until user presses a key. This way we can make teh out put of program to stay as long as user wants it to.

return 0;

at the end we return 0 to finish the program. You'll learn more about it when we'll discuss functions later.

}

No comments:

Post a Comment