understanding for ( ) statement

a.) this code was a free source from online [ not mine / cannot take credit ]
b.) you enter in how many numbers you want to add up [ example 5 ]
c.) then you enter in your 5 numbers and the prog. adds them up for you
d.) - - here is where my question comes in - - inside of the for ( )
statement / integer i = 0 [ understood ] / then i<numberitems .......
* is this read as i is less than numberitems ? i'm assuming yes / 0<5
* then, what does that nasty i++ do ??

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace std;

int main ()

{

    int sum = 0;
    int number;
    int numberitems;

    cout << "Enter number of items: \n";
    cin >> numberitems;

    for(int i=0;i<numberitems;i++)
    {
        cout << "Enter number: \n";
        cin >> number;

        sum=sum+number;

    }

    cout << "\n" << "\n";
    cout<<"the sum is: "<< sum<<endl;

}
for(int i=0;i<numberitems;i++)

Is actually 3 statements

create interger i and set it's value to 0;
int i=0;

while i is less than numberitems.
i<numberitems;

Add 1 to the value in i, which happens to occur after each loop.
i++


For a example you can run this code.
for(int i=0;i<10;i++)
{cout << i;}

Last edited on
Many thanks SamuelAdams.
I am very grateful for your help / explaination to my questions.
Many thanks !!
XMIT10
Topic archived. No new replies allowed.