Wednesday, 1 April 2015

An Introduction to c++; Basics

Introduction to C++:



Hey Guys, today i'm going to tell you some of the basics of C++ in very easy language for the beginners. 
"C++ is a computer language which is mostly used in programming. It is pronounced as See-plus-plus."

 

Basics for C++, from ground level:

I've seen on internet, its pretty hard to understand C++ as i've took help from web but couldn't get much as it was a little difficult for me when i was a beginner.
Now we are using "Dev-cpp" or Or Dev C++ program to make a program. We call this program maker software as "Integrated Development Environment" or simply IDE.

Now as we have many IDEs like Turbo c++, dev c++ (also called bloodshedcpp) and Visual Studio.

Starting from basics, first we need to understand some terms.

cout = console out
cin = console in
iostream = input output stream
conio.h = header files (that makes a program executable)
Semi colon (;) = to terminate a line
namespace std = To ignore the spaces which are extra
// = comments, which are ignored by program, for ease of user
endl = end line

so here is our first program.

#include <iostream>
using namespace std;
#include <conio.h>
int main ();
{
int a;
cout<<"Enter the value of a"<<endl;
cin>>a;
cout<<a<<endl;
getch();
return 0;
}

Now if you want to perforn addition then (I'm starting with codes directly) ;

{
int a,b,c;
cout<<"Give value of a"<<endl;
cin>>a;
cout<<"give value of b"<<endl;
cin>>b;
c= a+b;
cout<<"Value of c"<<c<<endl;
getch();
return 0;
}

Now similarly you can perform Subtraction, multiplication and division.


If and Else statements:

            Now we are going to discuss some if and else statements. These are very easy to understand. These are also called "Conditional statements". Now we will see some of its examples:

"Show if the number is positive, negative or zero"

{
int a;
cin>>a;
if (a>0)
cout<<" It is postive"<<endl;
else if (a<0)
cout<<"A is negative"<<endl;
else
cout<<"A is zero"<<endl;
getch ();
return 0;
}
Note that we don't put any condition in else because its only remaining possible condition.

Further we can extend it with the use of "AND" and "OR" conditions.
AND (&&) = if every given condition is true then whole statement is true. e.g 1+1 = 1, 1+0 = 0 (Boolean algebra) 
OR (||) = if any one condition is true then statement is true. e.g 1+0 = 1, 0+0 = 0

Integrating in Program we have;
"Assign three grades to student"

{
int a;
if (a>= 80) && (a<=100)
cout<<"Assigned A* grade"<<endl;
else if (a>=50) && (a<=79)
cout<<"Assigned B grade"<<endl;
else if (a<50) && (a>0)
cout<<"Assigned C grade"<<endl;
else
cout<<"Invalid input"<<endl;
getch();
return 0;
}

 Similarly you can integrate OR operation.

And further are switch statements. These are also conditional statements. give the value of an option. It it matches with any case then it will trigger that one. For example:


switch (i)
{
    case constant-expression //eg. 1  :
       statement;
       break; //it is optional, if you wont put it the term will go infinite.
    case constant-expression  :
       statement;
       break; //optional
  
    // you can have any number of case statements.
    default : //Optional
       statement(s);
}

A flowchart of switch statement
 Example of switch statement:

#include <iostream>
using namespace std;
int main ()
{
   // local variable declaration:
   char grade = 'D';

   switch(grade)
   {
   case 'A' :
      cout << "Excellent!" << endl; 
      break;
   case 'B' :
   case 'C' :
      cout << "Well done" << endl;
      break;
   case 'D' :
      cout << "You passed" << endl;
      break;
   case 'F' :
      cout << "Better try again" << endl;
      break;
   default :
      cout << "Invalid grade" << endl;
   }
   cout << "Your grade is " << grade << endl;
   return 0;
}

It will produce result that "You passed, Your grade is D"

This is enough for the basics. 

Thankyou for paying a visit.
Regards,
Musub Azfar Khan

No comments:

Post a Comment