Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: boolean functions  (Read 4145 times)

0 Members and 1 Guest are viewing this topic.

gregory

    Topic Starter


    Beginner

    Thanked: 1
    • Experience: Experienced
    • OS: Linux variant
    boolean functions
    « on: October 26, 2008, 02:02:01 PM »
    Is there a way to use boolean functions in the IF statement? Things like logical and, or, xor.

    Something that would act, like this looks
    Code: [Select]
    if %var%==x OR y (then...)

    devcom



      Apprentice

      Thanked: 37
      Re: boolean functions
      « Reply #1 on: October 26, 2008, 02:36:14 PM »
      Code: [Select]
      for %%a in (x y) do (
      if '%var%' equ '%%a' (
      commands
      )
      )
      Download: Choice.exe

      gregory

        Topic Starter


        Beginner

        Thanked: 1
        • Experience: Experienced
        • OS: Linux variant
        Re: boolean functions
        « Reply #2 on: October 26, 2008, 04:14:10 PM »
        Thanks for the help devcom.

        If I am understanding your code correctly, both x and y will be compared against %var% separately. Problem is, this way, there is a true/false produced for both x and y. I need to logically say (X AND Y) or (X OR Y). That way I have only one true/false produced. I hope that makes sense. Perhaps I'm the one confused.

        dslgeek



          Beginner
          Re: boolean functions
          « Reply #3 on: October 26, 2008, 06:57:40 PM »
          Maybe you want something else.
          In Boolean logic OR is inclusive, not exclusive.
          Do you want to say either but not both? You use the XOR which is exclusive.
          Do that make sense? Lets say A could be 0 or 1 and B could be 0 or 1. You want the case where  A or B has a value f 1, but not both.
          You would have something like like this
          if A + B = 0 goto getouttahere
          if A + B = 2 goto getouttahere
          some commands
          ....
          :getouttahere

          Sorry that is not real batch code, but you get the idea. :)


          gregory

            Topic Starter


            Beginner

            Thanked: 1
            • Experience: Experienced
            • OS: Linux variant
            Re: boolean functions
            « Reply #4 on: October 27, 2008, 12:48:05 AM »
            An example:
            Code: [Select]
            @echo off

            set var=x

            for %%x in (x y) do (
            if "%var%"=="%%x" (
            echo true
            ) else (
            echo false
            )
            )

            pause

            The output of the example:
            Code: [Select]
            true
            false
            Press any key to continue . . .

            X and Y are processed separately and compared against "%var%" separately, yielding one true and one false. I am needing to have them processed as one logical statement which would yield only one true/false. Perhaps this is just not possible with a batch file.

            Dias de verano

            • Guest
            Re: boolean functions
            « Reply #5 on: October 27, 2008, 01:56:40 AM »
            Maybe these will help

            1. from set /? (My asterisks)

            Code: [Select]
            The /A switch specifies that the string to the right of the equal sign
            is a numerical expression that is evaluated.  The expression evaluator
            is pretty simple and supports the following operations, in decreasing
            order of precedence:

                ()                  - grouping
                ! ~ -               - unary operators
                * / %               - arithmetic operators
                + -                 - arithmetic operators
                << >>               - logical shift
                &                   - bitwise and
                ^                   - bitwise exclusive or
                |                   - bitwise or
                = *= /= %= += -=    - assignment
                  &= ^= |= <<= >>=
                ,                   - expression separator

            *** If you use any of the logical or modulus operators, you will need to
            enclose the expression string in quotes.***

              Any non-numeric strings in the
            expression are treated as environment variable names whose values are
            converted to numbers before using them.  If an environment variable name
            is specified but is not defined in the current environment, then a value
            of zero is used.  This allows you to do arithmetic with environment
            variable values without having to type all those % signs to get their
            values.

            2.

            http://groups.google.com/group/alt.msdos.batch.nt/search?hl=en&group=alt.msdos.batch.nt&q=boolean&qt_g=Search+this+group


            gregory

              Topic Starter


              Beginner

              Thanked: 1
              • Experience: Experienced
              • OS: Linux variant
              Re: boolean functions
              « Reply #6 on: October 27, 2008, 01:36:26 PM »
              Thanks everyone.  :)

              @ Dias de verano

              Thank you.
              Through the link you provided, I was able to find this ftp://garbo.uwasa.fi/pc/link/tscmd.zip. Many good examples found there, including how to incorporate AND/OR/XOR/NOT into IF statements.

              An example of logical OR the long drawn out batch file way:
              IF "%var%" is something other than "a" or "b" or "", then ask user for valid input.
              Code: [Select]
              : logical.or.loop
              set logical.or=
              if "%var%"=="a" (set logical.or=true)
              if "%var%"=="b" (set logical.or=true)
              if "%var%"=="" (set logical.or=true)
              if not "%logical.or%"=="true" (
              echo.
              set var=
              set /p var=Invalid input, please try again.
              goto logical.or.loop
              )