Thursday, June 23, 2011

Repetition Statement: The For Loop in c++


Repetition Statement: The For Loop in c++


To understand this you must first go through The While loop in c++.
The below program, Program 1 does the same thing that the Program 1 of previous chapter The While Loop does. An integer i is declared and then three expression of the loop: initializer, loop-test and counting expression, all are passed as parameters in for loop. The variable i is initialized to 0 then it will test the condition whether the value of i is less than 10. If the condition is true, the body of loop will execute. The incrementation statement will be last statement to be executed by the for loop.

/* A c++ program example that uses for loop to display number from 0 to 9 using incrementation in the loop */


// Program 1

#include <iostream>

using namespace std;

int main ()
{
    int i;

    for (i=0; i<10; i++)
    {
        cout << i << endl;
    }

    return 0;
}

If you are just going to use the variable within the body of loop, then better declare it in the for loop itself. This will make your program more reliable. The below programs declare variable in the for loop.

/* A c++ program example that uses for loop to display multiplication of 8*/


// Program 2

#include <iostream>

using namespace std;

int main ()
{
    for (int i=8; i<=80; i+=8)
        cout << i << endl;

    return 0;
}


/* A c++ program example that tells you whether the number is even or odd. Number ranges from 1 to 20. */


// Program 3

#include <iostream>

using namespace std;

int main ()
{
    for (int i=1; i<=20; i++)
    {
        if (i%2==0)
            cout << i << "is an even number." << endl;

        else
            cout << i << "is an odd number." << endl;
    }

    return 0;
}

The operator "%" is called the remainder/modulo operator as already been described in Mathematical Operators. The statement i%2==0 simply means that the remainder of i divided by 2 equals to 0.

/* A c++ program example that uses for loop to diplay the square root and cube of numbers from 1 to 10. */


// Program 4

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    cout << "Number " << "Square " << "Cube" << endl;

    for (long i=1; i<=10; i++)
    {
        cout << setw (2) << i << setw (8)
        << i*i << setw (8) << i*i*i << endl;
    }

    return 0;
}


/*A c++ program example that displays multiplication table of a number provided by the user.*/


// Program 5

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{
    int number;

    cout << "Enter a number to get its multiplication table: ";
    cin >> number;
    cout << endl;
    cout << "-------------" << endl;

    for (int i=1; i<=10; i++)
    {
        cout << number << " x " << setw (2) << i << " = "
        << setw (3) << number*i << endl;
        cout << "-------------" << endl;
    }

    return 0;
}

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Want to learn more? View List Of All Chapters

12 comments:

  1. ur site is some contentive. but folloe proper coding standard. it is advisable to remove the ads.

    ReplyDelete
  2. I like C more than C++ because my job require programming with embeded systems. But I will learn C++ for my passion. Waiting your next posts.

    ReplyDelete
  3. Glad to have found your blog here. I am currently in C++ class, and may have to retake this one before moving forward in C++ because I fell way behind in my class. Thanks for all your posts here!

    ReplyDelete
    Replies
    1. You are most welcome! :D I am glad that it has been helpful to you. :)

      Delete
  4. And i think that there should more clear and descriptive way of posting ......
    Like you haven't written the meaning of i++ it means that i = i + 1;....

    ReplyDelete
    Replies
    1. Thanks Ashish for your comment! :)
      I have already explained that in previous chapter with programming example.
      Have a look: Increment Decrement Operator

      Delete
  5. It is very informative and has a very good quality in it.

    ReplyDelete
  6. thank u so much and pls explain interview based questions

    ReplyDelete
  7. this is interested topic of our school!

    can you teach me how to create a calendar program using the C++ compiler?

    tnx for any responsE!

    just contact us via email: villudo_markanthony@yahoo.com or txt us 09469237093....

    ReplyDelete
  8. can I ask something about the iterative statements

    ReplyDelete
  9. //Please make corrections in this c++ program
    #include
    void main()
    {
    char name[10];

    cout<<"Enter your name";
    cin>>name;

    cout<<"your name is "<<name<<endl;

    if(name=="madhav")
    { cout<<"Your roll no. is 62";
    }

    else
    cout<<"Roll no. is 63";

    }

    Can you please tell me why if statement is not displaying though the condition is true? Instead it is displaying statement in else part.

    ReplyDelete
  10. Honestly speaking, I'm too weak in this field. With the help of your clear article, I was able to understand everything quite well! Wishing you all the best! Best wishes, classyresumewriter.com/resume_writing

    ReplyDelete