For loop prints 0, after i++ increment expression!

How come: it prints value 0 for i, if based on definition of for loop, i++ will increase i from 0 to 1, before loop beings???

I cite:
"Like the while-loop, this loop repeats statement while condition is true. But, in addition, the for loop provides specific locations to contain an initialization and an increase expression, executed before the loop begins the first time, and after each iteration, respectively."
https://www.cplusplus.com/doc/tutorial/control/

1
2
3
4
5
6
7
8
9
10
11
12
int main()
{
    int n = 5;
    int arr[5];
   for(int i=0;i<n;i++)
	{
		cout<<"Enter element ["<<i<<"] ";
		cin>>arr[i];
	}

    return 0;
}


Thanks for answer.
Last edited on
It’s badly worded. The two aspects could be better described separately:
a) initialization - executed at the start of the first loop/iteration
b) increase expression - executed at the end of each loop/iteration
Hello empleat,

I do not quite understand your question. The program as written is working correctly.

Given this revised code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
   int n = 5;
   int arr[5];
    
   for(int i = 0; i < n; i++)
	{
		std::cout << "Enter element [" << i << "] ";
		std::cin >> arr[i];
	}

    for(int idx{}; idx < 5; idx++)
    {
        std::cout << "\n Element [" << idx << "] " << arr[idx];
    }
    
    return 0;
}


I get this output:

Enter element [0] 1
Enter element [1] 2
Enter element [2] 3
Enter element [3] 4
Enter element [4] 5

 Element [0] 1
 Element [1] 2
 Element [2] 3
 Element [3] 4
 Element [4] 5 


If you what a different number in the []s you have to tell it so.

Did you want:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

int main()
{
   int n = 5;
   int arr[5];
    
   for(int i = 0; i < n; i++)
	{
		std::cout << "Enter element [" << i + 1<< "] ";
		std::cin >> arr[i];
	}

    for(int idx{}; idx < 5; idx++)
    {
        std::cout << "\n Element [" << idx + 1 << "] " << arr[idx];
    }
    
    return 0;
}


Andy
Hello empleat,

Looking back at the subject I am wondering if you understand how a for loop works?

Andy

I do not quite understand your question. The program as written is working correctly.

Question is: if i++ increments i from 0 to 1, before statements between {} are executed. How come: cout prints i as 0, instead of 1? That's what I don't understand. Since I literally read it in tutorials, that increments happen before execution of statements.
It even says: "before the loop begins first time"!
Last edited on
Hello empleat,

That is what I thought. Looking at a for loop:
1
2
3
for (int idx{}; idx < 5; idx++)
         ^         ^       ^
      Part 1     Part 2   Part 3


You start with part 1 and set up the loop iterator.

Next you check part 2 and if it is true then you enter the for loop whether it is a single line or several lines between {}s.

When you have processed everything between the {}s and reach the closing } the for loop ends and goes to part 3 then checks part 2. If part 2 is true you enter the for loop and process what is there. Repeating the steps part 3 then part 2 until part 2 becomes false and ends.

The "idx++" does not increment until what is between the {}s is finished.

Of the code I posted look at what I did in lines 10 and 16. This does not change the value of "idx" it just add 1 before it is printed.

Andy
In general, the sentence "a and b did c and d, respectively" means "a did c and b did d". It does not mean "a and b both did c and d". The hint is the word "respectively"
https://www.google.com/search?q=define%3Arespectively

[The] for loop provides specific locations to contain an initialization and an increase expression, executed before the loop begins the first time, and after each iteration, respectively.


So this means
"The initialization is executed before the loop begins the first time, and the increase expression is executed after each iteration."
Last edited on
Oh okay, I guess this site is outdated. I use learncpp.com, but I was doing exercises from this site, so i used materials from this site while doing them...
The for loop
The for loop is designed to iterate a number of times. Its syntax is:

for (initialization; condition; increase) statement;

Like the while-loop, this loop repeats statement while condition is true. But, in addition, the for loop provides specific locations to contain an initialization and an increase expression, executed before the loop begins the first time, and after each iteration, respectively.

Thanks for clearing that up :)
Last edited on
Hello empleat,

Glad that cleared it up.

https://www.learncpp.com/ is good. I quite often use it as a reference.

Also https://en.cppreference.com/w/ is a better reference than here although they are working on updates here.

Andy
What do you mean, "outdated"? The explanation you pasted there is correct and it matches exactly with what mbozzi pasted.
Topic archived. No new replies allowed.