473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

if loop

for (int i = 1; i <= 10; i++)
cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?
Jul 23 '05 #1
11 2006
Roman Töngi wrote:
for (int i = 1; i <= 10; i++)

The above can also be written:

for(int i=1; i<11; i++)

cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?

In both cases the loop body is executed for i values in [1, 10].
--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #2
Roman Töngi schrieb:
for (int i = 1; i <= 10; i++)
cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?

No.

Maybe it get's clearer if you re-write the for-loop as a while-loop
(which are semantically equivalent, except for the scope of the loop
variable i):

int i=1;
while(i<=10)
{
cout << i << endl;
i++;
}
HTH
Stefan
Jul 23 '05 #3
Roman Töngi schrieb:
for (int i = 1; i <= 10; i++)
cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?

No.

Maybe it get's clearer if you re-write the for-loop as a while-loop
(which are semantically equivalent, except for the scope of the loop
variable i):

int i=1;
while(i<=10)
{
cout << i << endl;
i++;
}
HTH
Stefan
Jul 23 '05 #4
"Roman Töngi" <ro**********@bluewin.ch> wrote in message
news:42**********@news.bluewin.ch
When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?


No, but the return value from incrementing is not of interest. It
used nowhere in the code. First, you increment, then you use i to check
the condition. Note: you do not use the return value, but the new value
i has.

hth
--
jb

(reply address in rot13, unscramble first)
Jul 23 '05 #5
> for (int i = 1; i <= 10; i++)
cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?


The two expressions i<=19 and i++ are evaluated independently, so when i is
incremented from 10 to 11, then 11 is stored and used for the comparison.

Niels Dybdahl
Jul 23 '05 #6
I understand. Condition and Expression within the if head
are separate.

Thanks
Jul 23 '05 #7
On Wed, 20 Apr 2005 08:33:01 +0200, Roman Töngi <ro**********@bluewin.ch>
wrote:
for (int i = 1; i <= 10; i++)
cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?


i would say that the postfix increments i after the whole loop is done. so
when the loop has printed 10, i is incremented to 11, _then_ the loop
condition is evaluated and the loop isn't entered any more. so, no
printing of 11.

:)
Jul 23 '05 #8

"ulrich" <ua********@aon.at> wrote in message
news:opspisc6sin2mgp5@innsbruck-neu...
On Wed, 20 Apr 2005 08:33:01 +0200, Roman Tvngi <ro**********@bluewin.ch>
wrote:
for (int i = 1; i <= 10; i++)
cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for
construct?


i would say that the postfix increments i after the whole loop is done. so
when the loop has printed 10, i is incremented to 11, _then_ the loop
condition is evaluated and the loop isn't entered any more. so, no
printing of 11.

:)


Just to be clear... the same would happen with a prefix operator instead of
a postfix operator. The expression portion of the for statement is *always*
executed after the loop is executed.

-Howard


Jul 23 '05 #9

"Roman Tvngi" <ro**********@bluewin.ch> wrote in message
news:42**********@news.bluewin.ch...
I understand. Condition and Expression within the if head
are separate.


That's a "for" loop, not an "if" statement. Forget your coffee this
morning? :-)

-Howard

Jul 23 '05 #10

Howard wrote:
"ulrich" <ua********@aon.at> wrote in message
news:opspisc6sin2mgp5@innsbruck-neu...
On Wed, 20 Apr 2005 08:33:01 +0200, Roman Tvngi <ro**********@bluewin.ch> wrote:
for (int i = 1; i <= 10; i++)
cout << i << endl;

I expected the following:
1
2
3
4
5
6
7
8
9
10
11

When it comes to the last iteration i is 10.
Then i++ increments, but returns the old value 10.
So, condition is again true.
Now, i in the body of if is 11.

Apparently, I am wrong.
Does the postfix increment operator function differently within a for construct?
i would say that the postfix increments i after the whole loop is done. so when the loop has printed 10, i is incremented to 11, _then_ the loop condition is evaluated and the loop isn't entered any more. so, no
printing of 11.

:)


Just to be clear... the same would happen with a prefix operator

instead of a postfix operator. The expression portion of the for statement is *always* executed after the loop is executed.

-Howard


Hi,

I'll show you the execution order of a 'for loop' then you'll
understand

for ( int i = 0; i <= 10; i++)
cout << i;

Order Of Execution;

1) i = 0 ----> i initialized

2) i <= 10 condition checked ( if evaluates to true you get in to the
loop else
you exit the loop. your i will remain as 0 if you exit here

3) if in step 2 you did not exist i++ would be evaluated

4) now onwards step 2 and 3 are followed till you exit the loop.
just after you have 10 output i would be incremented to 11 and then the
condtion checked and evaluates to false and you exit the loop without
printing
10. but your i would contain the value 11

rgds

ruksan

Jul 23 '05 #11

"ruksan" <ru****@gmail.com> wrote in message
news:11**********************@l41g2000cwc.googlegr oups.com...

Hi,

I'll show you the execution order of a 'for loop' then you'll
understand

for ( int i = 0; i <= 10; i++)
cout << i;

Order Of Execution;

1) i = 0 ----> i initialized

2) i <= 10 condition checked ( if evaluates to true you get in to the
loop else
you exit the loop. your i will remain as 0 if you exit here

3) if in step 2 you did not exist i++ would be evaluated
That should read:

3) if in step 2 you did not exit the loop, then the loop body would be
executed. After that, the expression (i++, in this case) would be
evaluated.

4) now onwards step 2 and 3 are followed till you exit the loop.
just after you have 10 output i would be incremented to 11 and then the
condtion checked and evaluates to false and you exit the loop without
printing
10. but your i would contain the value 11


The variable i would indeed contain 11 when the check is made, but once the
loop is exited, i is no longer in scope and thus cannot be said to have any
value at all.

-Howard

Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Charles Alexander | last post by:
Hello I am new to php & MySQL - I am trying to retrieve some records from a MySQL table and redisplay them. The data in list form looks like this: Sample_ID Marker_ID Variation ...
3
by: Anand Pillai | last post by:
This is for folks who are familiar with asynchronous event handling in Python using the asyncore module. If you have ever used the asyncore module, you will realize that it's event loop does not...
43
by: Gremlin | last post by:
If you are not familiar with the halting problem, I will not go into it in detail but it states that it is impossible to write a program that can tell if a loop is infinite or not. This is a...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
2
by: Alex | last post by:
Compiler - Borland C++ 5.6.4 for Win32 Copyright (c) 1993, 2002 Borland Linker - Turbo Incremental Link 5.65 Copyright (c) 1997-2002 Borland Platform - Win32 (XP) Quite by accident I stumbled...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
32
by: cj | last post by:
When I'm inside a do while loop sometimes it's necessary to jump out of the loop using exit do. I'm also used to being able to jump back and begin the loop again. Not sure which language my...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
2
ADezii
by: ADezii | last post by:
If you are executing a code segment for a fixed number of iterations, always use a For...Next Loop instead of a Do...Loop, since it is significantly faster. Each pass through a Do...Loop that...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.