Home > Software engineering >  Batch IF Statement Block Syntax
Batch IF Statement Block Syntax

Time:12-06

I have the following code (in batch) to test part of a larger code that isnt working:

@echo off
set /a slotted55=8
set /a place=55
set /a key3=8
set /a key1=0
set /a key2=0
set /a key4=0
set /a key5=0
set /a key6=0
set /a strike=0
 if %place%==55 (
    if %key1%==%slotted55% (
        set key1=V
        set slotted55=V
        goto 5x5one
    ) else (
        if %key2%==%slotted55% (
            set key2=V
            set slotted55=V
            goto 5x5one
        )
    ) else (
        if %key3%==%slotted55% (
            set key3=V
            set slotted55=V
            goto 5x5one
        )
    ) else (
        if %key4%==%slotted55% (
            set key4=V
            set slotted55=V
            goto 5x5one
        )
    ) else (
        if %key5%==%slotted55% (
            set key5=V
            set slotted55=V
            goto 5x5one
        )
    ) else (
        if %key6%==%slotted55% (
            set key6=V
            set slotted55=V
            goto 5x5one
        )
    ) else (
        set /a strike=%strike%  1
        if %strike%==3 goto gameover
        set %slotted55%=X
        goto 5x5one
    )
 )
:gameover
echo gameover
pause
exit
:5x5one
echo good
pause

For some reason, it keeps crashing. I've looked at multiple other questions, but none of them seem to fit this situation. All the variables seem correct, and there are no missing operands, but its not like batch gives you an error message. Can anyone help me?

CodePudding user response:

As I stated in my comment, you do not require any of the else statements, alos as your opening if statement does not have an else, you should begin by using the opposite comparison, thus not having everything nested into one set of parentheses.

@Echo Off
SetLocal EnableExtensions
Set "place=55"
Set /A "key3=slotted55=8"
Set /A "key1=key2=key4=key5=key6=strike=0"

If "%place%" NEq "55" GoTo gameover

If "%key1%" == "%slotted55%" (
    Set "key1=V"
    Set "slotted55=V"
    GoTo 5x5one
)
If "%key2%" == "%slotted55%" (
    Set "key2=V"
    Set "slotted55=V"
    GoTo 5x5one
)
If "%key3%" == "%slotted55%" (
    Set "key3=V"
    Set "slotted55=V"
    GoTo 5x5one
)
If "%key4%" == "%slotted55%" (
    Set "key4=V"
    Set "slotted55=V"
    GoTo 5x5one
)
If "%key5%" == "%slotted55%" (
    Set "key5=V"
    Set "slotted55=V"
    GoTo 5x5one
)
If "%key6%" == "%slotted55%" (
    Set "key6=V"
    Set "slotted55=V"
    GoTo 5x5one
)
Set /A strike  = 1
If %strike% Equ 3 GoTo gameover
Set "slotted55=X"
GoTo 5x5one

:gameover
Echo gameover
Pause
Exit /B

:5x5one
Echo good
Pause

CodePudding user response:

Because of where you put your )s, all of your else statements are looking at each other instead of the other if statements that you've written. Right now, your logic looks something like this:

if "%x%"=="5" (
    echo X is 5
) else (
    echo X is not 5
) else (
    echo X is somehow neither 5 nor is it not 5
)

If you combine your else with the next if, your logic makes sense again.

set /a strike=0
if %place%==55 (
    if %key1%==%slotted55% (
        set key1=V
        set slotted55=V
        goto 5x5one
    ) else if %key2%==%slotted55% (
        set key2=V
        set slotted55=V
        goto 5x5one
    ) else if %key3%==%slotted55% (
        set key3=V
        set slotted55=V
        goto 5x5one
    ) else if %key4%==%slotted55% (
        set key4=V
        set slotted55=V
        goto 5x5one
    ) else if %key5%==%slotted55% (
        set key5=V
        set slotted55=V
        goto 5x5one
    ) else if %key6%==%slotted55% (
        set key6=V
        set slotted55=V
        goto 5x5one
    ) else (
        set /a strike=%strike%  1
        if %strike%==3 goto gameover
        set %slotted55%=X
        goto 5x5one
    )
)
  • Related