I tried the following code on https://www.onlinegdb.com/
Also tried it on Mac.
Couldn't find out why the for loop in strangeForLoop will not stop when i is equal to 0?
#include <stdio.h>
#include <stdint.h>
void strangeForLoop();
void normalForLoop();
const uint32_t COUNTDOWN = 3;
int BREAK = -2;
int main()
{
strangeForLoop();
printf("\n===========\n");
normalForLoop();
return 0;
}
void strangeForLoop() {
for(uint32_t i = COUNTDOWN; i>=0; i--) {
printf("strange i : %d\n", i);
if (i == 0)
printf("--> i: %d\n", i);
if (i == BREAK) break;
}
}
void normalForLoop() {
for(int i = COUNTDOWN; i>=0; i--) {
printf("normal i : %d\n", i);
if (i == 0)
printf("==> i: %d\n", i);
if (i == BREAK) break;
}
}
Any help will be greatly appreciated.