473,836 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2035
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**********@b luewin.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**********@b luewin.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:opspisc6si n2mgp5@innsbruc k-neu...
On Wed, 20 Apr 2005 08:33:01 +0200, Roman Tvngi <ro**********@b luewin.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**********@b luewin.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

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

Similar topics

0
2946
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 G23_NA17192.fsa rs7374540 A/C I23_Control.fsa rs7374540 C/C
3
5268
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 have a programmable exit condition. It keeps looping till the channels in its socket map (a dictionary) are closed and don't have any pending reads/writes. If you are using Python threads in your application, by using either the threading or...
43
5612
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 fallacy built on the assumption of mythical infinite all powerfull machines. In reality we deal with finite machines that are capable of two states in a loop, they either terminate, or repeat themselves. In the mythical halting problem scenario...
5
7306
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; int digit3 = 0; for( ; digit1 < 5 ; digit1++ ) {
32
4665
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 where the loop will be traversed a fixed number of times. It is very flexible, and novice programmers should take care not to abuse the power it offers."
2
2689
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 across some wierd loop behavior. With the pasted code I receive the output that follows. I realize that the code is broken, because the inner loop fails to reset j for each iteration of the outer loop (the fix is commented out). I also know that...
3
3532
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, while the C# for loop ending condition is evaluated on every iteration." Is this accurate? I don't understand how you could get away without evaluating the ending condition at every iteration. Otherwise, how would you
32
2616
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 memories are of but I think I just said loop somewhere inside the loop and it immediately jumped back to the start of the loop and began again. I can't seem to do that in .net. I this functionality available?
16
3555
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 instructions which can sure be optimized away is the check for the break condition, at least within the time where it is known that the loop will not reach it. Any idea how to write such a loop? e.g.
2
19325
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 iterates a specified number of times, requires you to also implement or decrement some sort of Loop Counter, while a For...Next Loop does that work for you. Both Loops will provide the same results, but the For...Next Loop is substantially faster. One...
0
9670
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10548
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9374
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7792
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6979
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5649
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5826
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4016
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3115
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.