What is Science?

1. Introduction The science of systematic processing of informations,...... particularly the automatic processing using digital .

Computer Science: Definition and History, Algorithms, Turing (Wikipedia, according to “Duden Informatik”) Machine, Higher Level Programming Languages, Tools, The first C++Program and its Syntactic and Semantic Ingredients

23 24

Informatics = Science of Computers Computer Science Informatics 6 ⊆

Computer science is not about machines, in the same way Computer science is also concerned with the development of fast that astronomy is not about telescopes. computers and networks...... but not as an end in itself but for the systematic processing Mike Fellows, US Computer Scientist (1991) of informations. http://larc.unt.edu/ian/research/cseducation/fellows1991.pdf 25 26 Computer Science = Computer Literacy This course 6

Computer literacy: user knowledge

Handling a computer Systematic problem solving with algorithms and the programming Working with computer programs for text processing, email, language C++. presentations . . . Hence: not only Computer Science Fundamental knowledge but also programming course. How does a computer work? How do you write a computer program?

27 28

Algorithm: Fundamental Notion of Computer Science Oldest Nontrivial Algorithm

Algorithm: Euclidean algorithm (from the elements from Euklid, 3. century B.C.) Instructions to solve a problem step by step Input: integers a > 0, b > 0 Execution does not require any intelligence, but precision (even computers can do it) Output: gcd of a und b according to Muhammed al-Chwarizmi, While b = 0 author of an arabic 6 If a>b then computation textbook (about 825) a a b ← − else: b b a ← − Result: a.

“Dixit algorizmi. . . ” (Latin translation) http://de.wikipedia.org/wiki/Algorithmus 29 a b a b a b a b 30 Live Demo: Turing Machine Euklid in the Box Speicher

0 1 2 3 4 5 6 7 8 9

R > L? [8] [9] L = 0? L R springe R L springe springe − − L R stop [8] zu 0 [9] zu 0 b a → → zu 6 → →

Programmcode Daten

Daten While b = 0 6 Links Rechts If a>b then a a b ← − a else: b b b a ← − Ergebnis: a. Register 31 32

ETH: pioneer of modern computer science ETH: pioneer of modern computer science

1950: ETH rents the from , the only working 1956: computer in Europe at that time. Neue Zürcher Zeitung, 30. August 1950 http://de.wikipedia.org/wiki/ERMETH 33 34 ETH: pioneer of modern computer science ETH: pioneer of modern computer science 1958–1963: Entwicklung von ALGOL 60 (der ersten formal definierten Programmiersprache), unter anderem durch Heinz Rutishauer, ETH

Die Klasse 1964 im Jahr 2015 (mit einigen Gästen) 1964: Erstmals können ETH-Studierende selbst einen Computer programmieren (die CDC 1604, gebaut von Seymour Cray). http://www.inf.ethz.ch/news-and-events/spotlights/1964.html Vortrag Walter Gander, 50 Jahre Programmieren, ETH Zürich, 2014 35 36

ETH: pioneer of modern computer science Computers – Concept

A bright idea: universal Turing machine (Alan Turing, 1936) 1968–1990: Niklaus Wirth entwickelt an der ETH die Programmiersprachen Pascal, Modula-2 und Oberon und 1980 die Lilith, einen der ersten Computer mit grafischer Benutzeroberfläche.

Alan Turing http://en.wikipedia.org/wiki/Alan_Turing 37 38 Computer – Implementation Computer

Z1 – Konrad Zuse (1938) ENIAC – (1945) Ingredients of a and Konrad Zuse Memory (RAM) for programs data Processor (CPU) to process programs and data I/O components to communicate with the world

John von Neumann http://www.hs.uni-hamburg.de/DE/GNT/hh/biogr/zuse.htm http://commons.wikimedia.org/wiki/File:John_von_Neumann.jpg 39 40

Memory for data and program Processor

Sequence of bits from 0, 1 . { } Program state: value of all bits. The processor (CPU) Aggregation of bits to memory cells (often: 8 Bits = 1 Byte) executes instructions in machine language Every memory cell has an address. has an own "fast" memory (registers) Random access: access time to the memory cell is (nearly) independent of its address. can read from and write to main memory features a set of simplest operations = instructions (e.g. adding to register values)

41 42 Computing speed Programming

In the time, on average, that the sound takes to travel from from my With a programming language we issue commands to a computer mouth to you ... such that it does exactly what we want. The sequence of instructions is the (computer) program 30 m = more than 100.000.000 instructions

b a contemporary desktop PC can process more than 100 millions instructions 2

