(2) cpp imperative programming

of 34 /34
Nico Ludwig (@ersatzteilchen) (2) Basics of the C++ Programming Language

Upload: nico-ludwig

Post on 11-Jul-2015

104 views

Category:

Technology


1 download

Embed Size (px)

TRANSCRIPT

Page 1: (2) cpp imperative programming

Nico Ludwig (@ersatzteilchen)

(2) Basics of the C++ Programming Language

Page 2: (2) cpp imperative programming

2

TOC● (2) Basics of the C++ Programming Language

– Imperative Programming

– Style and Conventions

– Constants

– Fundamental Types

– Console Basics

– Operators, Precedence, Associativity and Evaluation Order

– Control Structures and Blocks

● Sources:– Bruce Eckel, Thinking in C++ Vol I

– Bjarne Stroustrup, The C++ Programming Language

Page 3: (2) cpp imperative programming

3

Code Snippets● Hence we'll begin using code snippets (or simply called "snippets") as examples, i.e. no surrounding main() function etc.!

● If a snippet produces command line output, it will occasionally be shown in the code with the "// >"-notation in green color.

– In C++ a line starting with two slashes "//" is a C++-comment. We'll discuss those in one of the upcoming slides.

● We use colors in the snippets to highlight elements in the code: brown for text, blue for keywords and green for comments.

● Sometimes the #include directives will be left away, esp. if we can anticipate their inclusion from the code.

● In the end no fully runnable program code, but snippets will be shown in upcoming lectures!

// Main.cpp#include <iostream>

void main(){

std::cout<<"Hello World!"<<std::endl;}

#include <iostream>std::cout<<"Hello World!"<<std::endl;

#include <iostream>std::cout<<"Hello World!"<<std::endl;// >Hello World!

#include <iostream>std::cout<<"Hello World!"<<std::endl;

std::cout<<"Hello World!"<<std::endl;// >Hello World!

#include <iostream>std::cout<<"Hello World!"<<std::endl;// >Hello World!

Page 4: (2) cpp imperative programming

4

C++ Syntax Cornerstones – Part I – Imperative Elements● We'll begin by discussing imperative programming in C++.

– Imperative programming: writing programs with statements being executed sequentially to change the state of the program.

● Imperative programming includes following elements in general:– Variables (hold the state of the program)

– Statements (executable instructions, which typically change the state (i.e. the contents of variables) of the program)

– Branches (execute statements depending an a condition)

– Loops (execute statements repeatedly)

– Input and output (communicate with the "world outside of the program" (the user, the file system or a network))

● These elements are present in C++ as well:– Syntax elements: expressions, statements, blocks. They can be freely formatted.

● Statements and blocks are executed sequentially.

● Blocks group statements, esp. for branching and loops.

– The order of execution in expressions is not defined in C++!

– Input and output is implemented with special functions and objects.

if (answerIsOk){

std::cout<<"Result: "<<(3 + 4)<<std::endl;}

// Empty statement, pair of empty braces:// (We'll use this syntax in this course.)if (answerIsOk){ // pass}

// Empty statement:// (Not so good.)if (answerIsOk)

;

Page 5: (2) cpp imperative programming

5

C++ Syntax Cornerstones – Part II – Intermediate Results● Up to now we know how to "write" text on the command line from a C++ program:

● Well, it is also possible to write numbers to the command line:

● As we saw before we can also write the result of expressions to the command line:

● Here, the expression "45 * 3" is used in the calculation for two times. C++ allows storing intermediate results in variables.– We can calculate the expression "45 * 3" and store its intermediate result in a variable named product.

– Then we can use product in further expressions to get the final result:

– As can be seen, we've defined a variable product with the keyword int and initialized it with the result of the expression "45 * 3".

● Using the keyword "int" defines a variable being of type integer. Integer variables can hold integer values of course!

● We'll discuss the concepts of keywords and types at once.

std::cout<<"Hello World!"<<std::endl;// >Hello World!

