Reader small image

You're reading from  iOS 17 Programming for Beginners - Eighth Edition

Product typeBook
Published inOct 2023
Reading LevelBeginner
PublisherPackt
ISBN-139781837630561
Edition8th Edition
Languages
Tools
Right arrow
Author (1)
Ahmad Sahar
Ahmad Sahar
author image
Ahmad Sahar

Ahmad Sahar is a trainer, presenter, and consultant at Tomafuwi Productions, specializing in conducting training courses for macOS and iOS, macOS Support Essentials certification courses, and iOS Development courses. He is a member of the DevCon iOS and MyCocoaHeads online communities in Malaysia and has conducted presentations and talks for both groups. In his spare time, he likes building and programming LEGO Mindstorms robots.
Read more about Ahmad Sahar

Right arrow

Range Operators and Loops

In the previous chapter, you looked at conditionals, which allow you to do different things based on different conditions, and optionals, which enable you to create variables that may or may not have a value.

In this chapter, you will learn about range operators and loops. Range operators allow you to represent a range of values by specifying the start and end values for a range, and you’ll learn about the different types of range operators. Loops allow you to repeat an instruction or a sequence of instructions over and over. You can repeat a sequence a fixed number of times or repeat a sequence until a condition is met. You’ll learn about the different types of loops used to accomplish this.

By the end of this chapter, you’ll have learned how to use ranges and how to create and use the different types of loops (for-in, while, and repeat-while).

The following topics will be covered in this chapter:

  • Exploring range...

Technical requirements

The Xcode playground for this chapter is in the Chapter04 folder of the code bundle for this book, which can be downloaded here:

https://github.com/PacktPublishing/iOS-17-Programming-for-Beginners-Eighth-Edition.

Check out the following video to see the code in action:

https://youtu.be/OinSB6sPByU

If you wish to start from scratch, create a new playground and name it RangeOperatorsAndLoops.

You can type in and run all the code in this chapter as you go along. Let’s start with specifying a range of numbers using range operators.

Exploring range operators

Imagine you need to write a program for a department store that automatically sends a discount voucher to customers between the ages of 18 and 30. It would be very cumbersome if you needed to set up an if or switch statement for each age. It’s much more convenient to use a range operator in this case.

Range operators allow you to represent a range of values. Let’s say you want to represent a sequence of numbers starting with firstNumber and ending with lastNumber. You don’t need to specify every value; you can just specify the range in this way:

firstNumber...lastNumber

Let’s try this out in the playground. Follow these steps:

  1. Add the following code to your playground and click the Run button:
    let myRange = 10...20
    

    This will assign a number...

Exploring loops

In programming, you frequently need to do the same thing repeatedly. For example, each month, a company will need to generate payroll slips for each employee. If the company has 10,000 employees, it would be inefficient to write 10,000 instructions to create the payroll slips. Repeating a single instruction 10,000 times would be better, and loops are used for this.

There are three loop types: the for-in loop, the while loop, and the repeat-while loop. The for-in loop will repeat for a known number of times, and the while and repeat-while loops will repeat if the loop condition is true.

Let’s look at each type in turn, starting with the for-in loop, which is used when you know how many times a loop should be repeated.

The for-in loop

The for-in loop steps through every value in a sequence, and a set of...

Join our book community on Discord

https://packt.link/iosdevelopment

Qr code Description automatically generated

In the previous chapter, you looked at conditionals, which allow you to do different things based on different conditions, and optionals, which enable you to create variables that may or may not have a value.In this chapter, you will learn about range operators and loops. Range operators allow you to represent a range of values by specifying the start and end values for a range, and you'll learn about the different types of range operators. Loops allow you to repeat an instruction or a sequence of instructions over and over. You can repeat a sequence a fixed number of times or repeat a sequence until a condition is met. You'll learn about the different types of loops used to accomplish this.By the end of this chapter, you'll have learned how to use ranges and how to create and use the different types of loops (for-in, while, and repeat-while).The following topics will be covered in this chapter:

  • Exploring...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
iOS 17 Programming for Beginners - Eighth Edition
Published in: Oct 2023Publisher: PacktISBN-13: 9781837630561
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime

Author (1)

author image
Ahmad Sahar

Ahmad Sahar is a trainer, presenter, and consultant at Tomafuwi Productions, specializing in conducting training courses for macOS and iOS, macOS Support Essentials certification courses, and iOS Development courses. He is a member of the DevCon iOS and MyCocoaHeads online communities in Malaysia and has conducted presentations and talks for both groups. In his spare time, he likes building and programming LEGO Mindstorms robots.
Read more about Ahmad Sahar