problem with if statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • stmfc
    New Member
    • May 2007
    • 65

    #1

    problem with if statement

    whats wrong with this function:

    Code:
    int max(int x, int y) {
        return (if (x > y) { return x; } else { return y; });
    }
    i get an "expected an expression" error
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Use this instead:

    Code:
    int max(int x, int y)
    {
        if (x > y)
            return x; 
        else
            return y;
    }
    Regards

    Comment

    • stmfc
      New Member
      • May 2007
      • 65

      #3
      Originally posted by zodilla58
      Use this instead:

      Code:
      int max(int x, int y)
      {
          if (x > y)
              return x; 
          else
              return y;
      }
      Regards
      thanks for the reply.
      i know that there are many alternative ways to perfom that functionality.
      what i want to figure out is the rule which doesnt allow the above code to be considered as legal

      Comment

      • Meetee
        Recognized Expert Contributor
        • Dec 2006
        • 928

        #4
        Originally posted by stmfc
        thanks for the reply.
        i know that there are many alternative ways to perfom that functionality.
        what i want to figure out is the rule which doesnt allow the above code to be considered as legal
        Your function has return type int. In your previous code, you have taken return withing return like:

        Code:
        return(if(...) return x; else return y;)
        That is not proper way to return the value.

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          Originally posted by stmfc
          whats wrong with this function:

          Code:
          int max(int x, int y) {
              return (if (x > y) { return x; } else { return y; });
          }
          i get an "expected an expression" error
          Try it.
          [code=c]
          return x>y?x:y;
          [/code]

          Kind regards,
          Dmjpro.

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by stmfc
            whats wrong with this function:


            Code: ( text )
            int max(int x, int y) {
            return (if (x > y) { return x; } else { return y; });
            }


            i get an "expected an expression" error
            The function has an int return type so you have to return an int. That means the code between the parentheses must evaluate to an int. It doesn't.

            The key is in the "expected an expression" reported by the compiler. An expression must evaluate to true or false where false is 0 and true is not false.

            if statements don't have a value.

            Comment

            Working...