std::cout<<42<<std::endl;// >42

std::cout<<45 * 3 + 21 + 5 + 45 * 3<<std::endl;// >296

int product = 45 * 3;std::cout<<product + 21 + 5 + product<<std::endl;// >296

Page 6: (2) cpp imperative programming

6

C++ Syntax Cornerstones – Part III – Keywords, Compile Time and Run Time

● C++ reserves symbols for its grammar, these symbols are called keywords.– In upcoming snippets all keywords are written in blue color. We already saw some keywords: void, int and if.

● Syntax versus Semantics:

– These statements have similar C++-syntax, but their meaning is different: they have different semantics!

● Compile time errors versus run time errors:

– This code is not in correct C++-syntax! The compiler will reject it, issue a compile time error and abort the compilation-process.

● An important function of the C++-compiler is to check the syntax for syntax errors. Syntax errors result in compile time errors and abort compilation.

● The syntax error here: a constant literal value can not be assigned! Sounds logical...

– Both statements are ok for the compiler, but the last one behaves undefined at run time.

● The result of the division by 0 is undefined in C++! Sounds familiar...

– There can also exist link time errors in the C-family languages. We'll discuss this kind of complex matter in a future lecture.

int count = 2;

count = 2;

int zero = 0;int oddResult = 42/zero;

49 = 3 + 5;

initialization

assignment

invalid initialization

Page 7: (2) cpp imperative programming

7

C++ Syntax Cornerstones – Part IV – Variables● The most elementary statements are variable definitions.

– In C++, variable definitions make variables applicable in the code.

– Variables need to be typed on definition/declaration, this is called static typing.

● A defined variable has a value/state and consumes memory.– The compound of a variable's name and its value/state/memory is called object.

● Besides definition, variables can be initialized, assigned to and declared– Initialization gives initial values to a variable.

– Assignment sets a variable to a new value.

– Declaration makes a name known in the code.

● A declaration of a name can be repeated in the code, definitions cannot!

int age = 19; int age(19); int age = {19}; // Initialization

age = 25; // assignment

extern int anotherAge; // variable declaration

double width = 32.8;

C++11 – type inferenceauto age = 19;

C++11 – uniformed initializersint age{19};

Page 8: (2) cpp imperative programming

8

C++ Syntax Cornerstones – Part V – Scopes● The region, in which a symbol has a meaning and can be used is called scope.

– I.e. functions and variables have a scope.

– C++ limits the scope of symbols by the positioning in the code and by bracing.

● Local variables have the scope of the surrounding function.– The definition of a local symbol must be unique in the same scope:

– The definition of a local variable can be overlapped by a definition in a sub scope.

● Sub scopes are constructed as a block of code enclosed in braces.

● Overlapping definitions should be avoided!

int x = 23;{

int x = 50; // Ok! Overlaps x.std::cout<<x<<std::endl;// >50

}std::cout<<x<<std::endl;// >23

int x = 23;int x = 50; // Invalid! Redefinition of x.

Page 9: (2) cpp imperative programming

9

C++ Syntax Cornerstones – Part VI – Types● Fundamental types: builtin types, whose objects can be created with literals.

– We already know the fundamental type int. int variables can be created with integer literals:

– We'll learn C++' fundamental types in short.

● Compound types, i.e. complex types built from other types:– 1. Fundamental types with qualifier: pointers and arrays.

– 2. User defined types (UDTs): enums, structs, bit-fields, unions, classes and typedefs

– 3. Functions

● Other tools for types: – C/V-qualifiers: const/volatile

– The sizes of types and objects can be retrieved with the sizeof-operator.

– (Compound types can be aliased with typedefs, which makes them UDTs.)

● Type conversion:– Standard conversions are done implicitly.

– Explicit conversions are done with explicit cast operators.

// Standard (implicit) conversion:int i = 2.78;

// Explicit conversion with casts:int i = (int)2.78;int j = int(2.78);int k = static_cast<int>(2.78);

