how to decreasing loop ?

Hi all,

This simple decreasing loop does not work :

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, const char *argv[]) {
  int i = 10;
  for(; i == 0; i--) {
    cout << i << endl;
    if (i == 3) break;
  }
  return 0;
}


When I use the debugger, it does not enter the loop. Any reason ?
Change the loop condition

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, const char *argv[]) {
  int i = 10;
  for(; i >= 0; i--) {
    cout << i << endl;
    if (i == 3) break;
  }
  return 0;
}


Or you can write more simply

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, const char *argv[]) {
  int i = 10;
  
  do
  {
    cout << i << endl;
    if (i == 3) break;
  } while ( i-- );
  return 0;
}
Thanks vlad. Stupid am I !!!
@lalebarde: it's important to realize that the condition in the for loop is the running condition, not the quitting condition.
Topic archived. No new replies allowed.