<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://avendar.net/index.php?action=history&amp;feed=atom&amp;title=Avendar%3AProg_Section_3</id>
	<title>Avendar:Prog Section 3 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://avendar.net/index.php?action=history&amp;feed=atom&amp;title=Avendar%3AProg_Section_3"/>
	<link rel="alternate" type="text/html" href="https://avendar.net/index.php?title=Avendar:Prog_Section_3&amp;action=history"/>
	<updated>2026-04-17T23:40:57Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.41.4</generator>
	<entry>
		<id>https://avendar.net/index.php?title=Avendar:Prog_Section_3&amp;diff=2670&amp;oldid=prev</id>
		<title>Elanthe: Created page with &quot;=Control Structures=  &lt;i&gt;&quot;We offered them ORDER!&quot;&lt;/i&gt;      -- Khan, Star Trek: Episode 24: Space Seed  : As we have seen, control structures allow us to alter the normally linear order  : in which program commands are executed.  ==If Checks== :  The most common type of control structure is the if check, which tests to see if  :  a certain aspect of the game world is true or false. An if check alters the flow of :  the program so that if it is true, a block of commands fo...&quot;</title>
		<link rel="alternate" type="text/html" href="https://avendar.net/index.php?title=Avendar:Prog_Section_3&amp;diff=2670&amp;oldid=prev"/>
		<updated>2025-01-19T03:07:49Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;=Control Structures=  &amp;lt;i&amp;gt;&amp;quot;We offered them ORDER!&amp;quot;&amp;lt;/i&amp;gt;      -- Khan, Star Trek: Episode 24: Space Seed  : As we have seen, control structures allow us to alter the normally linear order  : in which program commands are executed.  ==If Checks== :  The most common type of control structure is the if check, which tests to see if  :  a certain aspect of the game world is true or false. An if check alters the flow of :  the program so that if it is true, a block of commands fo...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;=Control Structures=&lt;br /&gt;
 &amp;lt;i&amp;gt;&amp;quot;We offered them ORDER!&amp;quot;&amp;lt;/i&amp;gt; &lt;br /&gt;
    -- Khan, Star Trek: Episode 24: Space Seed&lt;br /&gt;
&lt;br /&gt;
: As we have seen, control structures allow us to alter the normally linear order &lt;br /&gt;
: in which program commands are executed.&lt;br /&gt;
&lt;br /&gt;
==If Checks==&lt;br /&gt;
:  The most common type of control structure is the if check, which tests to see if &lt;br /&gt;
:  a certain aspect of the game world is true or false. An if check alters the flow of&lt;br /&gt;
:  the program so that if it is true, a block of commands following the check will be &lt;br /&gt;
:  executed. If it is false, these commands will not be executed, and execution of the &lt;br /&gt;
:  program will skip past them. Every if statement appears before a block of commands, &lt;br /&gt;
:  and must always have a matching endif statement, to mark the end of this block of &lt;br /&gt;
:  commands. The syntax of an if check is generally:&lt;br /&gt;
&lt;br /&gt;
:    if check([&amp;lt;argument1&amp;gt;]) &amp;lt;logical operator&amp;gt; &amp;lt;argument2&amp;gt;&lt;br /&gt;
:    [block of commands]&lt;br /&gt;
:    endif&lt;br /&gt;
&lt;br /&gt;
:  The check is the actual if check. The check is really a function, which tests an &lt;br /&gt;
:  aspect of something in the game world. This can be something as simple as whether &lt;br /&gt;
:  or not a target is a pc, or as complex as a personâ€™s percentage in a particular &lt;br /&gt;
:  skill or spell. The argument 1 is the pc, mobile, object, number, or other entity &lt;br /&gt;
:  which is being checked. Generally, the target is a variable. It can, however, be a &lt;br /&gt;
:  number, or the literal name of the pc or npc in question. A check can also have a &lt;br /&gt;
:  blank argument, in the case of checks which do not require a target. The logical &lt;br /&gt;
:  operator expresses a logical relationship between the argument1 and the argument2 &lt;br /&gt;
:  of the if check. The operator can either be numerical (if either side resolves to &lt;br /&gt;
:  a number), or a string operator (if either side resolves to a string.)&lt;br /&gt;
&lt;br /&gt;
:  The following are numerical operators:&lt;br /&gt;
&lt;br /&gt;
    == equality &lt;br /&gt;
    != negation of equality (i.e., &amp;quot;is not equal to&amp;quot;) &lt;br /&gt;
    &amp;lt; less than &lt;br /&gt;
    &amp;lt;= less than or equal to &lt;br /&gt;
    &amp;gt; greater than &lt;br /&gt;
    &amp;gt;= greater than or equal to &lt;br /&gt;
    &amp;amp; Bitwise and &lt;br /&gt;
    | Bitwise or &lt;br /&gt;
