Wrong condition in do-while loop


#1

In the start of the first chapter, the following code is presented as a sample of while loop:

[code]#include <stdio.h>

int main(int argc, const char * argv[])
{
int i = 0;
while (i < 12) {
printf("%d. Aaron is Cool\n", i);
i++;
}
return 0;
}[/code]

In the do-while loop the original code above is rewrote to use a do-while loop, but the condition was wrong.

[code]#include <stdio.h>

int main(int argc, const char * argv[])
{
int i = 0;
do {
printf("%d. Aaron is Cool\n", i);
i++;
} while (i < 13);
return 0;
}[/code]

But, the condition must be the same as the original: i < 12. With the new condition, one more line will be printed “12. Aaron is Cool”.


#2

Nice catch. I’ll fix it in the next edition.


#3

The code has been fixed in the latest edition to where the correct condition is checked: i < 12. However, the flow-chart for the do-while loop still shows i < 13.

Just wanted to point this out.


#4

hi all. sorry my english is bad :blush:
i have 2 codes
1.

[code]#include <stdio.h>

int main(int argc, const char * argv[])
{

int i;
for ( i=99; i>-1; i=i-3) {
    printf ("%d\n", i);
    if (i%5 ==0) {
        printf("FOUND ONE\n", i);
}

}
return 0;
}[/code]

and 2.

[code]#include <stdio.h>

int main(int argc, const char * argv[])
{

int i;
for ( i=99; i>-1; i=i-3) {
    printf ("%d\n", i);
    if (i%5 ==0)
        printf("FOUND ONE\n", i);
}

}[/code]

both work
question: why we must use return 0;???