int value = 23;

Page 10: (2) cpp imperative programming

10

C++ Syntax Cornerstones – Part VII – Identifiers and Comments● C++ uses case sensitive identifiers, we have to obey common conventions.

– aValue is not the same identifier as aVaLuE!

– We've to use a common notation (e.g. camelCase or PascalCase) as convention!

– Umlauts and special characters (save the underscore) are not allowed in identifiers!

– Compile time constants are often named with ALL_UPPER_CASE_UNDERSCORE.

– C++ keywords and special identifier-patterns mustn't be used as identifiers.

● Identifiers mustn't start with underscore+upper-case-letter or contain a double-underscore.

– Free identifiers can be put into namespaces.

● Comments allow to write all kinds of human readable annotations into the code, without introducing syntax errors.– Comments can be applied everywhere in code. We should exploit comments!

– (Virtually, comments are not that good, because they never change!)

● We as programmers have to change comments along with changes in the code.

– Better than comments is self describing code!

– Semantically, comments are ignored by the compiler.

– Technically, comments are replaced by whitespaces before the preprocessing phase.

/* commented */ // commented

int _Eva = 34; // Invalid! bool may__be = true; // Invalid!

Page 11: (2) cpp imperative programming

11

Constants – Part I● Sometimes, we have to deal with values that never change!

● 1. We can use constant values, e.g. the value 3.14 (pi) calculating a circle's area:

– Will we remember the meaning of 3.14? Such values are called magic numbers.

– Magic numbers should be avoided, as we could forget their meaning!

● 2. We can use a variable for pi to give the value a memorable name:

– But we can not prevent programmers from assigning to variables!

● But we can solve these problems with the introduction of constants.

// The double variable PI:double PI = 3.14;

PI = 4; // Ouch! Changes the meaning of PI!

double a = r * r * 3.14;

// Better memorable, eh?double a = r * r * PI;

Page 12: (2) cpp imperative programming

12

Constants – Part II● 3. We've already discussed const-qualified types, which solve this problem:

– Such objects that can not be modified are called constants.

● Constants prevent us doing coding errors and provide self documentation.– They are not modifiable and they replace magic numbers perfectly.

● Facts about constants in C++:– Compile time constants are often named with ALL_UPPER_CASE_UNDERSCORE.

– Run time constants can also be defined. A mighty feature!

– Integral constants can also be defined with enums.

– The "constness" of user defined types can be controlled in a fine grained manner.

● But it can be a complex job, as this is also a mighty feature!

PI = 4; // Invalid! PI is a constant, not a variable!

// Constant double (compile time constant) PI:const double PI = 3.14;

// This line remains valid:double a = r * r * PI;

// a as run time constant:const double a = r * r * PI;

const double SPEED_OF_LIGHT_KM_S = 300000;

Page 13: (2) cpp imperative programming

13

C++ Fundamental Integral Datatypes● int, long, short, char/wchar_t and bool

● Integral types default to be signed, can be marked to be unsigned with the keyword unsigned.

● Signed types generally use the two's complement representation in memory.– So the range of the value domain is broken into a negative and a positive wing.

– The 0 (zero) counts as positive value.

● Literals and sizes:– char {'A', 65, 0x41, 0101}; 1 = sizeof(char), at least 1B underneath

– int/short {42, -0x2A, 052}; sizeof(char) ≤ sizeof(short), at least 2B ≤ sizeof(int)

– long {42L, -0x2al, 052L}; sizeof(int) ≤ sizeof(long), at least 4B

– wchar_t {L'A', 65, 0x41, 0101}; sizeof(char) ≤ sizeof(wchar_t) ≤ sizeof(long)

– bool {true, false; falsy: 0; truthy: 42, -0x2A, 052}; 1 ≤ sizeof(bool) ≤ sizeof(long)

● C99's <stdint.h> defines integer types of guaranteed size, <inttypes.h> defines integer types of specific size and<stdbool.h> defines an explicit boolean type.