&lt;br /&gt;
:  The following are string operators:&lt;br /&gt;
&lt;br /&gt;
    == Exact equality &lt;br /&gt;
    != Strings do not match &lt;br /&gt;
    / String contains the given text &lt;br /&gt;
    !/ String does not contain the given text &lt;br /&gt;
&lt;br /&gt;
:  As can be seen, not every logical operator works for every if check. In &lt;br /&gt;
:  general, if the result of a checkâ€™s evaluation is not numerical, only the first&lt;br /&gt;
:  two operators can be used (as these are the only ones which do not require a &lt;br /&gt;
:  number to appear to either side.)&lt;br /&gt;
&lt;br /&gt;
:  The argument2 is an argument which is compared to the result of the check. Generally&lt;br /&gt;
:  speaking, this can be a variable that represents a pc, mobile, object, a number, or&lt;br /&gt;
:  some other form of data. So, putting this all together, letâ€™s consider a few examples&lt;br /&gt;
:  of some if checks.&lt;br /&gt;
&lt;br /&gt;
    greet_prog 100&lt;br /&gt;
    if ispc($n)&lt;br /&gt;
    say Hello, $N. &lt;br /&gt;
    endif&lt;br /&gt;
    say $N just entered the room.&lt;br /&gt;
&lt;br /&gt;
:  This is a simple program, that will greet a pc if they enter the room, then let everyone&lt;br /&gt;
:  know the name of anything that enters a room, even if itâ€™s not a pc. So, in this prog, &lt;br /&gt;
:  we can see that:&lt;br /&gt;
:   - The check is â€˜ispcâ€™&lt;br /&gt;
:   - The argument1 is â€˜$nâ€™&lt;br /&gt;
:   - There is no logical operator.&lt;br /&gt;
:   - There is no argument2.&lt;br /&gt;
:   - The if block is one line: &amp;quot;say Hello, $N&amp;quot;.&lt;br /&gt;
:   - The endif comes right after that.&lt;br /&gt;
&lt;br /&gt;
:  Remember, the only required thing for an if check is the if, the check, and the endif.&lt;br /&gt;
&lt;br /&gt;
:  Now, letâ€™s suppose Jolinn, and his (Charmed) Ethron Love NPC were to enter a room, and&lt;br /&gt;
:  this prog is on a hen. (Yes, a hen.) Someone standing in the room would see:&lt;br /&gt;
&lt;br /&gt;
:    The hen says, â€˜Hello, Jolinnâ€™&lt;br /&gt;
:    The hen says, â€˜Jolinn just entered the room.â€™&lt;br /&gt;
:    The hen says, â€˜The hawt ethron love slave has just entered the room.â€™&lt;br /&gt;
&lt;br /&gt;
:  As we can see, since the ethron love slave is not a pc, the commands in the if block &lt;br /&gt;
:  are not executed.&lt;br /&gt;
&lt;br /&gt;
:  Now, what have if checks really allowed us to do? Basically, we can now say, &amp;quot;If a &lt;br /&gt;
:  condition is TRUE, then execute a specified list of commands.&amp;quot; But, what if we want to&lt;br /&gt;
:  say, &amp;quot;If a condition is TRUE, then execute a specified set of commands. However, if itâ€™s&lt;br /&gt;
:  FALSE, execute a different set of commands&amp;quot;? To help us with this scenario, we have the &lt;br /&gt;
:  else command. This command allows you to specify alternate set of commands in a body of&lt;br /&gt;
:  an if check, to be executed in the event that the check does not return a true result. &lt;br /&gt;
:  To see how else works, letâ€™s return to our example.&lt;br /&gt;
&lt;br /&gt;
    greet_prog 100&lt;br /&gt;
    if ispc($n)&lt;br /&gt;
    say Hello, $N. &lt;br /&gt;
    else&lt;br /&gt;
    say I would say hello, $N, but youâ€™re just a mobile.&lt;br /&gt;
    endif&lt;br /&gt;
    say $N just entered the room.&lt;br /&gt;
