473,785 Members | 2,299 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

For statement

Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexO f("\n",at+1)) n++;

All I'm trying to do is count the number of newlines in a string.
When I step through it in the debugger, it highlights the first statement
(int at=0),
then highlights the second statement, then jumps to the next line of code.
It doesn't appear to increment n at all, nor does it appear to ever evaluate
the 3rd statement of the for loop. The string I debugged with has 3 newlines
at various locations.

Thanks,
Gary
Nov 16 '05 #1
8 1266
> Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexO f("\n",at+1)) n++;

All I'm trying to do is count the number of newlines in a string.
When I step through it in the debugger, it highlights the first statement
(int at=0),
then highlights the second statement, then jumps to the next line of code.


The for statement has the following form:
for ([initializers]; [expression]; [iterators];) {
// statements
}

The initializers are a comma separated list of statements or expressions to
initialize counters.
The expression is an expression that can be evaluated as a boolean value. If
the expression evaluates to true the statements inside the for are executed
and then the iterators are evaluated.
The iterators are statements to increase or decrement the counters.

In your code the expression at==-1 always evaluates to false since at is
initialized to 0.

To count the number of occurances of a new line you can use this code:
int startPos=0;
int foundPos=0;
int count=0;
do {
foundPos=msg.In dexOf('\n',star tPos);
if (foundPos>-1) {
startPos=foundP os+1;
count++;
}
} while(foundPos > -1 && startPos < msg.Length);

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #2
>Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexO f("\n",at+1)) n++;

^^

I believe you want != there.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3
> >Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexO f("\n",at+1)) n++;

^^

I believe you want != there.

This won't work unless n is initialized to -1 because it will do a iteration
for at==-1 resulting in n being of by one. Of course, you could subtract 1
from n after the loop. Still, it would be faster than my example.

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
Nov 16 '05 #4

"Mattias Sjögren" <ma************ ********@mvps.o rg> a écrit dans le message
de news: eR************* *@TK2MSFTNGP15. phx.gbl...
Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexO f("\n",at+1)) n++; ^^

I believe you want != there.


Even with at != 1, it does not work. If first char is \n, the loop won't see
it because IndexOf is called with an index of 1 the first time.
And also, the count is incremented once more than necessary.

The following should work:

int n = 0;
for (int at = -1; (at = msg.IndexOf('\n ', at + 1)) != -1; )
n++;

but I would prefer:

int n = 0;
int at = -1;
while ((at = msg.IndexOf('\n ', at + 1)) != -1)
n++;

Unfortunately, the while syntax does not let me scope the at variable, but
this looks cleaner.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #5
=?Utf-8?B?R2FyeUZl?= <Ga****@discuss ions.microsoft. com> wrote in
news:03******** *************** ***********@mic rosoft.com:
Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexO f("\n",at+1)) n++;

All I'm trying to do is count the number of newlines in a
string. When I step through it in the debugger, it highlights
the first statement (int at=0),
then highlights the second statement, then jumps to the next
line of code. It doesn't appear to increment n at all, nor does
it appear to ever evaluate the 3rd statement of the for loop.
The string I debugged with has 3 newlines at various locations.


Gary,

In addition to the other posts, here is a non-deterministic way to
count the number of newlines in a string:

using System.Text.Reg ularExpressions ;
...
string input = "one\ntwo\nthre e\n";
string regex = "\n";
int numberOfMatches = Regex.Matches(i nput, regex, RegexOptions.Si ngleline).Count ;

--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #6
"Chris R. Timmons" <crtimmons@X_NO SPAM_Xcrtimmons inc.com> wrote in
news:Xn******** *************** ***********@207 .46.248.16:
In addition to the other posts, here is a non-deterministic way
to count the number of newlines in a string:


Duh! I really should proofread before I post. I meant non-
procedural, not non-deterministic.
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 16 '05 #7
Whatever! The reply was useful, and I thank you.

Gary

"Chris R. Timmons" wrote:
"Chris R. Timmons" <crtimmons@X_NO SPAM_Xcrtimmons inc.com> wrote in
news:Xn******** *************** ***********@207 .46.248.16:
In addition to the other posts, here is a non-deterministic way
to count the number of newlines in a string:


