Geekpedia Programming Tutorials






Introducing the 'for' loop

A really small tutorial introducing the 'for' loop, good for C++ newbies and programming beginners.

On Saturday, March 13th 2004 at 05:59 PM
By Andrew Pociu (View Profile)
****-   (Rated 3.6 with 8 votes)
Contextual Ads
More C++ Resources
Advertisement

In this tutorial I expect you to have a little knowledge about programming, but to understand at least the C++ variables and other basic information.

Like in any other programming language, loops are very useful for repeating tasks. For is probably the best example of a loop.

In this example we will see how to output a string ten times. We make use of ‘cout’ only once.


// Demonstrating the for loop

#include <iostream>
using namespace std;

int main()
{
    int counter;    // This variable will be changed by the loop

    for (counter = 0; counter < 10; counter++)    // Initialize to 0, check the current value and increment it
    {
        cout << "Loop number " << counter << "\n";    // Output some text and the value the variable holds
    }
    return 0;
}



Let’s dissect the code a little:

int counter - create a variable that will hold a different value each loop.
for (counter = 0 - initialize the variable to the value 0.
counter < 10 - check if the variable holds a value smaller than 10. If it does, the loop continues.
counter++ - increment the variable by one. As you may know, it’s equal to counter = counter + 1, but shorter.

The variable continues to be incremented, until it reaches 10. When the variable holds 10, the ‘counter < 10’ expression returns false, and the loop stops.

Check the following example:


// More about the for loop

#include <iostream>
using namespace std;

int main()
{
    cout << "Please input the starting number: ";
    int start_num;
    cin >> start_num;
    cout << "Please input the ending number: ";
    int end_num;
    cin >> end_num;
    int counter;
    for (counter = start_num; counter <= end_num; counter++)
    {
        cout << counter << "\n";
    }
    return 0;
}



for (counter = start_num - initialize the variable to the value of start_num.
counter <= end_num - check if the variable holds a value smaller or equal than the end_num variable. If it does, the loop continues.
counter++ - increment the variable by one.

This time the loop checks if the variable is smaller or equal, not just smaller.
If start_num was 3 and end_num was 9, and we used a counter < end_num condition, the result would be this:


1
2
3
4
5
6
7
8


Because when counter hits the value 9, the condition returns false. 9 is not smaller than 9, it is equal.

This is the result because when the counter hits the value 9, the condition returns false. 9 is not smaller than 9, it is equal.
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this tutorial
Comment Current Comments
by Ehi on Thursday, June 17th 2004 at 07:03 PM

Hello, I like your article. However I modified it and I would like to see you to see what is wrong with this code#include <iostream>
#include <stdlib.h>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
int main()
{
char myname;
int numb;

cout<<"Please type your name:";
cin>>myname;
cout<<endl;
cout<<"How many times do you want your name to be displayed: ";
cin>>numb;
cout<<endl;
cout<<"You want "<<myname<<" to be displayed "<<numb<<" times.";
system("pause");
}
Thank you.

by Ehi on Thursday, June 17th 2004 at 07:05 PM

Better still can I send it to you by email, because some of the syntax did not appear.

by ruud breukink on Friday, November 5th 2004 at 05:32 PM

Can I make variable designations in a for loop ?

meaning :

for (int i=0; i<Num; i++) {
string name = "test"+ (string)i;
ClassSomething* name = nem ClassSometing();
}

cheers, ruud

by aidi on Thursday, December 23rd 2004 at 12:01 AM

it very good for elementry class. i will copy your tutorial to development knowledge me. thank for you. i hope you can send information about programming C++ me at next time.

aidi


politeknik state of lhokseumawe
indonesia

by babyblue on Tuesday, September 9th 2008 at 06:31 AM

can u make an old version of c ... the truth is i cant understand the c language coz im only a 1st year college.. ^^

my professor teach only the old c like...


#include <iostream.h>
#include <stdio.h>
#include <conio.h>

main()
{
clrscr();

int x;
cout <<"input numbers 1-10";

for (x=0 ; x<=10 ; x )
cout<<x<<" ";
getch();
}


.. "like this??" ^^,


hmmm... thx...

by tara on Wednesday, March 3rd 2010 at 02:11 AM

can i have a sample program on looping for payroll system


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Tutorials
There are no related tutorials.

Comment Related Source Code
There is no related source code.

Jobs C++ Job Search
My skills include:
Enter a City:

Select a State:


Advanced Search >>
Latest Tech Bargains

Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons