This was my code without using else if:
#include <stdio.h>
main()
{
long s = 0, t = 0, n = 0;
int c;
while ((c = getchar()) != EOF)
if (c == ' ')
++s;
if (c == '\t')
++t;
if (c == '\n')
++n;
printf("spaces: %d tabulations: %d newlines: %d", s, t, n);
}
This is the code using else if:
#include <stdio.h>
main()
{
long s = 0, t = 0, n = 0;
int c;
while ((c = getchar()) != EOF)
if (c == ' ')
++s;
else if (c == '\t')
++t;
else if (c == '\n')
++n;
printf("spaces: %d tabulations: %d newlines: %d", s, t, n);
}
For a reason, not using the else if doesn't work. What is the reason? I know that using if does it one by one while using else if stops at the first statement that is true. This has a difference in performance. Anyhow not using else if in this particular (if not other) while loop doesn't seem to work.
Thanks.
Aucun commentaire:
Enregistrer un commentaire