Announcement

Collapse
No announcement yet.

And/or operators

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • And/or operators

    Can I use and or operators in the same if statement?

    Like this

    if ($C -lt 3 -and $C -gt 0 -or $C -eq 7) {perform this and that action}

    Will this and that be performed if C is either 1, 2 or 7, otherwise nothing will occur. Is this possible, or is my use of and or operators wrong?

  • #2
    Re: And/or operators

    Check the example syntax http://technet.microsoft.com/en-us/l.../hh847789.aspx

    You need to bracket the AND together similar to
    if ((($C -lt 3) -and ($C -gt 0)) -or ($C -eq 7) )

    Note the extra brackets around both parts of the AND which will evaluate and then have the OR applied
    Tom Jones
    MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
    PhD, MSc, FIAP, MIITT
    IT Trainer / Consultant
    Ossian Ltd
    Scotland

    ** Remember to give credit where credit is due and leave reputation points where appropriate **

    Comment


    • #3
      Re: And/or operators

      What will happen if I leave it at
      ($C -lt 3 -and $C -gt 0 -or $C -eq 7) ?

      Because I tried this

      $A=integer value
      $B=integer value
      $C= $A+$B
      if ($C -lt 3 -and $C -gt 0 -or $C -eq 7) {write-host Value of C is $C}

      And when I set the value of A and B as such, that C would equal 1, 2 or 7, I would get the output "Value of C is x"

      But if I set value of A and B so C would not equal 1,2 or 7, I did not get any output.
      So I'm just curious to know?

      Comment


      • #4
        Re: And/or operators

        May well work, but use extra brackets to enforce rules (BODMAS)
        Tom Jones
        MCT, MCSE (2000:Security & 2003), MCSA:Security & Messaging, MCDBA, MCDST, MCITP(EA, EMA, SA, EDA, ES, CS), MCTS, MCP, Sec+
        PhD, MSc, FIAP, MIITT
        IT Trainer / Consultant
        Ossian Ltd
        Scotland

        ** Remember to give credit where credit is due and leave reputation points where appropriate **

        Comment


        • #5
          Re: And/or operators

          Thank you for explaining.

          Comment

          Working...
          X