Redirecting using if statements?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ProjectConquest
Posts: 6
Joined: 14 Jan 2018 03:05

Redirecting using if statements?

#1 Post by ProjectConquest » 18 Jan 2018 12:16

Hello everyone, I got some, what was presumably, thought as simple code but refuses to work. I set two variables to two different numbers and based on the values redirects it to different code blocks. Any help is appreciated!
Also if I delete the Main2 code block and get rid of the "goto" statements in the if's it won't even run.

Code: Select all

set health= 3
set maxhealth= 7

:Main
cls
if %health%+5 GEQ %maxhealth% (
set health= %maxhealth%
goto Main2
)
if %health%+5 LSS %maxhealth% (
set health+= 5
goto Main2
)
echo Your health has been restored by 5!
pause >nul
goto Main2


:Main2
cls
echo %health%
pause

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Redirecting using if statements?

#2 Post by Squashman » 18 Jan 2018 12:47

You can only do arithmetic operations with the SET command and the /A option.

ProjectConquest
Posts: 6
Joined: 14 Jan 2018 03:05

Re: Redirecting using if statements?

#3 Post by ProjectConquest » 18 Jan 2018 20:47

Thank you for that, rookie mistake. But after I change it to /a it prints out 8 health even though the if statement should set it to 7 because 3 + 5 is 8 which is greater/equal to 7 which then i command it to set it to max health. But it seems its reading the second if statement that says if Less than 7 do +5 to health. Which doesn't make any sense since it should read the first if statement and immediately direct it to Main2, Any suggestions?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Redirecting using if statements?

#4 Post by Squashman » 18 Jan 2018 22:44

ProjectConquest wrote:
18 Jan 2018 20:47
Thank you for that, rookie mistake. But after I change it to /a it prints out 8 health even though the if statement should set it to 7 because 3 + 5 is 8 which is greater/equal to 7 which then i command it to set it to max health. But it seems its reading the second if statement that says if Less than 7 do +5 to health. Which doesn't make any sense since it should read the first if statement and immediately direct it to Main2, Any suggestions?
I am not much for blind faith. Especially with people on the internet. I don't have a crystal ball either so I can't see what you changed in your code.

ProjectConquest
Posts: 6
Joined: 14 Jan 2018 03:05

Re: Redirecting using if statements?

#5 Post by ProjectConquest » 19 Jan 2018 01:25

Here I'll post my code for you. I didn't change anything really though.

Code: Select all

set health= 3
set maxhealth= 7

:Main
cls
if %health%+5 GEQ %maxhealth% (
set /a health= %maxhealth%
goto Main2
)
if %health%+5 LSS %maxhealth% (
set /a health+= 5
goto Main2
)
echo Your health has been restored by 5!
pause >nul
goto Main2


:Main2
cls
echo %health%
pause

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Redirecting using if statements?

#6 Post by penpen » 19 Jan 2018 04:21

As Squashman said:
Squashman wrote:
18 Jan 2018 12:47
You can only do arithmetic operations with the SET command and the /A option.
This includes the if-command:
The if-command doesn't support math operations (addition in this case).

If there is a non numerical character then the if-command treats the values to check as text strings.
In alphanumerical order the letter '3' is lower than '7', so you get this result.

You have to create a dummy variable (using "set /A") with the result of that operation:

Code: Select all

:...
set /a "test=health+5"
if %test% GEQ %maxhealth% (
	set /a health=%maxhealth%
	goto Main2
)
:...
penpen

ProjectConquest
Posts: 6
Joined: 14 Jan 2018 03:05

Re: Redirecting using if statements?

#7 Post by ProjectConquest » 20 Jan 2018 03:10

Okay, sorry for being such a noob, great little trick with the dummy variable! Will definitely have to use that more often! Thanks a lot!

Post Reply