...Continued with for loop.

Another kind of loop in C++ is the for loop. The for loop is primarily used to repeat a statement or a block for a prescribed number of iterations means when we know how many times we want to execute the statements we can use the for loop in that situation.

Syntax:

for(initialization; termination; incrementation)
{
loop_statements
}

The first statement in the for loop is called intialization, which sets the loop_controller variable to its initial value. It executes only once in the beginning of the for loop.

The second statement in the for loop is called termination, which checks the loop_controller variable's value and decides whether the loop will execute further or not. This statements always executes to check the condition of the for loop.

The third statement in the for loop is called incrementation which, either increments or decrements, the loop_controller variable's value. (depending on the situation). This statement executes if the condition of the loop_controller variable is true.

so the above program to print the values from 1 to 10 can be re-written as follows:

for(int x=1;x<11;x++)
{
cout<}

Here's the explanation, how this will work.

Initially a variable X will have it's initial value 1.
Then it'll check whether X is less than 11 or not. Coz the condition is true(X is less than 11), It'll enter the for loop body and print the value of X i.e. 1 this time.
Then value of x will be incremented by 1 and X will have value 2.
Now, as i told first expression (i.e. initialization will not execute) coz it executes only once in the beginning of the loop.
Then again condition will be checked and will result to true. cos X is having a value less than 11 (i.e. 2 this time).
Then it's value will be printed.
Then X will be incremented by 1.
And again it's value will be checked and be printed till X becomes 10.
Then as X will be incremented by 1. X will have a value 11.
Now when the condition is checked it'll results to false and the loop terminates.

Continued...... with Variables.

Variables:
-------------

A basic objective in all computer programs is to manipulate some data and get some output. An element in this process is having a piece of memory that you can refer using a meaningful name, and where you can store an item of data. Each individual piece of memory so is called a variable. you already know, each variable will store a particular kind of data, and the type of data that can be is fixed when you define the variable in your program.

So, Now you would come to know what a variable is???
In simple words, we can say a variable is a named location in memory where we can store some data and retrieve that data by the name of the location (i.e. variable name).

In your applications, there would be a situation where you want to store the age of a person or you would want to store the marital status of a person.

So what value a variable hold will depends on situations and what data type we associate with that variable.

You can declare many variables in a program having different datatypes. So there are many kinds of datatypes in C++ and which are used frequently are as follows:

int
char
float
bool
long
User Defined Data types like Enum and Struct
and many more in the row which we will learn later.

So, you would be familiar with int data type till now which is used to store integer values.

The syntax to declare a variable is simple.

datatype variablename;

or you can store the value in the variable at the time of declaration as:

datatype variablename=value;

like,

int i=10;

above statement will declare a variable with integer datatype and having a value 10.

Char:
-------
Char datatype is used to store character values. Like
char[20]="We are using Orkut";

And float datatype is used to store floating point (fractional/decimal point value) value.
Remaining datatypes we'll be learning further as they will be required.

Bt now allow me to formally introduce you to Functions.

Functions/Methods:
------------------------
Functions are the set of statements which are having a function name and which we can access by referring to its name.

Or you can say, Methods are a set of actions taken by a receiver in response to a message(i.e. variable).

Functions plays an important role in object oriented programming. By these we can group some statements under one name and access them from anywhere the application.

Syntax:
---------

the syntax to create a method is as follows:

accessspecifier returntype methodname(parameters)
{

//body of the method.

}



So, in the syntax there are a few things to understand the structure of a function.

Access specifier: This specifies whether we are allowed to access/call the function from any other place(i.e class/namespace) or not. Means whether it is accessible to other program entities or not.

Return type: Specifies whether the function will return any value or not to the place from where we've called/access it. We might or might not want to return any value depending on the situations.

Method name: There should be the valid name for Method, so that we can refer to it. It should be unique and should not be same as any keyword like variable datatypes or class.

Method Body: In method body we place statements that we want to group together under a single name.

Functions:

So let's create a function to print the addition of two values. Later we'll be creating more complex functions.

public void add()
{
int a,b,sum;
cout<<"Enter the value of a:"a;
cout<<"Enter the value of b:"b;

sum=a+b;

cout<<"The total is: "<}


In the above statements we've created a simple function that have public accessibility. Means we can access it from anywhere in our program.
Other access specifier are protected, private and friendly which we'll learn later.

The above function will not return any value to its caller. For function that do not return any values we uses void as its return type.
If we want to return any value than we can simply give the data type of the value that we want to return like int, float, char or bool.

The above method will have the name "add" which should be uniqe.

