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

Author Topic: If/and code  (Read 4159 times)

0 Members and 1 Guest are viewing this topic.

batchbandit

    Topic Starter


    Newbie

    • Experience: Familiar
    • OS: Windows 10
    If/and code
    « on: August 30, 2019, 04:16:02 PM »
    Is there a code I can use in this case?

    I have set up the set /p choice codes, but this code

    Code: [Select]
    :game
    Player 1 chose %p1choice%!
    Player 2 chose %choose%!
    if %p1choice%='1' and %choose%='2'
    echo Player 2 wins!

    does not work.

    The code closes itself. So, I want to know - is there a code I can use that works like the "if [choice=1] and [choice=2] then echo [xyz]"

    Salmon Trout

    • Guest
    Re: If/and code
    « Reply #1 on: August 31, 2019, 04:28:08 AM »
    You don't get logical AND operator in batch but you can simulate it with 2 IF tests on one line

    You have the syntax wrong for IF tests, you need double equals signs, and if you have quotes they must be on left and right side of the == and the action you take if both tests are passed must be on the same line or in a bracket block

    if '%p1choice%'=='1' if '%choose%'=='2' echo Player 2 wins!

    if '%p1choice%'=='1' if '%choose%'=='2' (
        echo Player 2 wins!
    )


    Study batch syntax at a good site like SS64.com.

    Play around at the prompt

    C:\>set p=1

    C:\>set q=1

    C:\>set r=2

    C:\>if '%p%'=='1' echo yes
    yes

    C:\>if '%p%'=='1' if '%q%'=='1' echo yes
    yes

    C:\>if '%p%'=='1' if '%q%'=='2' echo yes

    C:\>if '%p%'=='1' if '%r%'=='2' echo yes
    yes







    Salmon Trout

    • Guest
    Re: If/and code
    « Reply #2 on: August 31, 2019, 05:37:17 AM »
    Or you can do this

    if '%p1choice%'=='1' (
        if '%choose%'=='2' (
            echo Player 2 wins!
        )
    )