2 Uniprocessor computer at 1 GHz. The Harvard Computers, human computers, ca.1890 http://en.wikipedia.org/wiki/Harvard_Computers 43 44

Why programming?

Do I study computer science or what ... There are programs for everything ... Mathematics used to be the lingua franca of the natural sciences on all universities. Today this is computer I am not interested in programming ... science. because computer science is a mandatory subject here, Lino Guzzella, president of ETH Zurich, NZZ Online, 1.9.2017 unfortunately......

45 46 This is why programming! Programming Languages

Any understanding of modern technology requires knowledge about the fundamental operating principles of a computer. The language that the computer can understand (machine Programming (with the computer as a tool) is evolving a cultural language) is very primitive. technique like reading and writing (using the tools paper and Simple operations have to be subdivided into many single steps pencil) The machine language varies between computers. Most qualified jobs require at least elementary programming skills Programming is fun!

47 48

Higher Programming Languages Programming langauges – classification

Differentiation into Compiled vs. interpreted languages can be represented as program text that C++, C#, Pascal, Modula, Oberon, Java vs. can be understood by humans Python, Tcl, Matlab is independent of the computer model Abstraction! Higher programming languages vs. Assembler → Multi-purpose programming languages vs. single purpose programming languages Procedural, object oriented, functional and logical languages.

49 50 Why C++? Why C++?

Other popular programming languages: Java, C#, Objective-C, Modula, Oberon, Python . . . C++equips C with the power of the abstraction of a higher C++ is practically relevant. programming language For the computational computing (as required in math and In this course: C++ introduced as high level language, not as physics), C++ offers a lot of useful concepts. better C C++ is widespread and “runs everywhere” Approach: traditionally procedural object-oriented. → C++ is standardized i.e. there is an “official” C++. The lecturer likes C++.

51 52

Deutsch vs. C++ Syntax and Semantics

Deutsch Es ist nicht genug zu wissen, man muss auch anwenden. Like our language, programs have to be formed according to certain rules. (Johann Wolfgang von Goethe) Syntax: Connection rules for elementary symbols (characters) Semantics: interpretation rules for connected symbols. C++ Corresponding rules for a computer program are simpler but also // computation more strict because computers are relatively stupid. intb=a a;//b=a^2 ∗ b = b b;//b=a^4 ∗

53 54 C++: Kinds of errors illustrated with German sentences Syntax and Semantics of C++

Das Auto fuhr zu schnell. Syntaktisch und semantisch korrekt. Syntax DasAuto fuh r zu sxhnell. Syntaxfehler: Wortbildung.

Rot das Auto ist. Syntaxfehler: Satzstellung. What is a C++ program?

Man empfiehlt dem Dozenten Syntaxfehler: Satzzeichen fehlen . Is it grammatically correct? nicht zu widersprechen

Sie ist nicht gross und rothaarig. Syntaktisch korrekt aber mehrdeutig. [kein Analogon] Semantics

Syntaktisch korrekt, doch semantisch fehlerhaft: Die Auto ist rot. Falscher Artikel. [Typfehler] What does a program mean? Syntaktisch und grammatikalisch korrekt! Semantisch Das Fahrrad gallopiert schnell. fehlerhaft. [Laufzeitfehler] What kind of algorithm does a program implement? Syntaktisch und semantisch korrekt. Semantisch Manche Tiere riechen gut. mehrdeutig. [kein Analogon]

55 56

Syntax and semantics of C++ Programming Tools

The ISO/IEC Standard 14822 (1998, 2011,...) Editor: Program to modify, edit and store C++program texts is the “law” of C++ : program to translate a program text into machine language defines the grammar and meaning of C++programs Computer: machine to execute machine language programs contains new concepts for advanced programming . . . : program to organize all procedures such as . . . which is why we will not go into details of such concepts file handling, editor-, compiler- and program execution.

57 58 Language constructs with an example The first C++ program Most important ingredients. . . // Program: power8.cpp // Raisea number to the eighth power. Comments/layout constants #include int main() { Include directive identifiers, names // input the main function objects std::cout <<"Computea^8 fora =?"; int a; Values effects expressions std::cin >> a; Statements: Do something (read in a)! Types and functionality L- and R- values // computation intb=a a;//b=a^2 Expressions: Compute a value (a2)! literals operators ∗ b = b b;//b=a^4 ∗ variables statements // outputb b,i.e.,a^8 ∗ std::cout << a <<"^8=" << b b <<"\n"; ∗ return 0; } 59 60

Behavior of a Program “Accessories:” Comments