// Definition and initialization of an int:int numberOfEntries = 16;

C++11 – new char typeschar16_t c1 = u'c';char32_t c2 = U'c';

C++11 – min. 64b integerlong long x = 24LL;

C++14 – binary integerliteralauto x = 0b00101001;

C++14 – digit separatorsint x = 1'000'000;

Page 14: (2) cpp imperative programming

14

C++ Fundamental Floating Point Datatypes● double, float and long double

– Always signed.

● Generally represented as described in IEEE 754 (here single (float)).

● Literals and sizes:– sizeof(float) ≤ sizeof(double) ≤ sizeof(long double)

– double {12.3, .5 (0.5) , 10. (10.0), 123E-1}; often 8B -> exp. 10b, mts. 53b, prc. 15-16 digits

– float {12.3F, .5f, 10.f, 123E-1f}; often 4B -> exp. 8b, mts. 23b, prc. 6-8 digits

– long double {12.3L, 123E-1L}; -> 10B with exp. 15b, mts. 64b, prc. 19-20 digits

● Leading zeros can not be used in floating point literals!

mmmmmmmmmmmmmmmmmmmmmmm

31st: sign bit

s eeeeeeee

30th – 23th: exponent 22th - 0th: mantissa

- 23 bit -- 8 bit -

2-1 2-2 2-3 ... ... 22 21 20

// Definition and initialization of a double:double approxDistance = 35.25;

C++14 – digit separatorsdouble x = 0.000'026'7;

Page 15: (2) cpp imperative programming

15

Text Type: C-strings● In some examples we already saw the application of text values, e.g. to write text to the command line.

● The type, which represents text in C++, is called string, more specifically c-string.– C-strings are represented as "strings of characters", which are arrays of chars in C++ lingo.

● Initialization, literals and literal concatenation:

The assignment of c-strings isn't defined, the function std::strcpy() must be used:

● The concepts of c-strings are simple in theory, but hard in practice! Why c-strings are hard to use:– Esp. assignment and non-literal concatenation must be done with functions!

– C-string manipulation involves working with pointers and raw memory.

– C-strings are 0-terminated, which makes them error prone as well...

– => We'll discuss the concepts of memory, arrays and c-strings in depth in a future lecture!

char aName[] = "Arthur";

const wchar_t* anotherName = L"Ford";

// Erroneous:aName = "Marvin"; // Invalid!

// Correct: Copy "Marvin" to aName:std::strcpy(aName, "Marvin"); // Ok!

C++11 – new string literals/raw stringschar16_t s1[] = u"st";char32_t s2[] = U"st";char s3[] = u8"st";char rawString[] = R"(s\t)";char fullName[] = "Arthur " "and" " Ford"; // Concatenation of literals (also over multiple lines)

Page 16: (2) cpp imperative programming

16

Working with the Console● All the examples in this course will be console applications.

● Console applications make use of the OS' command line interface.

● To program console applications, we need basic knowledge about the console.– Esp. following commands are usually required:

Action Windows Unix-like/OS XChange directory cd <dirName> cd <dirName>

Change directory to parent cd .. cd ..

List current directory dir ls -l

Execute an application <appName>.exe ./<appName>

Execute an application with three arguments

<appName>.exe a b "x t" ./<appName> a b "x t"

Page 17: (2) cpp imperative programming

17

Formatted Output to Console (STL)● Writing messages to the console is a very important means to communicate!

– #include (pronunciation: "pound include") the header file (h-file) <iostream>.

– Then the output streams std::cout and std::wcout can be used to output values.

● The operator << can be used in a chained manner to output multiple values:

● The output streams are buffered, the buffer must be flushed to console:

– To flush the stream, chain std::flush or std::endl to the output expression.

● Output can be formatted with manipulators (e.g. std::boolalpha), which can be chained as well:

– #include the h-file <iomanip> to use manipulators accepting arguments (e.g. std::setprecision()):

