arr[i]
evaluates to a character. If it is a null character then loop will terminate. You can think of it as
for(com/tag/int">int i=0; arr[i] != '\0'; i++) {}
In C++ any value other than 0
evaluates to true
. '\0'
is encoded as 0
and therefore arr[i]
will evaluate to false when its value becomes '\0'
.
manpreet
Best Answer
2 years ago
I am reading counting sort algorithm on Geeksforgeeks. I have understood the algorithm but I am having a problem in the following for loop syntax. I have tried exactly the same syntax in cpp shell but it is not working.
for(int i=0; arr[i]; i++) {
++count[arr[i]];
}
Here, arr[] is a char array and count[] is an integer array.
Please help me to understand this kind of for loop.