I have a problem when I use for
loop in c++. It did not work as I thought. I am stuck begin from below short code:
#include <iostream>
#include<vector>
using namespace std;
int main() {
vector<int> v; //v.size() is 0 now
for(int i=1;i<(v.size()-1);i+=2)
{
cout<<"i think this line will not be show!";
}
return 0;
}
This code print on console:
i think this line will not be show!
In above code. At for
loop statement, variable i
is initialized by 1, v.size()-1
will be -1 because v.size()
is zero now. So, I think i<(v.size()-1)
will be false
and the for
loop will be ignored and the program go to return statement without printing any character on console. But when I ran it I saw the for loop
still work and print line i think this line will not be show!
. I really dont understand how does the for
loop work in this case. Can someone explain it for me! Thank you so much.