At compile time: // Program: power8.cpp // Raisea number to the eighth power. program accepted by the compiler (syntactically correct) #include int main() { Compiler error // input comments std::cout <<"Computea^8 fora =?"; int a; During runtime: std::cin >> a; correct result // computation intb=a a; //b=a^2 ∗ b = b b; //b=a^4 incorrect result ∗ // outputb b,i.e.,a^8 ∗ program crashes std::cout << a <<"^8=" << b b <<"\n"; ∗ program does not terminate (endless loop) return 0; } 61 62 Comments and Layout Comments and Layout Comments are contained in every good program. The compiler does not care... document what and how a program does something and how it should be used, #include are ignored by the compiler int main(){std::cout << "Compute a^8 for a =? "; Syntax: “double slash” // until the line ends. int a; std::cin >> a; int b = a * a; b = b * b; std::cout << a << "^8 = " << b*b << "\n";return 0;} The compiler ignores additionally Empty lines, spaces, ... but we do! Indendations that should reflect the program logic

63 64

“Accessories:” Include and Main Function Include Directives // Program: power8.cpp // Raisea number to the eighth power. C++ consists of #include include directive int main() { declaration of the main function the core language // input standard library std::cout <<"Computea^8 fora =?"; int a; in-/output (header iostream) std::cin >> a; mathematical functions (cmath) // computation ... intb=a a; //b=a^2 ∗ b = b b; //b=a^4 ∗ // outputb b,i.e.,a^8 #include ∗ std::cout << a <<"^8=" << b b <<"\n"; ∗ makes in- and output available return 0; } 65 66 The main Function Statements: Do something! the main-function int main() { // input is provided in any C++ program std::cout <<"Computea^8 fora =?"; int a; is called by the operating system std::cin >> a; expression statements like a mathematical function ... // computation int b = a a; //b=a^2 ∗ arguments b = b b; //b=a^4 ∗ return value // outputb b,i.e.,a^8 ∗ std::cout << a <<"^8=" << b b <<"\n"; ∗ ... but with an additional effect return 0; return statement } Read a number and output the 8th power.

67 68

Statements Expression Statements

have the following form: building blocks of a program C++ expr; are executed (sequentially) end with a semicolon where expr is an expression Any statement has an effect (potentially) Effect is the effect of expr, the value of expr is ignored.

Example: b = b*b;

