| | |
For loop test condition
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 1,405
Reputation:
Solved Threads: 114
0
#2 25 Days Ago
Your condition for that loop is: Now think, is there any way that expression will ever be false in order to break out the loop? If s is a null-terminator, the loop wont break as s isn't a space, and the same the other way round.
The condition would make sense if instead of having ||, you used &&, but I don't know your intentions.
C++ Syntax (Toggle Plain Text)
(s[i] != '\0') || (s[i] != ' ')
The condition would make sense if instead of having ||, you used &&, but I don't know your intentions.
I need pageviews! most fun profile ever :)
•
•
Join Date: Nov 2009
Posts: 10
Reputation:
Solved Threads: 0
2
#3 25 Days Ago
yes u can .. but m afraid it would be an infinite loop...
your test condition is: s[i]!='\0')|| s[i]!=' '
which means if any of these two conditions is true then overall condition will be true which means loop will be incremented...
Here is the Truth table for OR:
False||False=False
False||True=True
True||False=True
True||True=True
which means condition will only be false when both conditions are false.. i.e loop will break only when s[i]='\0' as well as =' ' at the same time .. which is not actually possible...
but if u want loop to be terminated.. then.. use AND
truth table for AND is:
False&&False=False
False&&True=False
True&&Fasle=False
True&&True=True
which implies condition will fail if any 1 of the condition is false.. i.e if s[i]='/0' or ' ' any of these..
hope u get it..
njjoy!!
- Snehil
your test condition is: s[i]!='\0')|| s[i]!=' '
which means if any of these two conditions is true then overall condition will be true which means loop will be incremented...
Here is the Truth table for OR:
False||False=False
False||True=True
True||False=True
True||True=True
which means condition will only be false when both conditions are false.. i.e loop will break only when s[i]='\0' as well as =' ' at the same time .. which is not actually possible...
but if u want loop to be terminated.. then.. use AND
truth table for AND is:
False&&False=False
False&&True=False
True&&Fasle=False
True&&True=True
which implies condition will fail if any 1 of the condition is false.. i.e if s[i]='/0' or ' ' any of these..
hope u get it..
njjoy!!
- Snehil
![]() |
Other Threads in the C++ Forum
- Previous Thread: Help Writing to .txt File
- Next Thread: compile pthreads in windows netbeans
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






