Swift for PowerBuilder Developers – Part III

Introduction

In this third installment of the series, we’ll compare and contrast the different types of Loop constructs available in both PowerScript and Swift.

If you want to start at the beginning, here are Part 1 and Part 2.

Overview

Loops are a critical part of every programming language, and all of them (at least those that I’ve worked with) do things a bit differently.

There are essentially two basic types of loops – the FOR loop and the DO loop.  They differ in one basic aspect:  the FOR loop is used when a block of code is to be executed over a specific number of iterations, and the DO loop executes a block of code until a specific condition is satisfied.  It’s really important to understand what you’re trying to achieve in the code before you select the loop syntax that you’ll be using. Trying to force everything into a FOR loop isn’t always the best approach…

 

FOR Loops

PowerBuilder

The basic syntax of the FOR loop in PowerBuilder is:

FOR varname = start TO end {STEP increment}
    statementblock
NEXT
  • You must declare the variable varname before you can reference it in the FOR loop.  Integer datatypes perform best, but any numeric datatype is acceptable. If you know the value of varname won’t exceed 256, use a smallint.
  • varname will be automatically incremented from start to end in increment increments. The STEP keyword is optional, and the default step size is 1, but you can use any positive or negative whole number.
  • If you need the loop to count backwards, decrementing varname from end to start, set the initial value of start to be less than end, and set the step increment to a negative number.
  • If the increment is positive, and start is greater than end at the beginning of the loop, then statementblock will NOT execute.  The same is true for negative increments – when end is less than start at the beginning of the loop.
  • The statement can also be terminated with END FOR, if you find that more readable.

Swift

In Swift, there are two variants of the FOR loop, and they operate very differently.

The loop that is most similar to PowerBuilder’s FOR loop as outlined above is actually called the FOR-IN loop.  That looks like this:

FOR varname in range-expression {
    statementblock
}
  • You don’t need to declare varname prior to its reference in the FOR statement.
  • You get automatic incrementing of varname loop counter, but that increment is always by 1.
  • You can use closed (inclusive) or half-open (exclusive) range expressions (which were covered in Part 2).

Here’s a simple example that prints out the first 10 multiples of the number 3.

for index in 1...10 {
    print( "3 times \(index) equals \(index * 3)" );
}

 

If you have no need to access the loop counter variable in the statement block, you don’t even need to define one!  Just use an underscore character where you would normally put the loop counter variable name.  The loop will execute the same number of iterations, you just won’t waste memory space on a loop counter.

Here’s a simple example that prints out the first 10

let base = 3;
let power = 10;
var answer = base;
for _ in 1...power  {
   answer *= base
}
print( "\(base) to the power of \(power) is \(answer) );

There’s a variant of the FOR-IN loop that automatically iterates over arrays and dictionaries (which we haven’t covered yet, but aren’t really that difficult).

Here’s an example of that style:

let primeNumbers = [2, 3, 5, 7, 11, 13, 17, 19];
for number in primeNumbers {
   print( "\(number) is a prime number") ;
}

Notice that there is no loop counter variable used or declared, and no range expression is defined.  There’s also no need to write code that checks for overflow of the array boundary.  The loop will stop automatically after the final entry in the array is processed.

The other variant of the FOR loop in Swift is actually more comparable to PowerBuilder’s WHILE loop, but uses the C-style syntax (except that parentheses around the initialization;condition;increment code are not required as they are in C/C++).

for initialization ; condition ; increment {
   statementblock;
}

And an example that does exactly the same thing as the FOR-IN loop above might look like this:

for var index = 1 ; index <= 10 ; index++ {
   print( "3 times \(index) equals \(index * 3)" );
}

 

DO WHILE/UNTIL loops

A FOR loop is best when you have a static range upon which the loop will iterate – either a bounded array, or a defined range expression.  When you need a statement block to execute indefinitely until a specific condition is satisfied, use one of the DO loop constructs.  Let’s start with the PowerBuilder options for these.

PowerBuilder

In PB, there are four variants of the DO loop, and the differences between them are very subtle.  They differ based on the terminating condition being TRUE or FALSE, and whether the condition is evaluated at the start or end of each iteration.

Syntax Description
DO UNTIL condition
    statement block
LOOP
This version evaluates the condition at the start of each iteration. If the condition is initially TRUE, the statement block will not execute.  Otherwise, the loop will iterate UNTIL the specified condition becomes TRUE.
DO
    statement block
LOOP UNTIL condition
This version evaluates the condition at the end of each iteration, so the statement block will execute at least once. The loop will iterate UNTIL the specified condition becomes TRUE.
DO WHILE condition
    statement block
LOOP
This version evaluates the condition at the start of each iteration.  If the condition is FALSE on the first iteration, the statement block will not execute.  Otherwise, the loop will iterate WHILE the condition remains TRUE.
DO
    statement block
LOOP WHILE condition
This version evaluates the condition at the end of each iteration, so the statement block will execute at least once. The loop will iterate WHILE the specified condition remains TRUE.

Swift

Swift only has two variants of the DO loop, a WHILE and a REPEAT statement.  The syntax for these is very straightforward:

Syntax Description
while condition {
    statementBlock
}
This version evaluates the condition at the start of each iteration. If the condition is initially FALSE, the statementBlock does not execute at all.  Otherwise, the loop iterates WHILE the condition remains TRUE.

This functions exactly the same as PB’s syntax #3 shown above.

repeat {
    statementBlock
} while condition
This version evaluates the condition at the end of each iteration. The statementBlock will be executed at least once, even if the condition is initially FALSE. The loop will iterate WHILE the specified condition remains TRUE.

This functions exactly the same as PB’s syntax #4 above.

Conclusion

I’ll cover the basics of the three primary Collection types in the next post.  In Swift, these are arrays, sets, and dictionaries.

-Paul-

Advertisements

About Paul Horan

Software Technology - lots of experience with Sybase and IBM mobility/cloud offerings.
This entry was posted in Software, Swift and tagged , , , . Bookmark the permalink.