Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   What the parentheses do (http://www.programmingforums.org/showthread.php?t=13277)

357mag Jun 4th, 2007 12:19 AM

What the parentheses do
 
I'm working on this little program that returns the index position of the word "is" contained in a string. Here is the code:

[code]
int main()
{
string text = "My name is Randy.";
string word = "is";
int count = 0;

cout << "The string is " << endl << text;

for (int index = 0; (index = text.find(word, index)) != string::npos ;
index += word.length(), count++)

cout << "Index position of the word is " << index << endl;

return 0;
}

The program works and it tells me the index position of the word "is" is 8.
What I don't understand is why it is necessary to put parentheses around the test condition:

:

(index = text.find(word, index))

I took them out and then ran my program and I got an infinite loop and it said the index position was 1 and it printed that message in a infinite loop on the screen.

I understand that parentheses force the compiler to evaluate what is in them first, but I guess that's all I know. I don't quite follow why I must include them here.

ReggaetonKing Jun 4th, 2007 12:59 AM

Because the function find() is called first and then returns a integer value and then tests that to see if it does not equal string:npos.

357mag Jun 4th, 2007 4:20 AM

Sorry. I'm a musician by trade. I don't really follow. If I don't put the parentheses in somehow the test condition never fails and I get an infinite loop or something? The author in the book never explained why the parentheses are needed either.

Eoin Jun 4th, 2007 4:57 AM

Without them is possible the expression is being evaluated as
:

index = (text.find(word, index)) != string::npos)
meaning 'index' becomes a boolean value, probably 1 or 0. And because this happens during each iteration of the loop 'index' is being rest and so never advances beyond a single
:

index += word.length()

ReggaetonKing Jun 4th, 2007 7:59 AM

:

(index = text.find(word, index))
That line of code below searches for the first occurrence of the substring word in the current string, starting at position index. If found, return the position of the first character. If not, return a special value - string::npos.

:

index = text.find(word, index) != string::npos

is the same as

index = (text.find(word, index) != string::npos)


357mag Jun 5th, 2007 1:03 AM

I understand basically how the program works. It will start at index position 0 which is the beginning of the string and it will return the index position of where the word "is" was found. But I still don't understand why the parentheses are needed to make the code work properly.

You said the find function is called first. Why is that? Is it because the parentheses force the compiler to do that? And then you said something about it returns an integer value. What integer value? Is it a certain number? Or any number? A 1 or 0 perhaps?

I'm trying, but failing.

DaWei Jun 5th, 2007 3:22 AM

Okay, listen up. In the following code,
:

(index = text.find(word, index)) != string::npos
index will be set equal to text.find (word, index), then that will be tested against string::npos.

Without the parentheses, text.find (word, index) will be tested against string::npos, first and then the result of that test (a boolean value) will be assigned to index. Being a musician has nothing to do with it. Set aside some time to study precedence.

357mag Jun 5th, 2007 5:34 AM

Quote:

Originally Posted by DaWei (Post 128751)
Okay, listen up. In the following code,
:

(index = text.find(word, index)) != string::npos
index will be set equal to text.find (word, index), then that will be tested against string::npos.

Without the parentheses, text.find (word, index) will be tested against string::npos, first and then the result of that test (a boolean value) will be assigned to index. Being a musician has nothing to do with it. Set aside some time to study precedence.


I noticed without the parentheses the output window said "found at position 1" and then it kept running an infinite loop. So 1 would be the boolean value representing true then I guess? So then without the parentheses it finds the word I'm looking for ("is") at position 8 in the string, and that evaluates to true (which is 1) and that 1 is assigned to index. I don't know, is that correct?


All times are GMT -5. The time now is 2:33 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC