0

I have no idea at all how to make an "and if" statement for my batch file, and I hope I could get some help in here.

The line I'm trying to make:

if %a%==X/O and if %b%==X/O goto done
2
  • 1
    Simply remove the and... moreover I recommend to put all expressions left and right from the == in between "" to avoid trouble with empty variables... Commented Nov 14, 2015 at 13:04
  • at the start of the script, it changes the variables to 1, 2, 3 and so on. Commented Nov 14, 2015 at 22:10

2 Answers 2

3

In batch, if statements are quite simple, but unfortunately don't have the same amount of functionality of many other coding languages such as JavaScript.

In batch, to write an if statement, there are only 4 parts. Of course at the beginning you have to specify that it is a if statement, then you add the two things you will be comparing, and the operator. Once that is done, you specify the function you want the program to do if the condition is true. For example:

if "%type%" == "5" goto end

Also, one last thing to note. If your condition is false, the batch file will simply go to the next line. So you might want to add something like this:

:start
set num1=1
set num2=2
set num3=3
set /a totalnum=%num1%+%num2%+%num3%
if "%totalnum%" == "100" goto end
goto start
:end
exit
Sign up to request clarification or add additional context in comments.

Comments

0

Batch doesn't have support for ands or ors in if logic. You can simulate an and by nesting your conditions.

if %a%==X/O (
    if %b%==X/O (
        goto done
    )
)

Similarly, you can simulate an or by having two separate checks.

if %a%==X/O goto done
if %b%==X/O goto done

3 Comments

this does not seem to work, i typed in: if %a%==X/O ( if %b%==X/O ( if %c%==X/O ( if %d%==X/O ( if %e%==X/O ( if %f%==X/O ( if %g%==X/O ( if %H%==X/O ( if %i%==X/O goto done ) ) ) ) ) ) ) ) yet notthing happends when a-i is x or o.
@conecall - Oh, THAT'S what you wanted? You should have specified that. The code is expecting the input to be the literal string X/O.
but i used that before with only 1 variable, and it worked just fine. but do you know how i can do this then?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.