std::cout<<"Hello there, You know about "<<42<<"?";

// Use std::boolalpha to output boolean values as literal text:std::cout<<std::boolalpha<<(23 < 78)<<std::endl;

// Use std::fixed and std::setprecision() to output 2-digit floating point value:std::cout<<std::fixed<<std::setprecision(2)<<2.666<<std::endl;

std::cout<<"Hello there, You know about "<<42<<"?"<<std::endl;

Page 18: (2) cpp imperative programming

18

Formatted Input from Console (STL)

• Use std::cin and std::wcin (<iostream>) input streams to read all kinds of types.– Before a program requires input from the user, it should prompt with an output!

– Then the input operation blocks program execution and forces user interaction.

• The operator >> can be used in a chained manner to read multiple values.

• Esp. c-strings can also be read with std::cin's member function getline().

• Both variants read c-strings up to len-1 characters and append the 0-termination.

const int len = 256;char yourName[len];int yourAge = 0;std::cin>>yourName>>yourAge;

const int len = 256;char yourName[len];std::cin.getline(yourName, len);

Page 19: (2) cpp imperative programming

19

Input from Console (STL) – Error Handling● After reading, input streams should be failure-checked (e.g. wrong format read).

– On failure the stream/buffer must be cleared and the user should be informed like so:

● On failure (std::cin.fail() will evaluate to true) we need to do following to reuse the input stream std::cin afterwards:– (1) Clear the failed bit (std::cin.clear()).

– (2) Clear the unread buffer that remained after the failure (std::cin.ignore()).

● Clear the specified number of characters or until the termination character is reached.

● With std::numeric_limits<int>::max() (<limits>) and '\n': an unlimited count of characters is read until '\n' is found.

int number = 0;std::cout<<"Enter a number"<<std::endl;std::cin>>number;if (std::cin.fail()) // E.g.: std::cin awaited an int, but somewhat different was read:{

std::cout<<"Please enter a valid number!"<<std::endl;std::cin.clear(); // (1) Clear stream status, esp. reset the fail status.std::cin.ignore(std::numeric_limits<int>::max(), '\n'); // (2) Clear buffer, esp. remove the pending newline.

}std::cout<<"You've entered the number "<<number<<"!"<<std::endl;

Page 20: (2) cpp imperative programming

20

Operator Notations – Arity● Binary operators

● Unary operators

● Ternary operator

// Addition as binary operator:int sum = 2 + 3;

// Increment as unary operator:int i = 1;++i; // Increments i (the result is 2).

// The conditional operator is the only ternary operator:int i = 2;int j = 3;const char* answer = (i < j) ? "i less than j" : "i not less than j";

Page 21: (2) cpp imperative programming

21

Operator Notations – Placement● Prefix operators

● Postfix operators

● Infix operators

// Negation as prefix operator:bool succeeded = !failed;

// Increment as postfix operator:int result = item++;

// Addition as infix operator:int sum = 2 + 3;

Page 22: (2) cpp imperative programming

22

Mathematical Operators● Binary +, - and *, / known from elementary mathematics.

– Attention: Integer division yields an integer result!

– The result of the division by 0 is undefined.

● Unary – and + as sign-operators.

● Somewhat special: ++/-- and %.

● std::log(), std::pow(), trigonometric functions etc. in the h-file <cmath>.

● Bit operations work with integers as arguments and result in integers.– Operators: ^ (xor), | (bitor), & (bitand), ~ (compl), <<, >>

– These operators should be used with unsigned values!

Page 23: (2) cpp imperative programming

23

Logical Operators● Used to compare values and combine boolean results.

– Comparison: ==, != (not_eq), <, >, <=, >=

– Combination: && (and), || (or), ! (not)

– Logical operators return boolean results, which are also integral results in C/C++.

● && (logical and) and || (logical or) support short circuit evaluation.

● Logical operators are applied in conditional expressions for control structures (branches and loops).

