Page 1 of 1

Creating a While Loop in Batch | Batch Basics

Posted: 23 Aug 2017 05:15
by PaperTronics
If you have knowledge of programming languages other than Batch, you may know what is a WHILE loop. A While loop is basically a loop which has similar functionality to the FOR loop.
As you might know, there is no WHILE loop in Batch. But we can create a While loop in Batch using very Simple but Effective Batch Programming.

Creation

We can easily create a WHILE loop in Batch by mixing together IF conditions, the GOTO command and some plain old Batch logic.

Below is demonstration of a very basic WHILE loop in Batch:

Code: Select all

@echo off 

Set count=0
:a
if %count% gtr 10 (goto :b) else (set /a count+=1)
Echo I am Running %count% time
goto :a

:b
Echo I have finished my journey
pause
exit /b


This code will print the text "I am Running [value of count variable] time" ten times on the screen and then print "I have finished my journey". This was a very basic WHILE loop. There are countless other creative methods you can utilize this WHILE loop!


If you have any suggestions or questions I will be pleased to answer them!


PaperTronics

Re: Creating a While Loop in Batch | Batch Basics

Posted: 23 Aug 2017 06:07
by elzooilogico

Re: Creating a While Loop in Batch | Batch Basics

Posted: 23 Aug 2017 06:53
by ShadowThief

Re: Creating a While Loop in Batch | Batch Basics

Posted: 24 Aug 2017 05:17
by PaperTronics
@elizooilogico - Nice relevant post that you linked, I wasn't able to read the whole post due to lack of time, but I will in the future.

@ShadowThief - I wasn't able to get your intentions clearly by that link you provided? What did you want to show?



PaperTronics

Re: Creating a While Loop in Batch | Batch Basics

Posted: 24 Aug 2017 06:56
by ShadowThief
"If," "goto", and "while" should all be the same color because they're all commands.

This advice extends to your website as well.

Re: Creating a While Loop in Batch | Batch Basics

Posted: 25 Aug 2017 14:42
by PaperTronics
ShadowThief wrote:"If," "goto", and "while" should all be the same color because they're all commands.

This advice extends to your website as well.


Ah, thanks for pointing that out!

But you're wrong about the advice on my website since I and kvc have changed the layout completely! The article design is also fixed thanks to your advice back in February. Have a look at it again and I guarantee that you won't regret it!



PaperTronics