Thread: simple question about (else) statement

  1. #1
    Registered User
    Join Date
    Mar 2022
    Posts
    2

    simple question about (else) statement

    Here are 2 pieces of code. The first has the else statement tied to the wrong IF. The first is right. My problem is I'm failing to see how the first is tied to the wrong else.

    Example of right code
    .
    Code:
    #include <stdio.h>
    int main()
    {
    int card = 1;
    if (card > 1)
    card = card - 1;
    if (card < 7)
    puts("Small card");
    else {
    puts("Ace!");
    }
    return 0;
    }
    example of wrong code.

    Code:
    #include <stdio.h>
    int main()
    {
    int card = 1;
    if (card > 1)
    card = card - 1;
    if (card < 7)
    puts("Small card");
    else {
    puts("Ace!");
    }
    return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,126
    Well it helps if you indent it properly to begin with.
    Code:
    #include <stdio.h>
    int main()
    {
      int card = 1;
      if (card > 1)
        card = card - 1;
      if (card < 7)
        puts("Small card");
      else {
        puts("Ace!");
      }
      return 0;
    }
    vs
    Code:
    #include <stdio.h>
    int main()
    {
      int card = 1;
      if (card > 1)
        card = card - 1;
      if (card < 7)
        puts("Small card");
      else {
        puts("Ace!");
      }
      return 0;
    }
    But the two versions are the same, so it's hard to see what your point/question is anyway.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2022
    Posts
    2
    Quote Originally Posted by Salem View Post
    Well it helps if you indent it properly to begin with.
    That's exactly what I thought. I was just skimming through a book and doing the exercises in my head. I copy and pasted the code the "wrong" one, I have highlighted sure as heck spit back an error.

    I apologize for the indentation its a copy paste from a pdf exercise.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,126
    > I copy and pasted the code the "wrong" one, I have highlighted sure as heck spit back an error.
    Still in the dark at this end.

    The code you posted compiles and runs just fine.
    Code:
    $ gcc foo.c 
    $ ./a.out 
    Small card
    $
    Copy paste verbatim the actual error output of your compiler.

    No description of the error, no fuzzy pictures of the screen, the actual text.

    > I apologize for the indentation its a copy paste from a pdf exercise.
    One common source of difficulty is whatever generated the pdf turned ASCII quotes "" into some awful mangled pretty version
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple if Statement
    By raj21 in forum C Programming
    Replies: 2
    Last Post: 08-31-2015, 05:09 AM
  2. simple If statement not working
    By amitj93 in forum C Programming
    Replies: 6
    Last Post: 01-22-2013, 10:35 PM
  3. error with simple printf statement
    By jollykiddo in forum C Programming
    Replies: 2
    Last Post: 12-16-2011, 02:11 AM
  4. Simple if/else statement problem
    By benrogers in forum C Programming
    Replies: 13
    Last Post: 02-06-2011, 04:11 PM
  5. Why won't this simple statement work?
    By nick753 in forum C++ Programming
    Replies: 10
    Last Post: 10-18-2010, 11:20 PM

Tags for this Thread