// The mathematic boolean expression a = b and c = b:

if (a == b && c == b) // Ok{ // pass}if (a && b == c) // Wrong!{ // pass}

// A typical beginner's error: the "optical illusion":if (a = 0) // (1) Oops! a == 0 was meant,{ // pass // but this is ok for the compiler! It evaluates to false} // (0) always, because 0 will be assigned to a!if (0 == a) // Better! Always write the constant left from the{ // pass // equality comparison.} if (0 = a) // Invalid! Here (1) could have been caught, as we{ // pass // can't assign a constant.}

// Sometimes assigning and checking values are performed intentionally:if (a = b) // Intentional assignment and evaluation.{ // pass // On modern compilers this yields a warning!} // The warning can be eliminated by writing an extra pair of parentheses:if ((a = b)) // Still not a good idea, but the syntax underscores{ // pass // the intention in a better way.}

Page 24: (2) cpp imperative programming

24

Precedence, Associativity and Order of Execution● Precedence and precedence groups.

– As operator priority in maths, precedence is controllable with parentheses.

– Some operators have the same precedence and make up a precedence group.

● Associativity defines evaluation among expressions of the same precedence.– Associativity is controllable with parentheses as well.

– Associativity is defined as a "direction".

● The order of execution within expressions is not defined in C/C++!– The order of execution within expressions can't be controlled in C/C++!

int i = 0;std::cout<<i++<<" "<<i<<std::endl;// >0 1 → Could be this result ...// >0 0 → or this!

Page 25: (2) cpp imperative programming

25

Other Operators and Operator Overloading● Assignment and combined assignment.

– Operators: =, +=, *= /= etc.

– Operators: &= (and_eq), |= (or_eq), ^= (xor_eq), <<=, >>=

● Extra operators:– Operators: ,(comma), [], (), ?:, sizeof, new, delete and typeid

– Operators: * and &

– Operators: static_cast, const_cast, dynamic_cast and reinterpret_cast

– Operators: ., ->, .*, ->*

– Operators: ::, ::*

● C++ permits to redefine (overload) some operators for user defined types (UDTs).– The introduction of new operators is not possible.

– Arity, placement, precedence and associativity of operators can't be modified.

int i = 12;i = i + 2; // (i = 14) Add and assign.i += 2; // (i = 16) Add-combined assignment.

Page 26: (2) cpp imperative programming

26

Control Structures – Expressions, Statements and Blocks● An expression is like a mathematical term: "something, that yields a value".

● A statement is a set of expressions to take effect, it doesn't need to yield a value.– Single statements need to be terminated with a semicolon.

● A block is a set of statements within curly braces ({}).– C++ code is written in blocks, this is a main style feature.

● Blocks fringe type and function bodies type definitions and scopes.– Blocks must be cascaded to code meaningful programs.

– Blocks should be indented to enhance readability!

– Use common conventions for indenting and bracing!

– In this course:

● We'll always use braces and braces are mandatory!

● In the lectures 1TBS will be used primarily.

● Blocks are also used for control structures.– Mandatory for do-loops, try-blocks and catch-clauses.

if (true) // BSD style{

// In the block.}

if (true) { // 1TBS styleif (true) {

// In the cascaded// if-block.

}}

Page 27: (2) cpp imperative programming

27

Control Structures – Conditional Branching● if/else and if/else if statements

– Execute code, if a specified condition is met.

– Conditions evaluate to bool, numeric results (int, double) also evaluate to bool!

– Can be cascaded, but combining conditions via logical operators is preferred.

– Each conditional code should be in a block!

– Alternatively use ?: expressions.

● => switch statement– Does switch among a set of branched code blocks.

– Compares the statement's input against a set of constants.

– Jumps to a labeled case section, if the switch expression matches.

– Works only with integral values. Cannot be used with strings!

– Uses fall throughs, breaks and defaults - it is rather complicated!

● The switch statement will be avoided be used in this course!