&lt;br /&gt;
:  In this case, if Jolinn and his ethron entered the room, they would see:&lt;br /&gt;
&lt;br /&gt;
:    The hen says, â€˜Hello, Jolinn.â€™&lt;br /&gt;
:    The hen says, â€˜Jolinn just entered the room.â€™&lt;br /&gt;
:    The hen says, â€˜I would say hello, a hawt ethron love slave, but youâ€™re just a mobile.â€™&lt;br /&gt;
:    The hen says, â€˜A hawt ethron love slave just entered the room.â€™&lt;br /&gt;
&lt;br /&gt;
:  As we can see, the commands after the else statement are executed for the ethron, since&lt;br /&gt;
:  the ethron is NOT a pc, and therefore the else block has its triggered. &lt;br /&gt;
&lt;br /&gt;
:  Now, suppose that we have an effect which we want to occur under multiple conditions. An&lt;br /&gt;
:  example might be: A mob who will speak either to an aelin OR a nefortu when given a sum &lt;br /&gt;
:  of a gold. Now, we can easily do this by having two (or more) if check blocks in a row,&lt;br /&gt;
:  but this requires us to unnecessarily duplicate commands. To this end, we introduce the&lt;br /&gt;
:  or statement, which allows us to have an if-block that executes on any of multiple conditions&lt;br /&gt;
:  being satisfied. &lt;br /&gt;
:  Consider the following example:&lt;br /&gt;
&lt;br /&gt;
    speech_prog p Hello&lt;br /&gt;
    if race($n) == aelin&lt;br /&gt;
    or race($n) == nefortu&lt;br /&gt;
    say Why hello to you too, $N.&lt;br /&gt;
    else&lt;br /&gt;
    say Hmph. Whatever, wingless person! You are dead to me!&lt;br /&gt;
    endif&lt;br /&gt;
&lt;br /&gt;
:  So, if:&lt;br /&gt;
:  An aelin said &amp;quot;Hello&amp;quot;, they would see: â€˜Why hello to you too, aelin.â€™&lt;br /&gt;
:  A nefortu said &amp;quot;Hello&amp;quot;, they would see â€˜Why hello to you too, nefortu.â€™&lt;br /&gt;
:  An ethron said &amp;quot;Hello&amp;quot;, they would see â€˜Hmph. Whatever, wingless person! You are dead to me!â€™&lt;br /&gt;
&lt;br /&gt;
:  So, the or check allows multiple possible conditions to trigger the same if block. &lt;br /&gt;
&lt;br /&gt;
:  Now, what if we want to execute a block of commands when two (or more) simultaneous &lt;br /&gt;
:  conditions hold true at once. For example, suppose we want a mob that attacks a target&lt;br /&gt;
:  if it both a pc AND an ethron. Or, suppose we have a sword that requires you be good &lt;br /&gt;
:  AND lawful to wield. We can even imagine situations where we might want three, four,&lt;br /&gt;
:  five, or even an arbitrary number of conditions to hold true.&lt;br /&gt;
&lt;br /&gt;
:  To support this functionality, progs support the &amp;quot;nesting&amp;quot; of if check. &amp;quot;Nesting&amp;quot; means&lt;br /&gt;
:  that you can put one if check (including its body, else statement, endif) inside of another &lt;br /&gt;
:  if check. So, in essence, we would have:&lt;br /&gt;
&lt;br /&gt;
    if check1(var)&lt;br /&gt;
        if check2(var)&lt;br /&gt;
        command&lt;br /&gt;
        command&lt;br /&gt;
        command&lt;br /&gt;
        else (optional)&lt;br /&gt;
        command&lt;br /&gt;
        endif&lt;br /&gt;
    else (optional)&lt;br /&gt;
    endif&lt;br /&gt;
