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. Is this c/c++ code valid

Is this c/c++ code valid

Scheduled Pinned Locked Moved C / C++ / MFC
c++
12 Posts 7 Posters 0 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.
  • R Raj Prathap

    int i = 10; int k = 100; switch(i) { case 0: cout << "case 0" <<endl; break; if(k == 100) { case 10: cout <<"The device type is 100 so attribute 10 is valid"<<endl; break; } else { case 100: cout <<"attribute 100 is there" << endl; break; } break; default: cout <<"default"<<endl; } Please confirm whether 'if' could be placed inside a switch statement. Pratap

    B Offline
    B Offline
    BadKarma
    wrote on last edited by
    #2

    a. yes b. no c. maybe d. none of the above

    codito ergo sum

    R 1 Reply Last reply
    0
    • R Raj Prathap

      int i = 10; int k = 100; switch(i) { case 0: cout << "case 0" <<endl; break; if(k == 100) { case 10: cout <<"The device type is 100 so attribute 10 is valid"<<endl; break; } else { case 100: cout <<"attribute 100 is there" << endl; break; } break; default: cout <<"default"<<endl; } Please confirm whether 'if' could be placed inside a switch statement. Pratap

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #3

      Is this a real question? or you are new to c++?

      R 1 Reply Last reply
      0
      • B BadKarma

        a. yes b. no c. maybe d. none of the above

        codito ergo sum

        R Offline
        R Offline
        Raj Prathap
        wrote on last edited by
        #4

        :) Thanks for the reply. Please post your choice.

        1 Reply Last reply
        0
        • H Hamid Taebi

          Is this a real question? or you are new to c++?

          R Offline
          R Offline
          Raj Prathap
          wrote on last edited by
          #5

          I'm not new to C++ :omg: . Today I had to write some code which includes swith cases, but case lables are valid in only some conditions (depending on the device type). I was not completly sure that the code is invalid, and wrote a test pgm in vs2k3, and it worked fine. Before checking in, wanted a confirmation.

          1 Reply Last reply
          0
          • R Raj Prathap

            int i = 10; int k = 100; switch(i) { case 0: cout << "case 0" <<endl; break; if(k == 100) { case 10: cout <<"The device type is 100 so attribute 10 is valid"<<endl; break; } else { case 100: cout <<"attribute 100 is there" << endl; break; } break; default: cout <<"default"<<endl; } Please confirm whether 'if' could be placed inside a switch statement. Pratap

            _ Offline
            _ Offline
            _AnsHUMAN_
            wrote on last edited by
            #6

            Raj Prathap wrote:

            Please confirm whether 'if' could be placed inside a switch statement.

            and the reason behind this doubt for not using an 'if' in a switch block is ... :doh: !!! :-D

            Somethings seem HARD to do, until we know how to do them. ;-)_AnShUmAn_

            1 Reply Last reply
            0
            • R Raj Prathap

              int i = 10; int k = 100; switch(i) { case 0: cout << "case 0" <<endl; break; if(k == 100) { case 10: cout <<"The device type is 100 so attribute 10 is valid"<<endl; break; } else { case 100: cout <<"attribute 100 is there" << endl; break; } break; default: cout <<"default"<<endl; } Please confirm whether 'if' could be placed inside a switch statement. Pratap

              R Offline
              R Offline
              Rajesh R Subramanian
              wrote on last edited by
              #7

              Raj Prathap wrote:

              Please confirm whether 'if' could be placed inside a switch statement.

              Yes. But compile the following code and you'll know how much the compiler 'cares' about the IF statement.

              int i=4;
              switch(i)
              {
                if(false) //Whaaa?!
                {
                   case 4:
              	AfxMessageBox("Whack my head with that oldskool keyboard");
                          break;
                }
              }
              

              Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

              CPalliniC 1 Reply Last reply
              0
              • R Raj Prathap

                int i = 10; int k = 100; switch(i) { case 0: cout << "case 0" <<endl; break; if(k == 100) { case 10: cout <<"The device type is 100 so attribute 10 is valid"<<endl; break; } else { case 100: cout <<"attribute 100 is there" << endl; break; } break; default: cout <<"default"<<endl; } Please confirm whether 'if' could be placed inside a switch statement. Pratap

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #8

                Is it the encrypted version of some nice piece of code? :) BTW on my system (VS2005) it compiles, but at runtime the switch condition overrides (breaks :rolleyes:) the if one: is it what you're expecting?

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                In testa che avete, signor di Ceprano?

                R 1 Reply Last reply
                0
                • R Rajesh R Subramanian

                  Raj Prathap wrote:

                  Please confirm whether 'if' could be placed inside a switch statement.

                  Yes. But compile the following code and you'll know how much the compiler 'cares' about the IF statement.

                  int i=4;
                  switch(i)
                  {
                    if(false) //Whaaa?!
                    {
                       case 4:
                  	AfxMessageBox("Whack my head with that oldskool keyboard");
                              break;
                    }
                  }
                  

                  Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP

                  CPalliniC Offline
                  CPalliniC Offline
                  CPallini
                  wrote on last edited by
                  #9

                  Rutherford would say: "the switch statement targets its case condition passing the if block like as if you fired a 15-inch shell at a piece of tissue paper" :laugh:

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                  In testa che avete, signor di Ceprano?

                  1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    Is it the encrypted version of some nice piece of code? :) BTW on my system (VS2005) it compiles, but at runtime the switch condition overrides (breaks :rolleyes:) the if one: is it what you're expecting?

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                    R Offline
                    R Offline
                    Raj Prathap
                    wrote on last edited by
                    #10

                    yeah, it was encrypted with 128bit RSA algo. Its great that you could decrypt it :) .Just kidding.. Its neither encrypted nor a question from an interview. I had to write some code today and I re written the code in different way also. But I gor a doubt if this code is valid and wanted a confirmation.

                    CPalliniC 1 Reply Last reply
                    0
                    • R Raj Prathap

                      yeah, it was encrypted with 128bit RSA algo. Its great that you could decrypt it :) .Just kidding.. Its neither encrypted nor a question from an interview. I had to write some code today and I re written the code in different way also. But I gor a doubt if this code is valid and wanted a confirmation.

                      CPalliniC Offline
                      CPalliniC Offline
                      CPallini
                      wrote on last edited by
                      #11

                      The code compiles. But I doubt it works as you expect (i.e. avoid such misleading constructs). :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                      In testa che avete, signor di Ceprano?

                      1 Reply Last reply
                      0
                      • R Raj Prathap

                        int i = 10; int k = 100; switch(i) { case 0: cout << "case 0" <<endl; break; if(k == 100) { case 10: cout <<"The device type is 100 so attribute 10 is valid"<<endl; break; } else { case 100: cout <<"attribute 100 is there" << endl; break; } break; default: cout <<"default"<<endl; } Please confirm whether 'if' could be placed inside a switch statement. Pratap

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #12

                        Raj Prathap wrote:

                        Please confirm whether 'if' could be placed inside a switch statement.

                        Why would you go to all the trouble of creating this post when you could have just compiled it to find out? :rolleyes:

                        "Love people and use things, not love things and use people." - Unknown

                        "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

                        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