69 70 Return Statements Statements – Effects int main() { effect: output of the string Compute ... // input std::cout <<"Computea^8 fora =?"; do only occur in functions and are of the form int a; std::cin >> a; Effect: input of a number stored in a return expr; // computation Effect: saving the computed value of a*a into b int b = a a; //b=a^2 where expr is an expression ∗ b = b b; //b=a^4 ∗ specify the return value of a function Effect: saving the computed value of b*b into b // outputb b,i.e.,a^8 ∗ Example: return 0; std::cout << a <<"^8=" << b b <<"\n"; ∗ return 0; } Effect: return the value 0 Effect: output of the value of a and the computed value of b*b

71 72

Values and Effects Statements – Variable Definitions

int main() { // input std::cout <<"Computea^8 fora =?"; determine what a program does, int a; declaration statement

are purely semantical concepts: type std::cin >> a; names // computation Z Symbol 0 means Value 0 int b = a a; //b=a^2 ∈ ∗ std::cin >> a; means effect "read in a number" b = b b; //b=a^4 ∗ // outputb b,i.e.,a^8 ∗ depend on the program state (memory content, inputs) std::cout << a <<"^8=" << b b <<"\n"; ∗ return 0; }

73 74 Declaration Statements Types and Functionality

int: introduce new names in the program, C++ integer type consist of declaration and semicolon corresponds to (Z, +, ) in math Example: int a; × can initialize variables In C++ each type has a name and Example: int b = a * a; a domain (e.g. integers) functionality (e.g. addition/multiplication)

75 76

Fundamental Types Literals

C++ comprises fundamental types for represent constant values integers (int) have a fixed type and value natural numbers (unsigned int) are "syntactical values". real numbers (float, double) Examples: boolean values (bool) 0 has type int, value 0. ... 1.2e5 has type double, value 1.2 105. ·

77 78 Variables Objects

represent (varying) values, Beispiel represent values in main memory have int a; defines a variable with have type, address and value (memory content at the address) name name: a can be named (variable) ... type type: int ... but also anonymous. value address value: (initially) undefined Remarks are "visible" in the program Address: determined by A program has a fixed number of variables. In order to be able to deal with a compiler variable number of value, it requires "anonymous" addresses that can be address context. via temporary names.

79 80

Identifiers and Names Expressions: compute a value!

(Variable-)names are identifiers represent Computations allowed: A,...,Z; a,...,z; 0,...,9;_ are either primary( b) First symbol needs to be a character. or composed( b*b)...... from different expressions, using operators There are more names: have a type and a value std::cin (Qualified identifier) Analogy: building blocks

81 82 Expressions Building Blocks Expressions composite expression // input std::cout << "Compute a^8 for a =? "; represent computations int a; are primary or composite (by other expressions and operations) std::cin >> a; a * a // computation composed of intb=a a; //b=a^2 variable name, operator symbol,variable name ∗ b = b * b;//Twob=a^4 times composed expression variable name: primary expression can be put into parantheses // outputb b,i.e.,a^8 ∗ std::cout << a<< "^8 = " << b * b << ". n"; a * a is equivalent to (a * a) \

return 0;Four times composed expression

83 84

Expressions L-Values and R-Values

// input R-Value have type, value und effect (potentially). std::cout << "Compute a^8 for a =? "; int a; Example Example std::cin >> a; L-value (expression + address) a * a b = b * b type: int (type of the operands) type: int (Typ der Operanden) // computation L-value (expression + address) int b = a a; //b=a^2 Value: product of a and a Value: product of b and b ∗ Effect: none. effect: assignment of the product value b = b * b;//b=a^4 to b R-Value // outputb b,i.e.,a^8 ∗ std::cout << a<< "^8 = " << b * b << ". n"; The type of an expression is fixed but the value and effect are only \ determined by the evaluation of the expression return 0; R-Value (expression that is not an L-value)

85 86 L-Values and R-Values L-Values and R-Values

L-Wert (“Left of the assignment operator”) R-Wert (“Right of the assignment operator”) Expression with address Expression that is no L-value Value is the content at the memory location according to the Example: literal 0 type of the expression. Any L-Value can be used as R-Value (but not the other way L-Value can change its value (e.g. via assignment) round) An R-Value cannot change its value Example: variable name

87 88

L-Value and R-Value Operators and Operands Building Blocks left operand (output stream) output operator right operand (string) // input std::cout <<"Computea^8 fora =?"; int a; std::cin >> a; right operand (variable name) // computationinput operator int b = alefta; operand//b= (inputa^2 stream) ∗ b = b b; //b=a^4 ∗ // outputassignmentb b, operatori.e.,a^8 ∗ std::cout << a <<"^8=" << b * b <<"\n"; return 0; multiplication operator

89 90 Operators Multiplication Operator *

Operators expects two R-values of the same type as operands (arity 2) "returns the product as R-value of the same type", that means combine expressions (operands) into new composed formally: expressions The composite expression is an R-value; its value is the product of the specify for the operands and the result the types and if the have value of the two operands to be L- or R-values. have an arity Examples: a * a and b * b

91 92

Assignment Operator = Input Operator >>

Left operand is L-value, Right operand is R-value of the same type. left operand is L-Value (input stream) Assigns to the left operand the value of the right operand and right operand is L-Value returns the left operand as L-value assigns to the right operand the next value read from the input stream, removing it from the input stream and returns the input Examples: b = b * b and a = b stream as L-value Example std::cin >> a (mostly keyboard input) Attention, Trap! Input stream is being changed and must thus be an L-Value. The operator = corresponds to the assignment operator of mathematics (:=), not to the comparison operator (=).

93 94 Output Operator << Output Operator <<

Why returning the output stream? left operand is L-Value (output stream) allows bundling of output right operand is R-Value outputs the value of the right operand, appends it to the output std::cout << a << "^8 = " << b * b << "\n" stream and returns the output stream as L-Value is parenthesized as follows Example: std::cout << a (mostly console output) ((((std::cout << a) << "^8 = ") << b * b) << "\n") The output stream is being changed and must thus be an L-Value. std::cout << a is the left hand operand of the next << and is thus an L-Value that is no variable name

95 96 power8 exact.cpp power8 exact.cpp

// Program: power8_exact.cpp // Raise a number to the eighth power, // using integers of arbitrary size

#include Problem with power8.cpp: large input values are not correctly #include

handled int main() { reason: domain of the type int is limited // input std::cout << "Compute a^8 for a =? "; solution: use a different type ifmp::integer a; e.g. ifm::integer std::cin >> a; // computation ifmp::integer b = a * a; // b = a^2 b = b * b; // b = a^4

// output b * b, i.e., a^8 std::cout << a << "^8 = " << b * b << ".\n"; return 0; 97 } 98