c - What is the difference between IF and ELSE IF clauses? -


i'm on path of learning c k&r. besides exercises in book, i'm doing myself. wrote following code, is, counts input, gives feedback how many words left reach "goal" of 10 words , congratulates reach 10 words.

#include <stdio.h>  main() {     /* programm read input, check number of words,     , congratulate when reach value of 10 words*/      int c, nw, counter;      nw = 0;     counter = 0;      while (nw < 10)     {         c = getchar();          if (c == ' ' || c == '\t' || c == '\n')         {             counter = 0;         }          else if (c != ' ' || c != '\t' || c != '\n')         {             if (counter == 0)             {                 counter = 1;                 ++nw;             }             printf("only %d words left\n", 10-nw );         }     } } 

ok, in version code not count blanks words, resulting in correct output of words left.

at first wrote code "if" instead of "else if". did counted blanks words.

the question asking why ? wheres difference in using if or else if.

as understand is, compiler check whether condition met or not met. should case when using if. same expect else if instead of if, can't figure out problem is.

it looks might case of overthinking problem. logic you've ended with, aside being wrong, overly complicated. question of difference between if , else if fair, , promise address in answer.

first, let me restate trying do:

read input, count number of words, , congratulate when reach 10 words

from code, believe intention split words based on spaces, tabs, , newlines. there many ways split words, purposes of question, intended method fine.

the problem, of course, logic doesn't work. have condition can never false:

else if (c != ' ' || c != '\t' || c != '\n') 

think (hint: else doesn't change condition itself). out loud if helps: looking condition c isn't space, or c isn't tab, or c isn't newline. remember logical or (||) inclusive or, in other words, expression true if any of conditions true. example, let's suppose c space. first condition (c != ' ') fails, 2nd one, c != '\t' true because space not tab. thus, entire expression true. in fact, expression true any value of c.

but else?

as said, else part of else if doesn't make difference here. thing else if differently tack on new condition if statement. let's @ simpler example:

if (a == 1) {     /* 1 */ }  if (a != 1 && b == 2) {     /* isn't 1, b == 2 */ } 

that's example of 2 independent if statements. it's perfect example of use else, because noticed, 2nd if statement tests inverse of 1st. (a != 1). so, above can simplified follows:

if (a == 1) {     /* 1 */ else if (b == 2) {     /* isn't 1 , b 2 */ } 

in else if block, needn't test a != 1, that's implied because evaluate else statement if 1st if conditional false.

note else if combination of 2 separate keywords. equivalent to:

else {     if (b == 2) { ... } } 

however, convention omit optional braces , write as:

else if (b == 2) { ... } 

in fact, in cases don't need 2nd if @ all:

if (a == 1) {     printf("a 1!\n"); } else {     printf("a isn't 1. in fact, it's %d.\n", a); } 

simplified version

so, there's no need caught in else if. focus on logic, , best simplify it. simplify you, encourage skip on part , try on own first:

char c; int in_word = 0;  while (nw < 10) {     c = getchar();      if (c == ' ' || c == '\t' || c == '\n') {         /* if in word, count word! */         if (in_word) {             nw++;             printf("you have %d words go!", 10 - nw);         }         in_word = 0; /* not in word */     } else {         in_word = 1; /* we're in word     } } 

Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -