Thread: Having trouble understanding if and if else statements.

  1. #1
    Registered User
    Join Date
    Sep 2017
    Posts
    4

    Having trouble understanding if and if else statements.

    This is for a homework assignment and I'm very new to coding

    I was wondering if you were allowed to make two if statements, using the Boolean "&&" symbol, where the statements were similar in one aspect but different in another. The code keeps giving me an error when I add the if else statement, even though the statements look for different requirements. I have to add a lot more requirements and if else statements, however I can't even get the first part to work. Thank you for any help


    Code:
    #include <stdio.h>
    
    
    int main()
    {
      int n,m;
     scanf("%d", &n);
     scanf("%d", &m);
      
      if (n >= 0 && n-m < 0)
      {
        printf("A\n");
      }
      
      if else (n >= 0 && n-m > 0)
      {
        printf("B\n");
      }
     
      else 
      {
        printf("failed code\n");
      }
      
      return 0;
    }
    Last edited by BurritoSniper; 09-29-2017 at 10:16 AM. Reason: Mispelled printf

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    if else (n >= 0 && n-m > 0)
    You have the "if" and "else" reversed.

    Code:
    if( /*...*/ )
    {
        /*...*/
    }
    else if( /*...*/ )
    {
        /*...*/
    }
    else
    {
        /*...*/
    }

  3. #3
    Registered User
    Join Date
    Sep 2017
    Posts
    4
    Wow that's embarrassing. Honestly I thought if it was glowing blue then I had written it right and just completely overlooked that time and time again. I've been looking up solutions for 45 minutes now thinking it was a problem with what's inside the statement, not the statement itself. Thank you
    Last edited by BurritoSniper; 09-29-2017 at 10:24 AM.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You're welcome.

    The syntax highlighting only means you spelled the keywords correctly, not that they're in the correct order.

    Welcome to the frustrating world of debugging

  5. #5
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    82
    What error message did you get from your linter? The one below, from gcc, is a little more helpful than the one from cppcheck. My editor showed the one from gcc with a red squiggly underline under the else. Good tools help.

    christopher@water:~/cboard$ cppcheck ifelse.c
    Checking ifelse.c...
    [ifelse.c:15]: (error) syntax error
    christopher@water:~/cboard$ gcc ifelse.c
    ifelse.c: In function ‘main’:
    ifelse.c:15:6: error: expected ‘(’ before ‘else’
    if else (n >= 0 && n-m > 0)
    ^
    (The green arrow points to else in my console)
    Last edited by christophergray; 09-30-2017 at 05:59 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding Strings - Statements
    By YannB in forum C Programming
    Replies: 2
    Last Post: 01-29-2014, 08:26 AM
  2. Replies: 3
    Last Post: 09-12-2013, 06:22 PM
  3. Trouble understanding some code please help
    By Shinzu911 in forum C Programming
    Replies: 1
    Last Post: 02-19-2013, 09:01 PM
  4. trouble with if else statements
    By ilinan87 in forum C++ Programming
    Replies: 8
    Last Post: 10-28-2009, 03:20 AM
  5. a little trouble understanding
    By zema in forum C++ Programming
    Replies: 3
    Last Post: 11-05-2003, 05:49 PM

Tags for this Thread