Difference between revisions of "W1154 For Loop"

From Coder Merlin
Line 66: Line 66:
** An '''empty interval''' has no elements
** An '''empty interval''' has no elements
** A '''degenerate interval''' has a single element
** A '''degenerate interval''' has a single element
* Swift defines two range types:
** A '''ClosedRange''' includes both the lower bound and the upper bound and is written using ...
** A '''Range''' include the lower bound but excludes the upper bound and is written using ..<
}}
}}



Revision as of 22:01, 16 January 2020

Within these castle walls be forged Mavens of Computer Science ...
— Merlin, The Coder
Roller Coaster Loop

Prerequisites[edit]

Introduction[edit]

The for loop is a construct which enables us to execute a segment of code a defined number of times. It is thus the most appropriate choice when we know in advance how to calculate the number of iterations prior to beginning the loop. While this construct is not strictly necessary, it provides syntactic sugar by clarifying intent.

For Loops[edit]

for loops execute for a defined count of iterations, as such, the test condition is implied within the definition. Through each iteration, a loop control variable is adjusted toward its final goal. When that goal is reached, the loop exits.

Both while and repeat-while loops require the explicit definition of a loop control variable which must be explicitly incremented (or otherwise adjusted) and explicitly checked each time through the loop to determine if the loop has completed. In contrast, a for loop handles this work for us implicitly by defining the loop control variable, incrementing (or otherwise adjusting) it automatically, and checking for the completion condition.

The actual implementation is identical to that of a while loop.

Intervals[edit]

Assume two numbers, a and b, such that b > a. An interval is the set of numbers lying between these two numbers. Both a and b are the endpoints of the interval, the lower bound and the upper bound respectively. An open interval does not include either of the endpoints, a half-open interval include just one of its endpoints, while a closed interval includes both of its endpoints.

Interval Notation[edit]

Notation Interpretation Description
(a, b) a < x < b An open interval; both endpoints are excluded.
(a, b] a < x ≤ b A half-open interval; the lower bound is excluded.
[a, b) a ≤ x < b A half-open interval; the upper bound is excluded.
[a, b] a ≤ x ≤ b A closed interval; both endpoints are included.

Interval Classifications[edit]

An empty interval has no elements. Examples include:

  • (b, a), (b, a], [b, a), [b, a]
  • (a, a), (a, a], [a, a)

A degenerate interval has a single element. An example is:

  • [a, a]

Swift Ranges[edit]

Swift supports two range types

ClosedRange[edit]

A ClosedRange includes both the lower bound and the upper bound. It's written using ... between the lower and upper bounds. For example, the equivalent of [4, 7] is:

4 ... 7

Range[edit]

A Range includes the lower bound but excludes the upper bound. It's written using ..< between the lower and upper bounds. For example, the equivalent of [4, 7) is:

4 ..< 7

Key Concepts[edit]

Key ConceptsKeyConceptsIcon.png
  • The for loop is a construct which enables us to execute a segment of code a defined number of times.
    • Both while and repeat-while loops require the explicit definition of a loop control variable which must be explicitly adjusted and explicitly checked each time through the loop. In contrast, a for loop handles this work implicitly.
    • The actual implementation is identical to that of a while loop.
  • Intervals: Assume two numbers, a and b, such that b > a. An interval is the set of numbers lying between these two numbers.
    • Both a and b are the endpoints of the interval, the lower bound and the upper bound respectively.
    • An open interval does not include either of the endpoints
    • A half-open interval include just one of its endpoints
    • A closed interval includes both of its endpoints
    • An empty interval has no elements
    • A degenerate interval has a single element
  • Swift defines two range types:
    • A ClosedRange includes both the lower bound and the upper bound and is written using ...
    • A Range include the lower bound but excludes the upper bound and is written using ..<

Exercises[edit]

References[edit]