gm-docs-parser 1.0.0

A collection of typings for GameMaker Studio 2 manual pages
Documentation
<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head>

  <script type="text/javascript" language="JavaScript">
    //<![CDATA[
    function reDo() {
      if (innerWidth != origWidth || innerHeight != origHeight)
        location.reload();
    }
    if ((parseInt(navigator.appVersion) == 4) && (navigator.appName == "Netscape")) {
      origWidth = innerWidth;
      origHeight = innerHeight;
      onresize = reDo;
    }
    onerror = null;
  //]]>
  </script>
  <style type="text/css">/*<![CDATA[*/

    < !-- div.WebHelpPopupMenu {
      position: absolute;
      left: 0px;
      top: 0px;
      z-index: 4;
      visibility: hidden;
    }

    p.WebHelpNavBar {
      text-align: right;
    }

    -->
  
/*]]>*/</style>

  <script type="text/javascript">//<![CDATA[

    gRootRelPath = "../../..";
    gCommonRootRelPath = "../../..";
    gTopicId = "9.1.2.0_5";
  
//]]></script>

  <script type="text/javascript" src="../../../template/scripts/rh.min.js"></script>
  <script type="text/javascript" src="../../../template/scripts/common.min.js"></script>
  <script type="text/javascript" src="../../../template/scripts/topic.min.js"></script>
  <script type="text/javascript" src="../../../template/scripts/topicwidgets.min.js"></script>
<script type="text/javascript" src="../../../whxdata/projectsettings.js"></script>
  <link rel="stylesheet" type="text/css" href="../../../template/styles/topic.min.css"/>
  <link rel="stylesheet" type="text/css" href="../../../template/Charcoal_Grey/topicheader.css"/>
  <meta name="topic-status" content="Draft"/>

  <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  <title>for</title>
  <meta name="generator" content="Adobe RoboHelp 2019"/>
  <link rel="stylesheet" href="../../../assets/css/default.css" type="text/css"/>
  <meta name="rh-authors" content="Mark Alexander"/>
  <meta name="topic-comment" content="Page outlining the for function"/>
  <meta name="rh-index-keywords" content="for"/>
  <meta name="search-keywords" content="for"/>
</head>