Duh! I really should proofread before I post. I meant non-
procedural, not non-deterministic.
Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/

Nov 16 '05 #8
Yoiks! My misunderstandin g! I believe your correction will fix me right up.

Gary

"Mattias Sjögren" wrote:
Can anyone tell me why this statement doesn't work?
for (int at=0;at==-1;at=msg.IndexO f("\n",at+1)) n++;

^^

I believe you want != there.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #9

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

Similar topics

28
3597
by: Fábio Mendes | last post by:
I'm sorry if it's an replicate. Either my e-mail program is messing with things or the python-list sent my msg to /dev/null. I couldn't find anything related in previous PEP's, so here it goes a very early draft for a new "assert" syntax: This was inspired in Ruby's assert syntax. I'm not familiar with Ruby at all, so the chances are that this piece of code is broken, but I think the idea is very obvious. In Ruby, assert is simply a...
15
2808
by: Nerox | last post by:
Hi, If i write: #include <stdio.h> int foo(int); int main(void){ int a = 3; foo(a); }
13
2575
by: eman1000 | last post by:
I was recently looking at the prototype library (http://prototype.conio.net/) and I noticed the author used the following syntax: Object.extend(MyObj.prototype, { my_meth1: function(){}, my_meth2: function(){} }); to define new methods on the MyObj prototype object. Object.extend
37
3312
by: Steven Bethard | last post by:
The PEP below should be mostly self explanatory. I'll try to keep the most updated versions available at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt http://ucsu.colorado.edu/~bethard/py/pep_create_statement.html PEP: XXX Title: The create statement
18
2723
by: Steven Bethard | last post by:
I've updated the PEP based on a number of comments on comp.lang.python. The most updated versions are still at: http://ucsu.colorado.edu/~bethard/py/pep_create_statement.txt http://ucsu.colorado.edu/~bethard/py/pep_create_statement.html In this post, I'm especially soliciting review of Carl Banks's point (now discussed under Open Issues) which asks if it would be better to have the create statement translated into:
28
2952
by: Steven Bethard | last post by:
Ok, I finally have a PEP number. Here's the most updated version of the "make" statement PEP. I'll be posting it shortly to python-dev. Thanks again for the previous discussion and suggestions! PEP: 359 Title: The "make" Statement Version: $Revision: 45366 $ Last-Modified: $Date: 2006-04-13 07:36:24 -0600 (Thu, 13 Apr 2006) $
7
2702
by: Steven Bethard | last post by:
I've updated PEP 359 with a bunch of the recent suggestions. The patch is available at: http://bugs.python.org/1472459 and I've pasted the full text below. I've tried to be more explicit about the goals -- the make statement is mostly syntactic sugar for:: class <name> <tuple>: __metaclass__ = <callable>
19
8384
by: Steve | last post by:
ASP error number 13 - Type mismatch with SELECT...FOR UPDATE statement I got ASP error number 13 when I use the SELECT...FOR UPDATE statement as below. However, if I use SELECT statement without FOR UPDATE, it is fine and no error. I also tried Set objRs = objConn.Execute("SELECT * FROM EMP UPDATE OF EMPNO"), but it still couldn't help. any ideas? I tried to search in the web but couldn't find similar
18
7977
by: dspfun | last post by:
Hi! The words "expression" and "statement" are often used in C99 and C- textbooks, however, I am not sure of the clear defintion of these words with respect to C. Can somebody provide a sharp defintion of "expression" and "statement"? What is the difference between an expression and a statement?
23
2076
by: florian.loitsch | last post by:
According to the spec Section 14 the production SourceElements:SourceElements SourceElement is evaluated as follows: 1. Evaluate SourceElements. 2. If Result(1) is an abrupt completion, return Result(1) 3. Evaluate SourceElement. 4. Return Result(3). If I understood correctly the following program should alert 'undefined': alert(eval('3;;'));
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
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
10162
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...
1
10100
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8988
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...
0
5396
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
5528
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2893
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.