Skip to main content
Top

2018 | OriginalPaper | Chapter

5. Arrays and Loops

Authors : Ivor Horton, Peter Van Weert

Published in: Beginning C++17

Publisher: Apress

Activate our intelligent search to find suitable subject content or patents.

search-config
loading …

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.

Dont have a licence yet? Then find out more about our products and how to get one now:

Springer Professional "Wirtschaft+Technik"

Online-Abonnement

Mit Springer Professional "Wirtschaft+Technik" erhalten Sie Zugriff auf:

  • über 102.000 Bücher
  • über 537 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Maschinenbau + Werkstoffe
  • Versicherung + Risiko

Jetzt Wissensvorsprung sichern!

Springer Professional "Technik"

Online-Abonnement

Mit Springer Professional "Technik" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 390 Zeitschriften

aus folgenden Fachgebieten:

  • Automobil + Motoren
  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Elektrotechnik + Elektronik
  • Energie + Nachhaltigkeit
  • Maschinenbau + Werkstoffe




 

Jetzt Wissensvorsprung sichern!

Springer Professional "Wirtschaft"

Online-Abonnement

Mit Springer Professional "Wirtschaft" erhalten Sie Zugriff auf:

  • über 67.000 Bücher
  • über 340 Zeitschriften

aus folgenden Fachgebieten:

  • Bauwesen + Immobilien
  • Business IT + Informatik
  • Finance + Banking
  • Management + Führung
  • Marketing + Vertrieb
  • Versicherung + Risiko




Jetzt Wissensvorsprung sichern!

Footnotes
1
Without going too much into the specifics, statements such as count += height[i] < average; are sometimes produced by “clever” C++ programmers because they assume they will run faster than the original conditional statement if (height[i] < average) ++count; (because the latter contains a so-called branching statement). The fact is that any self-respecting compiler will already rewrite this code for you in a similar manner. Our advice is that it’s almost always best to leave the cleverness to the compiler; your job should be, in the first place, to produce code that is correct and clearly readable.
 
2
Technically, std::size() is primarily defined in the iterator header. But because it is such a commonly used utility, the Standard Library guarantees it to be available also when the array header is included (and with several other headers as well). Since you’ll mostly use std::size() with arrays, we believe it is easier to remember to include the array header instead of the iterator header.
 
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.
 
Metadata
Title
Arrays and Loops
Authors
Ivor Horton
Peter Van Weert
Copyright Year
2018
Publisher
Apress
DOI
https://doi.org/10.1007/978-1-4842-3366-5_5

Premium Partner