&lt;br /&gt;
:  Essentially, the if block of check2 is a subset of the if block of check1. The commands&lt;br /&gt;
:  in check2â€™s block are only executed if both check1 and check2 are true. To illustrate how &lt;br /&gt;
:  this works, letâ€™s consider a concrete example. Suppose that this prog appeared on a hen:&lt;br /&gt;
&lt;br /&gt;
    prog add speech_prog p Hello&lt;br /&gt;
    if ispc($n)&lt;br /&gt;
      if race($n) == ethron&lt;br /&gt;
      say Why hello there, $N, you beautiful green thing, you!&lt;br /&gt;
      else&lt;br /&gt;
      say Why hello there, $N.&lt;br /&gt;
      endif&lt;br /&gt;
    else&lt;br /&gt;
    say Youâ€™re nothing more than a drone, $N!&lt;br /&gt;
    endif&lt;br /&gt;
&lt;br /&gt;
:  Letâ€™s suppose we have Jolinn, who is a human, Aeolis, who is an ethron, and a hot ethron&lt;br /&gt;
:  love slave, who is an npc who is an ethron. &lt;br /&gt;
&lt;br /&gt;
:  If... &lt;br /&gt;
:  Jolinn says, &amp;quot;Hello&amp;quot;, he would see: &lt;br /&gt;
&lt;br /&gt;
:    The hen says, &amp;quot;Why hello there, Jolinn.&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:  Aeolis says, &amp;quot;Hello&amp;quot;, he would see: &lt;br /&gt;
&lt;br /&gt;
:    The hen says, &amp;quot;Why hello there, Aeolis, you beautiful green thing, you!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:  A hawt ethron love slave says, &amp;quot;Hello&amp;quot;, she would see: &lt;br /&gt;
&lt;br /&gt;
:    The hen says, &amp;quot;Youâ€™re nothing more than a drone, hawt ethron love slave!&amp;quot;&lt;br /&gt;
&lt;br /&gt;
:  As we can see, &amp;quot;nesting&amp;quot; results in a block of commands which is executed only if BOTH&lt;br /&gt;
:  checks are true. This is perfectly equivalent to the logical AND, &amp;quot;If A AND B are true,&lt;br /&gt;
:  then do C&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Loops==&lt;br /&gt;
&lt;br /&gt;
:  Often times, we may wish to repeat a command a certain number of times in a program. &lt;br /&gt;
:  Now, clearly, we could do this just by including that number of copies of the command,&lt;br /&gt;
:  but this solution leaves certain things to be desired. Firstly, it is inefficient &lt;br /&gt;
:  (what if we want to execute a command a hundred times?). Secondly, and most importantly,&lt;br /&gt;
:  it does not allow us to execute a command a variable number of times. &lt;br /&gt;
&lt;br /&gt;
:  Loop Syntax:&lt;br /&gt;
 &lt;br /&gt;
    Loop &amp;lt;low number&amp;gt; to &amp;lt;high number&amp;gt;&lt;br /&gt;
    command_1&lt;br /&gt;
    command_2&lt;br /&gt;
    .&lt;br /&gt;
    .&lt;br /&gt;
    command_n&lt;br /&gt;
    endloop&lt;br /&gt;
