Skip to main content

Arrays and Loops

  • Chapter
  • First Online:
Beginning C++20
  • 3131 Accesses

Abstract

An array enables you to work with several data items of the same type using a single name, the array name. The need for this occurs often—when working with a series of temperatures or the ages of a group of people, for example. A loop is another fundamental programming facility. It provides a mechanism for repeating one or more statements as many times as your application requires. Loops are essential in the majority of programs. Using a computer to calculate the company payroll, for example, would not be practicable without a loop. There are several kinds of loop, each with their own particular area of application.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 54.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

     Without going too much into the specifics, “clever” C++ coders concoct statements such as count += height[i] < average; because they assume they will run faster than the original conditional statement, if (height[i] < average) ++count;. The reason is that the latter contains an obvious so-called branching statement. And branching statements in general slow down execution. Any self-respecting compiler, however, will already rewrite this if statement for you in a similar manner. Our advice is to leave the cleverness to the compiler, and to always strive for clear and correct code instead.

  2. 2.

     Technically, std::size() is primarily defined in the <iterator> module. But because it is such a commonly used utility, the Standard Library guarantees it to be available also when the <array> module is imported (and with several other modules as well). Since you’ll mostly use std::size() with arrays, we believe it is easier to remember to import the <array> module instead of the <iterator> module.

  3. 3.

    If the for loop’s body contains continue statements (covered later in this chapter), you’ll need some additional work rewriting the loop to a while loop. Concretely, you’ll have to ensure a copy of the iteration code is added prior to every continue statement.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2020 Ivor Horton and Peter Van Weert

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Horton, I., Van Weert, P. (2020). Arrays and Loops. In: Beginning C++20. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-5884-2_5

Download citation

Publish with us

Policies and ethics