– Good, when used with only returns instead of any breaks in the case sections.

Page 28: (2) cpp imperative programming

28

Enumerations – Part I● C++ allows to group integral constants "thematically" into enumerations (enums).

– enums are the first user defined types (UDTs) we're going to discuss.

● An enum can be used like a fundamental type.– The grouped constants are valid values for an enum variable:

● The constants of an enum can have explicit integral compile time constant values:

– Otherwise the constants have increasing default values starting from 0:

// Definition of the enumeration Month:enum Month { // Month constants:

JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE, JULY,AUGUST, SEPTEMBER, OCTOBER, NOVEMBER, DECEMBER

};

Month myBirthMonth = OCTOBER;

enum Color { // Explicit values:RED = 32, BLUE = 40 // … and so forth

};

enum Color { // Default values:RED = 0, BLUE = 1 // … and so forth

};

Page 29: (2) cpp imperative programming

29

Enumerations – Part II● enum constants can be implicitly converted into ints:

– So, enums are very useful in switch statements!

● In the real world there often emerge issues with the scope of enum constants.– Therefor enums are often defined in mightier UDTs (e.g. classes) to get back scopes.

– … or enums are not used at all!

– enums are not even used in the STL.

– The usage of enums will be avoided in this course.

– C++11 introduced strongly typed enumerations. They solve some problems of "ordinary" enums.

enum Color {RED = 32, BLUE = 40 // … and more

};int colorValue = RED;std::cout<<colorValue<<std::endl;// >32

Color color = RED;switch (color) { case RED: std::cout<<"Red"<<std::endl; break; case BLUE: // …}

C++11 – strongly typed enumerationsenum class Color {

RED = 32, BLUE = 40 // … and more};// Using the constant BLUE in Color's scope:Color myFavouriteColor = Color::BLUE;

Page 30: (2) cpp imperative programming

30

Control Structures – Unconditional Branching● return and throw

– Return from a function with or w/o value.

– Throw an exception, which leaves the function as well.

● labels and goto– The unconditional jump statement.

– Do not use goto statements! Leads to "pasta oriented programming".

● When is goto used anyhow:– On the automatic generation of code.

– For algorithms that need to run really fast.

Page 31: (2) cpp imperative programming

31

Control Structures – Iteration – for Loop● The for loop is a flexible counting head controlled loop statement.

– 1. Initialization expression or statement.

– 2. Conditional expression.

– 3. Update expression.

● Can imitate the functionality of any other loop!

● => Popular to iterate arrays by index (filling, processing and output).– It should be said that processing arrays with for loops is risky:

● An array's first index is at 0, the last index is at "length – 1".

● If we get these bounds wrong (wrong initialization of the counter (i) or wrong conditional expression), we could end up in an "off-by-one-error".

● Loops are executed sequentially, not in parallel (e.g. on multiple CPU cores).

// Print the numbers 1 – 5 to console:for (int i = 1; 5 >= i; ++i) {

std::cout<<i<<std::endl;}

Page 32: (2) cpp imperative programming

32

Control Structures – Iteration – while and do Loop● while loops are head controlled.

● do loops are foot controlled.– Must be used with a block!

– Useful for menus at the console.

● Normally, the update-operation of the loop condition is done in the loop's body.

● Loops are executed sequentially, not in parallel (e.g. on multiple CPU cores).

// Print the numbers 1 – 5 to console:int i = 1;while (i <= 5) {

std::cout<<i<<std::endl;++i;

}

// Print the numbers 1 – 5 to console:int i = 1;do {

std::cout<<i<<std::endl;++i;

} while (i < 5);

Page 33: (2) cpp imperative programming

33

Control Structures – Iteration – Loop Control● continue – skips the current loop.

● break – leaves the next surrounding loop completely.

● goto – jumps to a label at almost any position in code.– Can be used to leave a nested innermost loop.

● return and throw – leave a function.

Page 34: (2) cpp imperative programming

34

Thank you!