![]() |
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. |
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.
|
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.
|
Without them is possible the expression is being evaluated as
:
index = (text.find(word, index)) != string::npos):
index += word.length() |
:
(index = text.find(word, index)):
index = text.find(word, index) != string::npos |
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. |
Okay, listen up. In the following code,
:
(index = text.find(word, index)) != string::nposWithout 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. |
Quote:
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