the basics of programming - johannes kepler university linz · 2020. 9. 1. · the basics of...

22
The Basics of Programming The Basics of Programming Wolfgang Schreiner Research Institute for Symbolic Computation (RISC) Johannes Kepler University, Linz, Austria [email protected] http://www.risc.jku.at Wolfgang Schreiner RISC

Upload: others

Post on 14-Mar-2021

1 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

The Basics of ProgrammingWolfgang Schreiner

Research Institute for Symbolic Computation (RISC)

Johannes Kepler University, Linz, Austria

[email protected]

http://www.risc.jku.at

Wolfgang Schreiner RISC

Page 2: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Basic Notions

• Program: formal description of a method solving a problem.– Consists of instructions (also called commands or statements).

– Operates on data (values like numbers, characters, text files, images, . . . ).

We will now investigate those elements of programming that are validfor (almost) any programming language.

Wolfgang Schreiner 1

Page 3: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Variables

Data are stored in variables.

• Variable: a labelled box holding some content.– The label is the name of the variable.

– The content is the value of the variable.

• Example: variable named x with value 17:

x: 17

• Variables may hold different kinds of values.– Integer numbers: x: 17

– Characters: ch: ’+’

– Strings of characters: str : “hello”

Think of variables as boxes.Wolfgang Schreiner 2

Page 4: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Changing Variable Values

A program operates on variables and changes its values.

• Variables before the execution of a program.

x: 17ch: ’+’str : “hello”

• Variables after the execution of the program.

x: 18

ch: ’*’str : “world”

The name “variable” comes from “vary” (changing).

Wolfgang Schreiner 3

Page 5: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Variable Types

Variable types may not change arbitrarily

• Every variable has an associated type.– The set of values that it may hold during its whole life-time.

– The type remains fixed during its life-time.

– The type may be attached to the visual representation.

x: 17 integerch: ’+’ characterstr : “hello” string

For the moment, we will only use variables of type “integer”.

Wolfgang Schreiner 4

Page 6: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Variable Assignments

The simplest and most important instruction.

x← E

• Read as “x becomes E”.– Variable x.

– Mathematical expression E.

• Compute value of E and store it in x.– Overwrites previous value of x.

Assignments change variable values.

Wolfgang Schreiner 5

Page 7: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Example

• x: 5 , y: 1

y ← 2

• x: 5 , y: 2

y ← x + 1

• x: 5 , y: 6

x← x + 1

• x: 6 , y: 6

x← x ∗ y

• x: 36 , y: 6

The execution of a program is a sequence of variable assignments.

Wolfgang Schreiner 6

Page 8: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Sequences

A program may consist of a sequence of commands:

• x: 5 , y: 1

y ← 2y ← x + 1x← x + 1x← x ∗ y

• x: 36 , y: 6

Multiple commands may be listed one after another.

Wolfgang Schreiner 7

Page 9: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Control Flow Diagrams

Arrows may indicate the order in which commands are executed.

↓y ← 2↓

y ← x + 1↓

x← x + 1↓

x← x ∗ y↓

Such diagrams will become important to visualize control structures.

Wolfgang Schreiner 8

Page 10: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Assertions

Diagrams may be annotated by assertions.

• Assertion: a proposition about the variable values.– Must hold at the denoted position of the program execution.

↓y ← 2 ∗ x↓ -- y is even

z ← y + 1↓ -- z is odd

Dashed line -- indicates position of assertion in control flow.

Wolfgang Schreiner 9

Page 11: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Conditionals

A statement may be only executed if a condition holds.

E

C1 C2

true false

• Conditional statement– Condition E.

– Commands C1 and C2 (the branches).

– If E holds, then C1 is executed, else C2 is executed.

The control flow may branch.Wolfgang Schreiner 10

Page 12: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Example

Computation of the minimum of two values.

x<y

m <− x m <− y

true false

−− m <= x and m <= y and (m=x or m=y)

−− x>=y

Variable m is set to the smaller of the values of x and y.

Wolfgang Schreiner 11

Page 13: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Example

Alternative version of minimum computation.

m>y

m <− y

−− m <= x and m <= y and (m=x or m=y)

m <− x

true false

−− m = x

−− m = x and m <= y

A conditional may also have only one branch.

Wolfgang Schreiner 12

Page 14: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Loops

A statement may be repeatedly executed (iterated).

E

C

true

false

• Loop statement– Condition E (the loop condition).

– Command C (the loop body).

– While E holds, C is executed.

The control flow may form loops.

Wolfgang Schreiner 13

Page 15: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Example

The computation of an exponentiation value.

i < b

r <− r*a

false

i <− i+1

r <− 1

i <− 0

−− r = a^b

r = a^i −−

true

−− i = b

r i

Before first iteration 1 0

After first iteration 4 1

After second iteration 16 2

After third iteration 64 3

The variable r is set to ab.Wolfgang Schreiner 14

Page 16: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Algorithm Descriptions

Format for describing algorithms.

Command

Name(Parameters)

• Every algorithm has a name and parameters.– Parameters: variables provided by the user.

– Algorithm may use these variables and internal ones.

• Triangles initiate and terminate the control flow.

Wolfgang Schreiner 15

Page 17: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Swapping two Variables

Exchange the values of two variables.

x: 3 , y: 5 ; x: 5 , y: 3

Swap(x,y)

?

We have to develop the body of the algorithm skeleton.

Wolfgang Schreiner 16

Page 18: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Swapping two Variables

Algorithm needs an auxiliary variable.

Swap(x,y)

z <− x

y <− zx <− y

x y z

6 3 6 5 Initial Situation

3 After z ← x

5 After x← y

3 After y ← z

Every algorithm can be simulated with paper and pencil.

Wolfgang Schreiner 17

Page 19: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Maximum of Three Numbers

Find the largest of three numbers.

Max3(a,b,c,m)

a > b

a > c

m <− a

b > c

m <− c m <− b m <− c

true false

true truefalse false

How to verify the correctness of the algorithm?

Wolfgang Schreiner 18

Page 20: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Maximum of Three Numbers

Max3(a,b,c,m)

a > b

a > c

m <− a

b > c

m <− c m <− b m <− c

−− b >= a

−− c >= b−− c >= a

Use assertions to support the reasoning.

Wolfgang Schreiner 19

Page 21: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Primality Test

Test whether a given natural number n is a prime number.

• Prime number: a natural number n such that– n is greater than 1 and

– n is only divisible by 1 and itself.

∗ n is divisible by m (written as m|n) if and only if n = m · o for some natural number o.

∗ For example, 10 is divisible by 5 (5|10) because 10 = 5 · 2.

• Prime numbers: 2, 3, 5, 7, 11, 13, 17, . . .– 2 is the only even prime number (why?).

Signal success by setting a variable p to “true” or “false”.

Wolfgang Schreiner 20

Page 22: The Basics of Programming - Johannes Kepler University Linz · 2020. 9. 1. · The Basics of Programming Primality Test Test whether a given natural number n is a prime number. Prime

The Basics of Programming

Primality Test

n<2

p <− true

n=2

2 | n

p <− false c <− 3

c*c <= n and p = true

c | n

p <− false c <− c+2

p <− false

IsPrime(n,p)

true false

true false

false

true false

true false

true

Wolfgang Schreiner 21