{"id":26,"date":"2007-12-17T11:06:33","date_gmt":"2007-12-17T09:06:33","guid":{"rendered":"http:\/\/tom.paschenda.org\/blog\/?p=26"},"modified":"2007-12-17T11:06:33","modified_gmt":"2007-12-17T09:06:33","slug":"classic-for-loop-in-a-windows-batch-file","status":"publish","type":"post","link":"https:\/\/tom.paschenda.org\/blog\/?p=26","title":{"rendered":"Classic for-loop in a windows batch file"},"content":{"rendered":"<p>Question: How can a classic for loop<br \/>\n<code><br \/>\nfor (int i=0;i&lt;10;++i) {<br \/>\n\/\/ do stuff<br \/>\n}<br \/>\n<\/code><br \/>\nbe realized in a windows batch file?<\/p>\n<p>Answer:<br \/>\n<code><br \/>\necho off<br \/>\nSET \/a i=0<\/code><\/p>\n<p>:loop<br \/>\nIF %i%==10 GOTO END<br \/>\necho This is iteration %i%.<br \/>\nSET \/a i=%i%+1<br \/>\nGOTO LOOP<\/p>\n<p>:end<br \/>\necho That&#8217;s it!<\/p>\n<p>The answer uses goto and the \/A option of the set command.  Without that option, SET interprets the right side of the equals sign as a string and i would contain &#8216;i+1&#8217;, then &#8216;i+1+1&#8217; and so on. To make SET interpret the right side as an arithmetic operation, the option \/A is required. Enter SET \/? to get a detailed explanation of the SET command&#8217;s capabilities.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Question: How can a classic for loop for (int i=0;i&lt;10;++i) { \/\/ do stuff } be realized in a windows batch file? Answer: echo off SET \/a i=0 :loop IF %i%==10 GOTO END echo This is iteration %i%. SET \/a i=%i%+1 GOTO LOOP :end echo That&#8217;s it! The answer uses goto and the \/A option [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-26","post","type-post","status-publish","format-standard","hentry","category-windows-batch-programming"],"_links":{"self":[{"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/26","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26"}],"version-history":[{"count":0,"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/26\/revisions"}],"wp:attachment":[{"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tom.paschenda.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}