for and while loop continues to loop even though it shouldn't C++

Viewed 61

Not a duplicate of C++ for loop continue to loop even though it shouldn't

I found out that for loop continues even though the conditions are false. An example is here

int isSubstring(std::string s1, std::string s2)
{
    for (int i = 0; i < s2.size() - s1.size(); i++) {
        
    }
    return -1;
}

If I call it like isSubtring("abcd", "a"); it should've returned -1 right? But it doesn't. In Visual Studio debugger I saw the value of i goes up forever. But, if call this

for (int i = 0; i < -3; i++) {
        std::cout << "\ni=" << i;
    }

It behaves as intended. This also applies for while loop. My question is, why is that?

Edit: removed unnecessary code

0 Answers
Related