And in the body, user will be prompted to enter two values and sum will be displayed to him.
A function is a group of statements that is executed when it is called from some point of the program. The following is its format:

type name ( parameter1, parameter2, ...) { statements }

where:

* type is the data type specifier of the data returned by the function.
* name is the identifier by which it will be possible to call the function.
* parameters (as many as needed): Each parameter consists of a data type specifier followed by an identifier, like any regular variable declaration (for example: int x) and which acts within the function as a regular local variable. They allow to pass arguments to the function when it is called. The different parameters are separated by commas.

* statements is the function's body. It is a block of statements surrounded by braces { }.

Here you have the first function example:

// function example
#include
using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return (r);
}

int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
return 0;
}

The result is 8

In order to examine this code, first of all remember something said at the beginning of this tutorial: a C++ program always begins its execution by the main function. So we will begin there.

We can see how the main function begins by declaring the variable z of type int. Right after that, we see a call to a function called addition. Paying attention we will be able to see the similarity between the structure of the call to the function and the declaration of the function itself some code lines above:

The parameters and arguments have a clear correspondence. Within the main function we called to addition passing two values: 5 and 3, that correspond to the int a and int b parameters declared for function addition.
At the point at which the function is called from within main, the control is lost by main and passed to function addition. The value of both arguments passed in the call (5 and 3) are copied to the local variables int a and int b within the function.

Function addition declares another local variable (int r), and by means of the expression r=a+b, it assigns to r the result of a plus b. Because the actual parameters passed for a and b are 5 and 3 respectively, the result is 8.

The following line of code:

return (r);

finalizes function addition, and returns the control back to the function that called it in the first place (in this case, main). At this moment the program follows it regular course from the same point at which it was interrupted by the call to addition. But additionally, because the return statement in function addition specified a value: the content of variable r (return (r);), which at that moment had a value of 8. This value becomes the value of evaluating the function call.
Arguments passed by value and by reference.


Until now, in all the functions we have seen, the arguments passed to the functions have been passed by value. This means that when calling a function with parameters, what we have passed to the function were copies of their values but never the variables themselves. For example, suppose that we called our first function addition using the following code:

int x=5, y=3, z;
z = addition ( x , y );

What we did in this case was to call to function addition passing the values of x and y, i.e. 5 and 3 respectively, but not the variables x and y themselves.


This way, when the function addition is called, the value of its local variables a and b become 5 and 3 respectively, but any modification to either a or b within the function addition will not have any effect in the values of x and y outside it, because variables x and y were not themselves passed to the function, but only copies of their values at the moment the function was called.

But there might be some cases where you need to manipulate from inside a function the value of an external variable. For that purpose we can use arguments passed by reference, as in the function duplicate of the following example:

// passing parameters by reference
#include
using namespace std;

void duplicate (int& a, int& b, int& c)
{
a*=2;
b*=2;
c*=2;
}

int main ()
{
int x=1, y=3, z=7;
duplicate (x, y, z);
cout << "x=" << x << ", y=" << y << ", z=" << z;
return 0;
}

x=2, y=6, z=14

The first thing that should call your attention is that in the declaration of duplicate the type of each parameter was followed by an ampersand sign (&). This ampersand is what specifies that their corresponding arguments are to be passed by reference instead of by value.
When a variable is passed by reference we are not passing a copy of its value, but we are somehow passing the variable itself to the function and any modification that we do to the local variables will have an effect in their counterpart variables passed as arguments in the call to the function.



To explain it in another way, we associate a, b and c with the arguments passed on the function call (x, y and z) and any change that we do on a within the function will affect the value of x outside it. Any change that we do on b will affect y, and the same with c and z.

That is why our program's output, that shows the values stored in x, y and z after the call to duplicate, shows the values of all the three variables of main doubled.

If when declaring the following function:

void duplicate (int& a, int& b, int& c)

we had declared it this way:

void duplicate (int a, int b, int c)

i.e., without the ampersand signs (&), we would have not passed the variables by reference, but a copy of their values instead, and therefore, the output on screen of our program would have been the values of x, y and z without having been modified.

Passing by reference is also an effective way to allow a function to return more than one value. For example, here is a function that returns the previous and next numbers of the first parameter passed.

// more than one returning value
#include
using namespace std;

void prevnext (int x, int& prev, int& next)
{
prev = x-1;
next = x+1;
}

int main ()
{
int x=100, y, z;
prevnext (x, y, z);
cout << "Previous=" << y << ", Next=" << z;
return 0;
}

previous=99 and next=101

Like it on Facebook, Tweet it or share this article on other bookmarking websites.

No comments