Wednesday, 1 July 2015

C++ easy understanding

                         Easy C++ Program Basics 


  • A simple C++ program:

Let's use the code below to find what makes up a very simple C++ program - one that simply prints "Hello World!" and stops. Adjust your browser so you can see the text window below the code. Then point your mouse at different statements in the program.


#include <iostream>

using namespace std;

int main()
{
 cout << "Hello World!" << endl;
 return 0;
}


iostream is a preprocessive directive need probably for all programs it is also called header files it helps to include the contents of the file. int main() is used to start the main program body.The body of the Program shoulad start and end with the curly braces as shown.

{
 cout << "Hello World!" << endl;
 return 0;
}

  • The above is the body of program.
  • it has cout<< it is used to show output to screen
  • which will display Hello World! and note that it should be in inverted commas as shown.
  • return 0; is used to savely end the program.

Program to add to numbers:

#include <iostream>
 
using namespace std;
 
int main()
{
   int a, b, c;
 
   cout << "Enter two numbers to add\n";
   cin >> a >> b;
 
   c = a + b;
   cout <<"Sum of entered numbers = " << c << endl;
 
   return 0;
}

  • Look here we add two numbers.
  • In body in the first we declare three integers.
  • int is data type which is integers all three numbers would be integers.
  • c integer would act as a result of addition of a and b.
  • than we use cout which would display sum of entered numbers.
  • In cin>> we give input of any two numbers, as cin>> allows to take input from the user.
  • I think you understand well.

LOGICAL OPERATORS and RELATIONAL OPERATORS IN C++:

 Increment and decrement operator:

Loops in C++ Language

C++ programming language provides the following types of loop to handle looping requirements. Click the following links to check their detail.
Loop TypeDescription
Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.
Execute a sequence of statements multiple times and abbreviates the code that manages the loop variable.
Like a while statement, except that it tests the condition at the end of the loop body
You can use one or more loop inside any another while, for or do..while loop.

Loop Control Statements:

Loop control statements change execution from its normal sequence. When execution leaves a scope, all automatic objects that were created in that scope are destroyed.
C++ supports the following control statements. Click the following links to check their detail.
Control StatementDescription
Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch.
Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.
Transfers control to the labeled statement. Though it is not advised to use goto statement in your program.

The Infinite Loop:

A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, you can make an endless loop by leaving the conditional expression empty.
#include <iostream>
using namespace std;
 
int main ()
{

   for( ; ; )
   {
      printf("This loop will run forever.\n");
   }

   return 0;
}
When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C++ programmers more commonly use the for(;;) construct to signify an infinite loop.
Now we will Discuss each tutorial one by one

While Loop:

#include <iostream>
using namespace std;
 
int main ()
{
   // Local variable declaration:
   int a = 10;

   // while loop execution
   while( a < 20 )
   {
       cout << "value of a: " << a << endl;
       a++;
   }
 
   return 0;
}
When the above code is compiled and executed, it produces the following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15                  

For Loop:  

The syntax of a for loop in C++ is:
for ( init; condition; increment )
{
   statement(s);
}
Here is the flow of control in a for loop:
  • The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
  • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
  • After the body of the for loop executes, the flow of control jumps back up to theincrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition.
  • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
j#include <iostream>
using namespace std;
 
int main ()
{
   // Local variable declaration:
   int a = 10;

   // do loop execution
   do
   {
       cout << "value of a: " << a << endl;
       a = a + 1;
   }while( a < 20 );
 
   return 0;
}
When the above code is compiled and executed, it produces the following result:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Thats enough for the beginners of C++

No comments:

Post a Comment