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
  1. Home
  2. General Programming
  3. C#
  4. More if's?

More if's?

Scheduled Pinned Locked Moved C#
regexquestion
25 Posts 6 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.
  • M MasterSharp

    How do you make it so an if statement has a lot of things to match? It's hard to explain, but instead of '&&', is there a way to make it so you can have a bunch of if's to follow? I hope you understand what I mean... Thanks in advance, M.S.

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

    You can do one of three things: 1 - if (x == 0 && y ==1 && z == 2) 2 - if (x == 0) { if(y == 1) { if ( z== 2 ) { 3 - if (ValidateForm()) { } where you have a lot of stuff to check and you factor it into a function ( this works only if the things you are validating are member variables ) If you had lots of different ifs, as in if (x == 0 ) { } else if (x == 1) { } else if (x==2) then you'd want a switch statement. But, if you have a lot of things to check, there's no way around that you need to specify what they all are.

    Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

    M 1 Reply Last reply
    0
    • C Christian Graus

      You can do one of three things: 1 - if (x == 0 && y ==1 && z == 2) 2 - if (x == 0) { if(y == 1) { if ( z== 2 ) { 3 - if (ValidateForm()) { } where you have a lot of stuff to check and you factor it into a function ( this works only if the things you are validating are member variables ) If you had lots of different ifs, as in if (x == 0 ) { } else if (x == 1) { } else if (x==2) then you'd want a switch statement. But, if you have a lot of things to check, there's no way around that you need to specify what they all are.

      Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

      M Offline
      M Offline
      MasterSharp
      wrote on last edited by
      #4

      No, actually, I mean (though this isn't correct, something close to the idea), like this: if (condition) || condition || condition and so on... or is that what the || operator does?

      P C 2 Replies Last reply
      0
      • M MasterSharp

        No, actually, I mean (though this isn't correct, something close to the idea), like this: if (condition) || condition || condition and so on... or is that what the || operator does?

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #5

        Like this?

        if ( a )
        {
        ...
        }
        else
        {
        if ( b )
        {
        ...
        }
        else
        {
        ...
        }
        }

        M 1 Reply Last reply
        0
        • P PIEBALDconsult

          Like this?

          if ( a )
          {
          ...
          }
          else
          {
          if ( b )
          {
          ...
          }
          else
          {
          ...
          }
          }

          M Offline
          M Offline
          MasterSharp
          wrote on last edited by
          #6

          No, I mean having more things that have to be true (for example) in the if statement? I'm sorry, it's complicated...

          C P 2 Replies Last reply
          0
          • M MasterSharp

            No, actually, I mean (though this isn't correct, something close to the idea), like this: if (condition) || condition || condition and so on... or is that what the || operator does?

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

            I don't follow. condition is different every time ? if (x == 1 || y == 2 || z == 3) will succeed if only one of those statements is true. && requires they are all true.

            Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

            M 1 Reply Last reply
            0
            • M MasterSharp

              No, I mean having more things that have to be true (for example) in the if statement? I'm sorry, it's complicated...

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

              Only because you're not making sense. Try giving a real world example, so we can work out what you mean.

              Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

              1 Reply Last reply
              0
              • M MasterSharp

                No, I mean having more things that have to be true (for example) in the if statement? I'm sorry, it's complicated...

                P Offline
                P Offline
                PIEBALDconsult
                wrote on last edited by
                #9

                That's what && is for.

                C 1 Reply Last reply
                0
                • P PIEBALDconsult

                  That's what && is for.

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

                  Is that what he means ? MasterSharp, this is why we discouraged you from writing owner drawn controls yesterday. Not because we don't want to help, but because you need to grasp the basics first. If this is what you meant, I urge you again to buy a book on C# and work through it, so you cover these sort of elementary gaps in your knowledge and build a firm foundation you can build on.

                  Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                  1 Reply Last reply
                  0
                  • C Christian Graus

                    I don't follow. condition is different every time ? if (x == 1 || y == 2 || z == 3) will succeed if only one of those statements is true. && requires they are all true.

                    Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                    M Offline
                    M Offline
                    MasterSharp
                    wrote on last edited by
                    #11

                    How many '&&'s can you use, is what I mean. I guess that what it boils down to.

                    P C 2 Replies Last reply
                    0
                    • M MasterSharp

                      How many '&&'s can you use, is what I mean. I guess that what it boils down to.

                      P Offline
                      P Offline
                      PIEBALDconsult
                      wrote on last edited by
                      #12

                      Until your & key wears out, and even then you can copy-and-paste.

                      M 1 Reply Last reply
                      0
                      • P PIEBALDconsult

                        Until your & key wears out, and even then you can copy-and-paste.

                        M Offline
                        M Offline
                        MasterSharp
                        wrote on last edited by
                        #13

                        Oh... I thought you could only use it once...

                        P 1 Reply Last reply
                        0
                        • M MasterSharp

                          Oh... I thought you could only use it once...

                          P Offline
                          P Offline
                          PIEBALDconsult
                          wrote on last edited by
                          #14

                          Well, next time... TRY IT!! You ain't gonna hurt the computer.

                          M E C 3 Replies Last reply
                          0
                          • P PIEBALDconsult

                            Well, next time... TRY IT!! You ain't gonna hurt the computer.

                            M Offline
                            M Offline
                            MasterSharp
                            wrote on last edited by
                            #15

                            Well, it just seemed it would screw up tough, so thanks for the advice!

                            P 1 Reply Last reply
                            0
                            • M MasterSharp

                              Well, it just seemed it would screw up tough, so thanks for the advice!

                              P Offline
                              P Offline
                              PIEBALDconsult
                              wrote on last edited by
                              #16

                              You can't screw up... unless you're testing a hard-disk-wiper-outer or something.

                              E 1 Reply Last reply
                              0
                              • M MasterSharp

                                How do you make it so an if statement has a lot of things to match? It's hard to explain, but instead of '&&', is there a way to make it so you can have a bunch of if's to follow? I hope you understand what I mean... Thanks in advance, M.S.

                                M Offline
                                M Offline
                                Matthew Cuba
                                wrote on last edited by
                                #17

                                Hi,

                                MasterSharp wrote:

                                Forum C# Subject: Re: More if's? Sender: MasterSharp Date: 20:08 1 Oct '07 How do you make it so an if statement has a lot of things to match? It's hard to explain, but instead of '&&', is there a way to make it so you can have a bunch of if's to follow? I hope you understand what I mean... Thanks in advance, M.S.

                                The long and short of it is that you can chain together as many expressions as you like with &&'s and ||'s. I'd recommend picking up an introductory C# book - there are a number of good ones. Jesse Liberty's "Learning C#" is a good one, or you might find the Head First C# book interesting to read - it isn't your standard C# text. Apress has some good ones too. I have several books on the subject and each provides a different perspective. I think that you'd really benefit by getting one of these books and working through it. Most have exercises, and those are helpful. There are a bunch of great folks here at Code Project who can answer questions, but you're not going to learn the language nearly as well as if you invest some time with a good text - your question indicates that you are new to the language and probably haven't worked through an introductory text, because just about any good introductory text would have provided the answer to that question and TONS of others you are likely to have along the way. .NET development, and programming in general, is a journey and NONE of us has it all figured out - if you'll invest the time upfront with a good text on the language, and then experiment along the way, read the MS-provided help pages, you'll likely be quite capable at developing C# apps in a reasonable period of time. Of course, when you are stumped on a program, there are a bunch of folks here that would be happy to take a look at the code snippet and respond. At least that has been my experience. Good Luck!

                                It isn't enough to do well in life. One must do good when and where one can. Otherwise, what's the point?

                                1 Reply Last reply
                                0
                                • M MasterSharp

                                  How many '&&'s can you use, is what I mean. I guess that what it boils down to.

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

                                  Seriously, you will never learn anything if you don't try basic stuff like this before asking others to explain it to you.

                                  Christian Graus - Microsoft MVP - C++ "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )

                                  M 1 Reply Last reply
                                  0
                                  • P PIEBALDconsult

                                    You can't screw up... unless you're testing a hard-disk-wiper-outer or something.

                                    E Offline
                                    E Offline
                                    El Corazon
                                    wrote on last edited by
                                    #19

                                    PIEBALDconsult wrote:

                                    unless you're testing a hard-disk-wiper-outer or something.

                                    well, you know I started out with my first computer project to be a disk operating system... but then I smartly decided to start with drawing user interfaces. So far, in the last 27 years of computer programmng, I have crashed many computers, but I have never managed to crash a user. :->

                                    _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

                                    1 Reply Last reply
                                    0
                                    • P PIEBALDconsult

                                      Well, next time... TRY IT!! You ain't gonna hurt the computer.

                                      E Offline
                                      E Offline
                                      El Corazon
                                      wrote on last edited by
                                      #20

                                      PIEBALDconsult wrote:

                                      Well, next time... TRY IT!!

                                      Programming is like pinball. The reward for doing it well is the opportunity of doing it again. - programmers' saying

                                      _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

                                      P 1 Reply Last reply
                                      0
                                      • E El Corazon

                                        PIEBALDconsult wrote:

                                        Well, next time... TRY IT!!

                                        Programming is like pinball. The reward for doing it well is the opportunity of doing it again. - programmers' saying

                                        _________________________ Asu no koto o ieba, tenjo de nezumi ga warau. Talk about things of tomorrow and the mice in the ceiling laugh. (Japanese Proverb)

                                        P Offline
                                        P Offline
                                        PIEBALDconsult
                                        wrote on last edited by
                                        #21

                                        But my wife is better at pinball than I am... oh, wait a minute... Programming Sex is like pinball. The reward for doing it well is the opportunity of doing it again. :cool:

                                        M 1 Reply Last reply
                                        0
                                        • P PIEBALDconsult

                                          Well, next time... TRY IT!! You ain't gonna hurt the computer.

                                          C Offline
                                          C Offline
                                          ChrisKo 0
                                          wrote on last edited by
                                          #22

                                          "ERROR: You've mortally wounded the compiler, please refrain from using Visual Studio again. FOREVER!" :-D

                                          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