title: What is the reason that I can't go out the for-loop?
body: <p>guys.
  This is my first question in stackoverflow.
  This code is a code for program solving and the question is asking to find the smallest sequence that has no neighboring redundant sequence in it. The input is the sequence's length, and the sequence should have only 1, 2, 3.
  For example, if I input 7, the output should be 1213121.
  Now, my real question is about the for-loop.</p>
  <pre><code>#include &lt;bits/stdc++.h&gt;
  
  using namespace std;
  
  int mini = 0xffffff;
  int n;
  string s1, s2;
  
  bool isgood(string s)
  {
      for(int i=1;i&lt;=s.size()/2;i++)
      {
          s1 = &quot;&quot;;
          s2 = &quot;&quot;;
          for(int j=s.size()-1;j&gt;=s.size()-i;j--)
          {
              s1 += s[j];
          } //this works ok
          for(int k=s.size()-1-i;k&gt;=s.size()-i-i;k--)//Problem
          {
              cout&lt;&lt;&quot;*** &quot;&lt;&lt;k&lt;&lt;endl; //I printed the k, and it printed 0, -1, -2, -3......, and it didn't go out.
              s2 += s[k];
          }
          if(s1.compare(s2)==0)
          {
              return false;
          }
      }
      return true;
  }
  
  void f(string s)
  {
      if(s.size()==n)
      {
          if(mini&gt;stoi(s))
          {
              mini=stoi(s);
          }
          return;
      }
      else
      {
          for(int r=1;r&lt;=3;r++)
          {
              if(isgood(s+char(r+'0'))==true)
              {
                  f(s+char(r+'0'));
              }
          }
      }
  }
  
  int main()
  {
      scanf(&quot;%d&quot;, &amp;n);
      f(&quot;1&quot;);
      printf(&quot;%d&quot;, mini);
  }
  </code></pre>
  <p>as you see in my code, there is a for, but it doesn't go out.
  when I changed it to for(int k=s.size()-1-i;k&gt;s.size()-i-i-1;k--), it didn't made the exact goal, but the program worked.
  Please help me what is the problem, and how can I solve it.</p>