<body>
  <div class="topic-header rh-hide" id="rh-topic-header" onclick="rh._.goToFullLayout()">
    <div class="logo">
    </div>
    <div class="nav">
      <div class="title" title="for">
        <span>for</span>
      </div>
      <div class="gotohome" title="Click here to see this page in full context">
        <span>Click here to see this page in full context</span>
      </div>
    </div>
  </div>
  <div class="topic-header-shadow rh-hide" id="rh-topic-header-shadow"></div>



  <!--<div class="body-scroll" style="top: 150px;">-->
  <h1>for</h1>
  <p>One of the most used ways to iterate over a <a class="tooltip" title="In programming, a statement is a single line of code written legally in a programming language that expresses an action to be carried out. A statement might have internal components of its own, including expressions, operators and functions. An example of a statement is A = B + 5. A GameMaker Studio 2 program is nothing but a sequence of one or more statements that together perform a task (like move the player). ">statement</a> (or statements) multiple times is to use a <tt>for</tt> loop, which has this form:</p>
  <p class="code">for (&lt;assignment&gt;; &lt;expression&gt;; &lt;operation&gt;;)<br/>     {<br/>     &lt;statement&gt;;<br/>     &lt;statement&gt;;<br/>     ...<br/>     }</p>
  <p>This works as follows - First the assignment is executed which assigns a value to a variable, then the <a class="tooltip" title="An expression is a combination of one or more constants, variables, operators, and/or functions that are interpreted according to particular rules of precedence and association to return another value. A simple expression would be (5 + 5), which returns 10.">expression</a>  is evaluated and, if it is <tt>true</tt>, the statements in the curly brackets{}
    are executed executed. Next the operation is performed on the assigned variable and then the expression is evaluated again. This loop will continue until the expression is found to be <tt>false</tt>.</p>
  <p>Now, this may sound complicated when written like that, but you should interpret it as:</p>
  <ol>
    <li>The first assignment initializes the for-loop</li>
    <li>The expression tests whether the loop should be ended</li>
    <li>The statement is performed</li>
    <li>The operation is performed</li>
    <li>Go back to step 2 and perform the expression again and continue or exist the loop</li>
  </ol>
  <p>This is extremely useful for doing repetitive tasks that would involve multiple lines of code in any other way, and is commonly used as a counter for evaluating arrays, drawing things, setting incremental values, etc... The following code example illustrates
    a typical use for this type of statement:</p>
  <p class="code">for (var i = 0; i &lt; 10; i += 1)<br/>     {
    <br/>     draw_text(32, 32 + (i * 32), string(i) + &quot;. &quot;+ string(scr[i]));<br/>     }
  </p>
  <p>The above code initialises a <span class="inline">for</span> loop, starting at 0 and counting up to (and including) 9, and then uses the loop value of <tt>i</tt> to draw the values stored in an array down the screen. Note how the <tt>for</tt> loop variable
    <tt>i</tt> is used to not only loop through the array, but to draw a number as well as tell GameMaker Studio 2 where to draw the values to in the room. This flexibility is one of the main reasons why <tt>for</tt> loops are so important in programming.</p>
  <p><strong>When should you use a <tt>for</tt> loop?</strong> Anytime you need to perform a fixed number of iterations over one or more statements while keeping track of the <a class="tooltip" title="An iteration is a single pass through a set of operations in your project code. One form of iteration in computer programming is via loops. A loop will repeat a certain segment of code until a condition is met and it can proceed further. Each time the computer runs a loop, it is known as an iteration. In simple terms, iteration is the process of repeating a particular snippet of code over and over again to perform a certain action.">iteration</a> that is
    currently being run and using that iteration value.</p>
  <p class="note"><b>NOTE</b>: You will see multiple examples when working with other people of the variables &quot;<tt>i</tt>&quot; and &quot;<tt>j</tt>&quot; being used for the loop counter variable. These are <b>not</b> obligatory variable names and you can use anything
    like &quot;<tt>a</tt>&quot; or &quot;<tt>foo</tt>&quot; or whatever. The use of &quot;<tt>i</tt>&quot; and &quot;<tt>j</tt>&quot; is simply a standard convention in programming.</p>
  <p>It is worth noting that you can use the special <span class="inline"><a href="break.htm">break</a></span> and <span class="inline"><a href="continue.htm">continue</a></span> statements within a <span class="inline">for</span> loop too. Using <span class="inline">break</span>    will immediately exit the loop and move on to any code that is in the event or function after the loop should have finished, eg:</p>
  <p class="code">var _inst = noone;<br/> for (var i = 0; i &lt; 10; i += 1)<br/>     {
    <br/>     _inst = instance_find(obj_Enemy_Parent, i);<br/>     if instance_exists(_inst)<br/>         {<br/>         if _inst.object_index == obj_Enemy_Melee<br/>             {<br/>             break;<br/>             }<br/>         }<br/>     }<br/>    target = _inst;</p>
  <p>The above code loops through the 10 neaerst instances of the given &quot;parent&quot; object, and if an instance is found it checks the instance object ID, and if it is an instance of <span class="inline">obj_Enemy_Melee</span> then the loop is ended
    using <span class="inline">break</span> and the ID value assigned to a variable (if no instance is found, then the keyword <span class="inline"><a href="../Instance_Keywords.htm">noone</a></span> will be added to the variable).</p>
  <p>An example of using <span class="inline">continue</span> in a <span class="inline">for</span> loop would be:</p>
  <p class="code">var _val = 0;<br/> for (var i = 0; i &lt; 10; i += 1)<br/>     {
    <br/>     if (val_array[i] &lt;= 0)<br/>         {<br/>         continue;<br/>         }<br/>     _val += val_array[i];<br/>     }
    <br/> draw_text(32, 32, &quot;Positive Values Total = &quot; + string(_val));</p>
  <p>This code will check the value stored in each array position of a 10 length 1D <a href="../Arrays.htm">array</a>, and if any are less than or equal to 0 it will <span class="inline">continue</span> the loop, meaning that the current iteration will end,
    <span class="inline">i</span> will be incremented, and the next loop iteration will be started. If the value is greater than 0, then it is added to the local variable <span class="inline">_val</span>, and after the loop is finished the total value
    is drawn to the screen.</p>
  <p>For more examples of loop keywords please see the sections on <a href="repeat.htm"><tt>repeat</tt></a>, <a href="while.htm"><tt>while</tt></a>, and <a href="do___until.htm"><tt>do / until</tt></a>.</p>
  <p> </p>
  <p> </p>
  <p> </p>
  <div class="footer">
    <div class="buttons">
      <div class="clear">
        <div style="float:left">Back: <a href="../Language_Features.htm">Language Features</a></div>
        <div style="float:right">Next: <a href="switch.htm">switch</a></div>
      </div>
    </div>
    <h5>© Copyright YoYo Games Ltd. 2020 All Rights Reserved</h5>
  </div>
  <!-- KEYWORDS
for
-->
  <!-- TAGS
for
-->

</body></html>