I was watching my friend try to solve a programming challenge and at one point, his code looked like this:
#include <iostream>
#include <string>
using namespace std;
int main(){
//Prompt user to enter string
const string SPACE {" "};
string input;
cout << "Please enter String you would like to see in a pyramid: ";
getline(cin, input);
//Iterate string
for(int i {0}; i < input.length(); i++){ //Going through each character in string...
for(int j {0}; j <= i; j++){ //Going through each character in string from 0 to i...
cout << input[j];
}
for (int k {i}; k < input.length(); k--){ //Going backwards from string.length() to 0
cout << input[k - 1];
}
cout << endl;
}
}
The question I have is, why does the third for loop (last loop) not result in an infinite loop? k = 0 as the initial condition for the first iteration (since i = 0), and k is decremented by 1 for each iteration. Since the terminating condition should theoretically ALWAYS be false (k should always be less than input.length() for a positive integer length string), why does this loop terminate?