1

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.

melpomene
  • 84,125
  • 8
  • 85
  • 148

3 Answers3

7

Correctly indented, your first program looks like this:

#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);
}

The body of a while loop is a single statement.

if ... else if ... else if ... else all forms one big statement. By separating your conditions into several statements (if, if, if), you've moved all but the first one out of the while loop.

To avoid this problem, always use a compound statement (i.e. a block: { ... }) as the body of a while or if statement.

By the way, main() hasn't been valid C since 1999. It should be int main(void).

melpomene
  • 84,125
  • 8
  • 85
  • 148
  • Thanks, really appreciate it. Also I am studying C from the original book by Brian Kernighan, which in main is used without return data type, as it is from the 1980s. I used Visual Studio to compile it, and it didn't give any errors. Thanks for the correction, though. – Philosophical Ragdoll Jan 30 '18 at 19:43
1

From standard (Only selected the relevant portions of the grammar rules)

   iteration-statement: while ( expression ) statement

   statement: selection-statement

   selection-statement:
             if ( expression ) statement
             if ( expression ) statement else statement

You are writing an iteration statement - which consists of while(expression) and a single statement. That statement in your case a selection statement - now check it. Unless you use else if or else it is not a single statement - rather it's multiple statement with all except one in the while statement, rest are outside of it.

Your code basically means this

  while ((c = getchar()) != EOF){ <--- the selection statement
    if (c == ' ')
        ++s;
  }<-- 
  if (c == '\t')
        ++t;
  if (c == '\n')
        ++n;

Putting brackets in the blocks and indentation will make you refrain from this kind of errors.

user2736738
  • 30,591
  • 5
  • 42
  • 56
1

Because you forgot the braces around the while, so it only loops through the first if statement, then exits the loop and evaluates the other two if statements.

Also, why are you not using a switch statement?

while ((c = getchar()) != EOF) {
    switch(c) {
       case ' ':
          ++s;
          break; 
       case  '\t':
          ++t;
          break;
       case '\n':
          ++n;
          break;
    }
}
printf("spaces: %d tabulations: %d newlines: %d", s, t, n);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Chronocidal
  • 6,827
  • 1
  • 12
  • 26