Wednesday, October 14, 2015

Well explained Program to display a table C++

In this program we'll be looking at loops (for loop) and discuss a sample program in detail to understand how it works. The code of the program which we'll look at today is given below.

#include <iostream>
using namespace std;
int main (void)
{
    
    
    int a,b ;
    cin >> b;
    
    for ( a =1;  a <=10; a++ )    
    {
               cout << b <<"x" <<  a << "=" << (b * a) << "\n" ;
                     
                     }
        
        
    system("pause");
    return 0;
}
#include <iostream>
using namespace std;
int main (void)
{
    int a,b ;
    cin >> b;

The above code is described in detail in our previous program.
In this program we are going to execute one statement more then one time as our output. We are going to develop arithmetic table through a simple one line code. In table we know we print from 1 to 10. To save ourself the trouble of writing it 10 times use loop. Now what is loop? Let us discuss about loop. There are three kinds of loops, for loop, while loop, do while loop. We are using here for loop as it is the basic concept in loops but we will discuss the other two loops in our coming programs. 

Basic syntax of a for loop is:
for(initialization; condition; increment/decrement)

In the initialization part we initialize the value of variable from which we wants to start the loop. The second expression is the condition, we give the loop a specific condition on which loop execute. The loop will execute and repeat the output till the given condition become false. The third expression is increment ore decrement operator. Increment operator increases the value of variable by a certain value, while decrement operator decreases the value by as much as we want. 

Suppose “a” is a integer and its value is 2, when we write increment operator with “a” like a++ then the value of “a” will increase by 1, now the value of “a” is 3 instead of 2. Increment will add 1 in given variable’s value. So what if we 
want to decrease a value of variable?? Then will use decrement operator, it works just like increment but the difference is decrement decrease the value.  Similarly in loop, increment will increase the value of variable till the condition becomes false. Every expression inside the loop will follow by “;”semicolon, but there will be no semicolon after the third expression and all three expressions should be written inside the brackets as written below in “for loop”. Variables of int data type a and b. The value of “a” will be assigned in loop because it will execute ten times and the value of “b” will b taken by user.

    for ( a =1;  a <=10; a++ ) 

Now we will discuss about this loop, now in this for loop the value of “a” is equal to 1 and the condition given to “a” is smaller than and equal to 10. This condition means that when the value of “a” will become bigger then 10 the condition will become false and the loop will end, hence till the value of “a” 
become 10, it will repeats 10 times before the condition becomes false, that’s why we use increment operator at the end which will add 1 in the current value of “a”. One main thing in loop to remember is when the first 2 expressions are true it executes the output value after then its value is incremented. The value of “a” is 1 it is now compared with condition, the condition becomes true then the value of “a” is displayed as 1, after that the value is incremented and then the value will become 2 and so on. At the end when the value will become 10 It will be compared with condition, the condition is right then the value will 
be shown on screen, now the value will increase and new value becomes 11, it will be evaluated in condition 
since condition is now false for a equal to 10the program will move out of loop. 

    {
        cout << b <<"x" <<  a << "=" << (b * a) << "\n" ;

This is loop’s body and loops cout stream data statement. In this statement b will show the value which was given by the user, “x” is written as it is for multiplication operator, the value of a will executed by the loop, “=” is used for is equal sign as it is written in double quotation, (b*a ) will multiply the values of a and b. This cout statement will execute ten times because of “for loop”.

    }                
    system("pause");
    return 0;
}

No comments:

Post a Comment