<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Loops &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/loops/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Mon, 22 Mar 2021 16:18:35 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>A Functional ForEach</title>
		<link>https://blog.xojo.com/2021/03/22/a-functional-foreach/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Mon, 22 Mar 2021 10:01:00 +0000</pubDate>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Declares]]></category>
		<category><![CDATA[Extension Methods]]></category>
		<category><![CDATA[Loops]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=8185</guid>

					<description><![CDATA[When looping through an array, I am a big fan of using For Each as I find it more readable than using a For loop with a counter and looking up the item in the array with the counter. Sometimes it’s fun to do stuff just because you can! Plus, it makes for a good excuse to learn about some more advanced Xojo features]]></description>
										<content:encoded><![CDATA[
<p>When looping through an array, I am a big fan of using <a href="https://documentation.xojo.com/api/code_execution/for_each...next.html">For Each</a> as I find it more readable than using a For loop with a counter and looking up the item in the array with the counter.</p>



<p>Such a loop might look like this:</p>



<pre class="wp-block-preformatted">Var xdcs() As String = Array("Orlando", "Austin", "Houston", _
"Las Vegas", "Denver", "Miami")

For Each city As String in xdcs
  Display(city)
Next</pre>



<p>The Xojo Developer Conference, aka XDC, is the biggest Xojo event of the year. We&#8217;re looking forward to <a href="https://www.xojo.com/xdc/">XDC London</a> in October 2021. </p>



<p>Here is the code for the Display method:</p>



<pre class="wp-block-preformatted">Public Sub Display(it As String)
  ListBox1.AddRow(it)
End Sub</pre>



<p>But what if you wanted to simplify things even further? Say you want to write code like this:</p>



<pre class="wp-block-preformatted">xdcs.ForEach(Display)</pre>



<p>Is that even possible in Xojo? Yes, it mostly is, with a couple things called <a href="https://documentation.xojo.com/api/language/extends.html">Extension Methods</a> and <a href="https://documentation.xojo.com/api/language/delegate.html">Delegates</a>.</p>



<p>To set this up, add a Module to your project and add a Delegate to the module. A Delegate is a type that is a method declaration. Here are its specifics:</p>



<ul class="wp-block-list"><li>Delegate Name: ForEachString</li><li>Parameters: it As String</li><li>Scope: Global</li></ul>



<p>Any actual method that matches this delegate signature is said to satisfy the delegate. If you look at the Display method above, you’ll see that it matches this delegate’s signature. So the Display method could be passed into a parameter of this delegate type.</p>



<p>In the same module, add a method. This will be an extension method on a string array. An extension method is a method that is called using dot notation on a specific data type (or class), even though it is not specifically part of the data type. This method also takes the delegate defined above as a parameter. Here are the method specifics:</p>



<ul class="wp-block-list"><li>Method Name: ForEach</li><li>Parameters: Extends stringArray() As String, action As ForEachString</li><li>Scope: Global</li></ul>



<p>Its code still uses For Each and calls the delegate like this:</p>



<pre class="wp-block-preformatted">For Each s As String In stringArray
  action.Invoke(s)
Next</pre>



<p>Note: The above method and delegate are designed to work with String arrays. To work with other array types you’d need to add equivalent delegates and ForEach method overloads.</p>



<p>With these pieces now in place, this code (which is almost exactly what we wanted above &#8212; <a href="https://documentation.xojo.com/api/language/addressof.html">AddressOf</a> is used to pass a reference of the Display method to the delegate) now works:</p>



<pre class="wp-block-preformatted">Var xdcs() As String = Array("Orlando", "Austin", "Houston", _
"Las Vegas", "Denver", "Miami")

xdcs.ForEach(AddressOf Display)</pre>



<p>The above code is slightly shorter since it doesn’t need to declare the variable, but was this really worth all this this effort just for that? Well, perhaps not, but <em>I</em> think it’s cool and sometimes it’s fun to do stuff just because you can! Plus, it makes for a good excuse to learn about some more advanced Xojo features. Either way, I hope you found this interesting.</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Code Tip: Be Careful With Loops</title>
		<link>https://blog.xojo.com/2018/03/14/code-tip-be-careful-with-loops/</link>
		
		<dc:creator><![CDATA[Norman Palardy]]></dc:creator>
		<pubDate>Wed, 14 Mar 2018 07:16:10 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[IR]]></category>
		<category><![CDATA[LLVM]]></category>
		<category><![CDATA[Loops]]></category>
		<category><![CDATA[Uint8]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=3979</guid>

					<description><![CDATA[Every once in a while we get a question or post like this one that wonders why a loop might turn into an infinite loop or why it doesn't behave as expected and stop when the loop reaches the limit. And this behaviour is not specifically a Xojo thing, most programming languages do this. In fact, there are some very well know encryption algorithms, like BlowFish, that rely on this behaviour.]]></description>
										<content:encoded><![CDATA[<p>Every once in a while we get a question or post like <a href="https://forum.xojo.com/conversation/post/377126">this one</a> that wonders why a loop might turn into an infinite loop or why it doesn&#8217;t behave as expected and stop when the loop reaches the limit.</p>
<p><span id="more-3979"></span></p>
<p>In this case the specific FOR NEXT loop in question is :</p>
<pre>For X As UInt8 = 0 To 255 
  Dim strAnything As String = "Hello World" 
Next
</pre>
<p>If you run this code you&#8217;ll find that it runs forever. You&#8217;ve accidentally created an infinite loop.</p>
<p>How did this come to be this way? There are two things involved. One is what range of values a Uint8 can actually store. And the second is the actual code generated for the for loop itself. When these two combine the stopping conditions will never occur.</p>
<p><a href="http://developer.xojo.com/integer-size-specific">Uint8 values</a> can hold a range of values like any integer. They are all positive. And the range is from 0 &#8211; 255.</p>
<p>So a Uint8 will NEVER hold a value &gt; 255 or &lt; 0.</p>
<p>Combine this with the fact that a FOR loop is generated using roughly this underlying code</p>
<pre>Dim loopCounter As UInt8
Dim loopLimit As Integer = 255
Dim loopBase As Integer = 254
Dim loopStep As Integer = 1

loopCounter = loopBase

loopTop:
  If loopCounter &gt; loopLimit Then
    GoTo loopEnd
  End If
  
  // loop body code would be here
  // its been elided to focus on the actual loop mechanics
  //    dim strAnything as string = "Hello World" 
  // 
  loopCounter = loopCounter + loopStep // NOTE THIS CAN CAUSE OVERFLOW !

  GoTo LoopTop

loopEnd:
</pre>
<p>This is NOT the actual code used. It is just a Xojo version of the actual underlying <a href="https://blog.xojo.com/2018/01/24/compilers-104-ir-generation/">LLVM IR</a> code that gets generated.</p>
<p>Hopefully this explains the confusion. Since a Uint8 cannot hold a value greater than 255, you will find that when loopCounter IS already 255 and the increment, or loopStep in this example, is added to the existing value the loopCounter goes NOT from 255 to 256 but back to 0. It wraps around. This isn&#8217;t a bug.</p>
<p>The addition of the loopStep value causes the Uint8 to overflow since the value 256, in binary, requires 9 bits, not 8 but the Uint8 can only hold 8 bits. And so when you add it the 8 bits the value <em>can</em> hold are all 0 and the loop counter resets to 0.</p>
<p>And this behavior is not specifically a Xojo thing, most programming languages do this. In fact, there are some very well know encryption algorithms, like <a href="https://en.wikipedia.org/wiki/Blowfish_(cipher)">BlowFish</a>, that rely on this  .</p>
<p>In the specific sample code that prompted this post, a loop counter that is an Integer or Uint16 is needed because they can hold values that exceed the loops upper bound.</p>
<p>Loops that count down have a similar problem. but counting down and underflowing instead of overflowing.</p>
<p>The moral of the story is to stick to using the general <a href="http://developer.xojo.com/integer">Integer data type</a> unless you have a specific need for a <a href="http://developer.xojo.com/integer-size-specific">size-specific Integer type</a>.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
