Intro to Coding Vol 3

Intro to Coding Vol 3

By Aditya Chopra

In the past few editions we went over the syntaxes of C++ along with its various features and tricks. In this edition we go over a few more loops and introduce a more advanced concept of functions in C++.

While loop

A while loop works in the following way:

cpp_while_loop.jpg.png

The condition is given in the first round brackets. As long as this condition is satisfied the loop will run. When this condition is no longer met, the loop stops running. The syntax of the loop is quite simple.

while(condition)

{

statements;

}

Since there is no increment of counters specified the loop must have a statement which ensures that a counter is increasing or decreasing otherwise the statement will run infinitely and you will get an error in your code. For example:

while (i<10)

{cout<<“i is still less than 10”<<endl;

i++;

}

The above code will run till i<10. As soon as due to the increment statement i’s value exceeds 9, the loop will no longer run. Thus the code will display the statement exactly 10 times. “endl” is a keyword used to end the current line and display whatever comes next in the next line.

Do while Loop

A do while loop is quite similar to a while loop. The only difference being that first the statement is carried out and then the condition is checked. It’s logic works as shown below:

cpp_do_while_loop.jpg

A do while loop has the following syntax:

do {

statements;

} while(condition);

Again an increment or decrement counter needs to be specified.

do {

cout<<“i still less than 10”ends;

i++;

} while(i<10);

This loop while display the statement 11 times, since when the tenth time the loop is running the condition is no longer true, but the loop will be executed before the condition is checked.

Nested Loops and Two Dimensional arrays

In the previous volume we had discussed a matrix being inputted as a two dimensional array. This is done be using a nested for loop. A nested for loop is simply one for loop running inside another, with two different counters such that one counter can be imagined to move horizontally and another vertically. A matrix can be seen below:

two_dimensional_arrays.jpg

int a[2][3];  // creates a 3×4 matrix

int x= a[2][3]; // x gets value from the 4th element of the 3rd row. the first bracket denotes the number of rows and the second bracket shows the number of columns.

But how do we enter all the values into a really large array? We use for loops inside for loops i.e nested for loops. The syntax is shown below:

int ar[5][2]; // creates an array of 5 rows and 2 columns.

int i, j; // the two counters we need for the nested for loops.

for(i=0; i<=5;i++)

{for(j=0;j<=2;j++)

{

cin>>ar[i][j]; // takes in the element for the ith and jth position

}

}

or(i=0; i<=5;i++)

{for(j=0;j<=2;j++)

{

cout<<ar[i][j]endl; // displays the element in the ith and jth position

}

}

In the above shown code we see how data can be entered quickly in a large array. Essentially i traverses the number of rows and j traverses the number of columns. Thus for i=0 the inner loops runs from j=0 all the way till j=2. Then when all of those values of entered the outer loop is incremented from 0 to 1. This goes on till from (0,0) we reach element (5,2)  and all values are entered. Similarly we can display the values of an array too.

Functions in C++

In C++ till now we have been running our codes in the main() function which is a mandatory element of each C++ code. However we can have numerous user defined function which can be used if a particular segment needs to be repeated numerous times.

A C++ function must have:

a) A return type

b) function name

c) Parameters

d) statements

An arbitrary function can be defined as :

return_type funcname( parameters){

statements;

}

A user defined function can be accessed through the main function.

an example is:

int funcmax(int x, int y) // This is the Function declaration and syntax of a user defined function

{

int max;

if (x>y)

max=x;

else max=y;

return max; // Necessary return statement

} // This codes checks for the max value of the two parameters x and y

int main() //Main function to access user defined function

{

int i=10, j=4, val;

val=funcmax(i,j); //Over here the funcmax function is called to compare the values of i and j

cout<<val;

int val2=funcmax(35,12); //Over here funcmax is called to compare two integers

cout<<val2;

return 0;

}

A function can be mapped to do numerous tasks. Why not go to cpp.sh and try out a few functions yourself.  Coding is a very hands on activity, without practice you can’t retain any syntax or logic.

For any queries or help contact: adityachopra@vasantvalley.edu.in