Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. using return statement in "conditional operator (? :) "ternary operator

using return statement in "conditional operator (? :) "ternary operator

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mushtaque Nizamani
    wrote on last edited by
    #1

    Hi, can we use return statement in "?:" conditional ternay operator. I am using like this (avar == 2)?return:executesomethng(..); but the compiler is not allowing me do that, and it gives the error C2059(syntax error : 'token'). Regards, Mushq

    R C B 3 Replies Last reply
    0
    • M Mushtaque Nizamani

      Hi, can we use return statement in "?:" conditional ternay operator. I am using like this (avar == 2)?return:executesomethng(..); but the compiler is not allowing me do that, and it gives the error C2059(syntax error : 'token'). Regards, Mushq

      R Offline
      R Offline
      Roger Stoltz
      wrote on last edited by
      #2

      Either way, in my opinion you're putting a lot of time and effort to write code that is hard to read and understand, even for yourself if you return to it two months from now. My suggestion is that you rewrite it: use an ordinary if-statement.


      "It's supposed to be hard, otherwise anybody could do it!" - selfquote

      1 Reply Last reply
      0
      • M Mushtaque Nizamani

        Hi, can we use return statement in "?:" conditional ternay operator. I am using like this (avar == 2)?return:executesomethng(..); but the compiler is not allowing me do that, and it gives the error C2059(syntax error : 'token'). Regards, Mushq

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #3

        A well written method would have only one return point.  The way to do this is, if (avar != 2) {     executesomething(..); }

        Christian Graus - C++ MVP 'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert

        1 Reply Last reply
        0
        • M Mushtaque Nizamani

          Hi, can we use return statement in "?:" conditional ternay operator. I am using like this (avar == 2)?return:executesomethng(..); but the compiler is not allowing me do that, and it gives the error C2059(syntax error : 'token'). Regards, Mushq

          B Offline
          B Offline
          Bill Gates Antimatter Particle
          wrote on last edited by
          #4

          (avar == 2)?return:executesomethng(..); err am i missing something here or should this be: (avar == 2) ? return executesomethng(); : return executesomethngelse(); :|

          "Life begins at 140"

          K 1 Reply Last reply
          0
          • B Bill Gates Antimatter Particle

            (avar == 2)?return:executesomethng(..); err am i missing something here or should this be: (avar == 2) ? return executesomethng(); : return executesomethngelse(); :|

            "Life begins at 140"

            K Offline
            K Offline
            krmed
            wrote on last edited by
            #5

            Actually, I agree that there should be just one return, and the correct syntax for what you're trying would be

            return (avar == 2) ? executesomething() : executesomethingelse();

            Hope that helps.

            Karl - WK5M PP-ASEL-IA (N43CS) PGP Key: 0xDB02E193 PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups