Thread: Who can tell me how it works ?

  1. #1
    Registered User
    Join Date
    May 2011
    Location
    Wuhan, China
    Posts
    3

    Who can tell me how it works ?

    I read the book of K&R "The C Programming Language (Second Edition)", in the part 1.5.4 (words counting), it shows a programm(See the end).

    This program is OK, but I don't know how the if-else sentence works, see, the 1st if contains c == '\n', the 2nd also contains '\n', and once the 2nd "if" do the "state == IN", will the last else if work?

    So anyway, it's different from the "if-else" I learned before, I just know that all the "if" conditions should be different, and computer will execute only one sentence. So how this program run? Thanx for answer.

    P.S: the program
    Code:
    #include <stdio.h>
    
    #define IN 1
    #define OUT 0
    
    main()
    {
        int c, nl, nw, nc, state;
    
    	state = OUT;
    	nl = nw = nc = 0;
    	while ((c = getchar()) != EOF){
    	    ++nc;
    		if (c == '\n')
    			++nl;
    		if (c == ' ' || c == '\n' || c == '\t')
    			state = OUT;
    		else if (state == OUT){
    		    state = IN;
    			++nw;
    		}
    	}
    	printf("%d %d %d\n", nl, nw, nc);
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You have several if statements in a row here. They are not related.

    Code:
    if (c == '\n')
            ++nl;
    That is one if statement. If c matches the criteria, nl is incremented.

    Code:
    if (c == ' ' || c == '\n' || c == '\t')
            state = OUT;
    else if (state == OUT) {
            state = IN;
            ++nw;
    }
    This is a completely separate if statement with two tests. If the first test is true, then state is sent to OUT; otherwise the second test is performed. If that is true, then state is set to IN and nw is incremented; if neither are true, nothing happens -- there's no else going with the last if.

  3. #3
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    There is no else if statement, it is merely an illusion caused by writing the next if as part of the else and not adding brackets since it is just a one line instruction. There are ifs and elses in C, no else ifs. I think that is the source of confusion for the OP.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  4. #4
    Registered User
    Join Date
    May 2011
    Location
    Wuhan, China
    Posts
    3
    Ah, I understand, the 1st if statement is just independent, it has no relationship with the if - elseif (the latter one is a group) which comes later, the computer will test the 1st "if", and it will also test the later ones. Thannk you!

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Wuhan, China
    Posts
    3
    yeah, you're right. It's just an illusion, I understand now, thanx~
    Quote Originally Posted by claudiu View Post
    There is no else if statement, it is merely an illusion caused by writing the next if as part of the else and not adding brackets since it is just a one line instruction. There are ifs and elses in C, no else ifs. I think that is the source of confusion for the OP.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    1
    i don't know about the else if being an illusion. You can do this:

    Code:
    if
    
    else if
    
    else.
    But if you do this, it will be a syntax error
    Code:
    if
    
    else {
       if
    }
    else

  7. #7
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well obviously because the first if has two elses. If you had written that correctly it would have been:

    Code:
    if{
    }
    
    else{
       if{
      }
      else{
      }
    }
    Which is the exact same as writing
    Code:
    if
    else if
    else
    Hence there is no else if statement. There are only ifs and elses. Just because you write them on different lines doesn't add any semantic value to the language.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    2
    So if statements dont have to be paired with else? for instance
    Code:
    x = getint()
    if (x = 1)
    {
    blah;
    } 
    blah2;
    return 0
    does this if statement just move on, or do you have to put the blah2; return 0 in an else statement?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    They mean different things. else happens when the part in the if statement is false, as opposed to things after the if, which just happen.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    [QUOTE=Rogerdodger91;1028992]So if statements dont have to be paired with else? for instance
    Code:
    if (x == 1)
    Just a quick note, I'm sure it's just a typo, but be careful of = vs ==. A single = is for assignment, a double == for comparison. When used in a conditional expression, x = 1 is always true, and it sets x to 1, possibly changing it's value. So in the example you posted, blah would always get executed. You probably want that second = in your code sample.

  11. #11
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    As tabstop pointed out, an isolated if just means, if the condition is true, do this extra stuff in the if, and keep doing everything after the if as you would normally do even if the if clause wasn't there.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Rogerdodger91 View Post
    So if statements dont have to be paired with else? for instance
    Code:
    x = getint()
    if (x = 1)
    {
    blah;
    } 
    blah2;
    return 0
    does this if statement just move on, or do you have to put the blah2; return 0 in an else statement?
    I see the others are giving you good explanations but maybe something more visual would help...

    Code:
    if (x == 11)
     printf("Golly!");
    else
     printf("Oh darn");
    Ok... that prints Golly if x is equal to 11 or oh darn if it's not. It will never print both statements.

    Code:
    x = 10;
    
    if (x == 11)
      printf("Golly");
    else if (x == 10)
      printf("bingo");
    else
     printf("oh darn");
    Here the sequence is is x == 11, no, do the else ... runs into the next if ... is x == 10, yes, do this... the final else is never executed. The effect is that only one of three statements is executed...

    Your other inquiry about elseless ifs is actually used more often than if - else, at least in my code.

    Code:
    if (x == 11)
      printf("Golly");
    
    printf("thanks for playing");
    return 0;
    In this case the Golly is only printed if x == 11, but the program signs off no matter what.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Now it works, now it won't
    By skytreader in forum C Programming
    Replies: 3
    Last Post: 11-23-2009, 09:56 AM
  2. nothing works
    By psycho88 in forum C Programming
    Replies: 2
    Last Post: 12-01-2005, 11:23 AM
  3. How come this works?
    By caduardo21 in forum C Programming
    Replies: 3
    Last Post: 08-20-2005, 02:07 AM
  4. How come this works?
    By caduardo21 in forum C++ Programming
    Replies: 4
    Last Post: 07-16-2005, 05:59 PM
  5. it never works...
    By Ryce in forum Game Programming
    Replies: 5
    Last Post: 08-30-2001, 07:31 PM

Tags for this Thread