how to do a while loop on batch
Good morning, below is a code I'm doing in batch. One of the requirements is to have a while loop in the code. However, this process was not explained to us in class. I have also included the instructions given for the program. Not sure how to write the logic for this one. Hopefully you guys can help. Thank you for your time. Using Windows Batch in command prompt. - Implement division by 0, with error trapping, using if and while() commands,
How: - Append your name, current date and time to a NEW file called results.txt - Use a while loop. - If the user enters the value 55 for the first or the second number, you must terminate this implementation of divide by 0. - Ask the user to enter the first number and check its value. - Ask the user to enter the second number. - If the second number is ZERO inform the user and ask for a correct second number. - If the second number is NOT a zero, do the division, display all numbers in this computation using labels, and add them to the results.txt file.
@ echo off setlocal enabledelayedexpansion echo results.txt echo Roberto Martinez >> results.txt set dt=%date:~7,2%-%date:~4,2%-%date:~10,4%_%time:~0,2%_%time:~3,2%_%time:~6,2% echo %dt% >> results.txt goto :main :main setlocal set /p choice1="Enter your first number" set /p choice2="Enter your second number" if !choice1! == 55 goto :eof if !choice2! == 0 goto :eof :loop goto :loop echo. echoo ^^ endlocal
First of all, StackOverflow has the policy of not making your homework for you, so I'll only give you a general idea of what you need.
You already have a goto, and a label inside your batch. When implementing a while loop, you can use these like this:
@echo off :loop set /p "test=something other than 0 " if "%test%"=="0" goto :loop
This can be compared to a while (input == 0) loop, as batch has no actual while command.
Also, to divide something, you can use something like this:
@echo off set five=5 set /a result=10/%five% echo %result% pause
Note the /a after set