&lt;br /&gt;
:  This will execute the commands in the loop (1-n), a number of times equal to high &lt;br /&gt;
:  number - low number. In general, low number and high number are both numbers, or variables &lt;br /&gt;
:  whose values resolve to being numbers. Just like if checks, you can &amp;quot;nest&amp;quot; loops, by placing&lt;br /&gt;
:  one loop block inside of another one.&lt;br /&gt;
&lt;br /&gt;
:  In a loop, you can reference the &amp;quot;iteration&amp;quot;, or number the loop is on, by the $v variable,&lt;br /&gt;
:  adjoined to a number. The number represents the &amp;quot;depth&amp;quot; of the loop you are currently in. &lt;br /&gt;
:  So, by default, $v1 references the outermost loopâ€™s current iteration. $v2 references the&lt;br /&gt;
:  next innermost loop iteration, and so on, until $vn, where n is the innermost loop iteration.&lt;br /&gt;
&lt;br /&gt;
:  This can be a little confusing, so letâ€™s give a concrete example. Suppose that our favorite&lt;br /&gt;
:  counting hen has a prog that reads:&lt;br /&gt;
&lt;br /&gt;
    prog add speech_prog p Loopy&lt;br /&gt;
    loop 1 to 3&lt;br /&gt;
      say The outermost loop is on number $v1.&lt;br /&gt;
      loop 1 to 2&lt;br /&gt;
        say The second loop check is on number $v2.&lt;br /&gt;
        loop 7 to 8&lt;br /&gt;
          say The innermost loop check is on number $v3.&lt;br /&gt;
        endloop&lt;br /&gt;
      endloop&lt;br /&gt;
    endloop&lt;br /&gt;
    say The loop now, is ended.&lt;br /&gt;
&lt;br /&gt;
:  So, if you were to say &amp;quot;Loopy&amp;quot; to the hen, you would see the following:&lt;br /&gt;
&lt;br /&gt;
    The hen says, â€˜The outermost loop is on number 1.â€™ &lt;br /&gt;
    The hen says, â€˜The second loop check is on number 1.â€™ &lt;br /&gt;
    The hen says, â€˜The innermost loop check is on number 7.â€™ &lt;br /&gt;
    The hen says, â€˜The innermost loop check is on number 8.â€™ &lt;br /&gt;
    The hen says, â€˜The second loop check is on number 2.â€™ &lt;br /&gt;
    The hen says, â€˜The innermost loop check is on number 7.â€™&lt;br /&gt;
    The hen says, â€˜The innermost loop check is on number 8.â€™&lt;br /&gt;
    The hen says, â€˜The outermost loop is on number 2.â€™&lt;br /&gt;
    The hen says, â€˜The second loop check is on number 1.â€™&lt;br /&gt;
    The hen says, â€˜The innermost loop check is on number 7.â€™&lt;br /&gt;
    The hen says, â€˜The innermost loop check is on number 8.â€™&lt;br /&gt;
    The hen says, â€˜The second loop check is on number 2.â€™&lt;br /&gt;
    The hen says, â€˜The innermost loop check is on number 7.â€™&lt;br /&gt;
    The hen says, â€˜The innermost loop check is on number 8.â€™&lt;br /&gt;
    The hen says, â€˜The outermost loop is on number 3.â€™&lt;br /&gt;
    The hen says, â€˜The second loop check is on number 1.â€™&lt;br /&gt;
    The hen says, â€˜The innermost loop check is on number 7.â€™&lt;br /&gt;
    The hen says, â€˜The innermost loop check is on number 8.â€™&lt;br /&gt;
    The hen says, â€˜The second loop check is on number 2.â€™&lt;br /&gt;
    The hen says, â€˜The innermost loop check is on number 7.â€™&lt;br /&gt;
    The hen says, â€˜The innermost loop check is on number 8.â€™&lt;br /&gt;
    The hen says, â€˜The loop now, is ended.â€™&lt;br /&gt;
&lt;br /&gt;
:  As we can see, loops are interpreted from the &amp;quot;outside in&amp;quot;. Begin with the outermost &lt;br /&gt;
:  loop, then execute its contents. If those contents contain a loop statement, execute&lt;br /&gt;
:  that loop. Continue until the innermost loop is reached.&lt;br /&gt;
&lt;br /&gt;
:  And, as we can see, $v1, $v2, and $v3 are variables which refer to iteration of the &lt;br /&gt;
:  corresponding loop.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Break==&lt;br /&gt;
&lt;br /&gt;
:  Finally, it is sometimes necessary to terminate execution of a prog entirely. In this&lt;br /&gt;
:  instance, the break command accomplishes this effect. The moment this command is executed,&lt;br /&gt;
:  the prog will stop. This is particularly useful for terminating out of certain branches &lt;br /&gt;
:  of if checks.&lt;/div&gt;</summary>
		<author><name>Elanthe</name></author>
	</entry>
</feed>