Programming Paradigms

Prof. Dr. Mirco Schoenfeld

The problem

Remember?

Functionality

In your tasks, you have used a specific set of functions to control the agent.

  • move() to move your agent forward
  • turnLeft() and turnRight() to turn your agent
  • canMove() to tell you’re blocked by a wall
  • visitedBefore() to tell you if you have visited a path before
  • isExit() which tells you if you have reached the exit

Functionality

Why have we just written the functionality like this?

move()

In program code, this is how we call functions. More on that later.

Functionality

What else do we need?

We need control instructions to control the flow of execution.

Control instructions

Control instructions are core components of
imperative programming.

In imperative programming, algorithms are
described as a sequence of commands to be executed.

Your algorithm is a description of how to achieve a certain result.

Imperative programming

Imperative programming is a programming paradigm.

That is a high-level way to conceptualize and structure the implementation of a computer program.

Imperative programming

In imperative programming, we have

Programming paradigms

There are also other paradigms:

Back to the topic

https://www.getyarn.io/yarn-clip/87e76303-46c4-4cb3-9552-2b5cf2a9b2e7/gif

Control instructions

What kind of control instructions do we need to escape our maze?

Control instructions

To escape our maze, we need to

  • decide, e.g. if we hit a wall,
  • repeat, e.g. moving forward,
  • save our state, e.g. describing where we were,
  • evaluate our state.

if-then-else

To decide, we use conditional statements.

If (A) Then
  (B)
Else
  (C)
End If

A is a Boolean expression. More on that later.

The Else-part is optional!

loops

To repeat, we use loops, here a condition-controlled loop:

While (A) do
  (B)
Done

B is executed and repeated as long as the condition A holds.

loops

Another variant of this loop called do while loop:

Do
  (B)
While (A)

It executes B once before checking A the first time.

assignments

To save our state, we need variables and assignments.

Variables

Variables are abstract storage locations with a symbolic name.

Variables contain some data or object referred to as a value.

Variables

Variables in CS are different from variables in mathematics!

In mathematics, variables refer to something that can be modified.

In computers, variables can actually change their value.

assignments

Assignments are the most fundamental construct in imperative programming.

x = 5

This assigns the value 5 to a variable named x.

assignments

The value of x can be changed later in the program through subsequent assignments.

x = 5
# some other stuff
# happening here
x = 7

expressions

To evaluate our state, we need expressions.

These are syntactic entities that may be evaluated to determine their value.

Expressions

Example expressions:

x + y
2 * (i + 1)
pow(x,2)

Expressions combine values of variables to a new value.

Boolean expressions

And what are Boolean expressions?

If (A) Then
  (B)
End If

Boolean expressions are expressions that evaluate to
either true or false.

Boolean expressions

Example Boolean expressions:

x < 5
x >= y
x == y
x != y

It’s your turn!

Task

Write code using conditionals

Consider variable x:

  1. Is the value of x smaller than 5? Let the code call print("smaller") if so.
  2. Does x contain ‘word’? Let the code call print("x contains 'word'") if so and print("nope") otherwise.
  3. Call print("adding one!") and increment x by one as long as x is smaller than 10.

Time: 30 minutes

Task Summary

What have you achieved?

Task Summary

IF x<5 THEN
  print("smaller")
ENDIF
IF x == 'word' THEN
  print("x contains 'word'")
ELSE
  print("nope")
ENDIF
WHILE x < 10 DO
  print("adding one!")
  x = x+1
DONE

A special loop

What does this count-controlled loop do?

for ( i=1; i<=100; i+=1){
  print i;
}

Summary

Summary

A brief summary of control flows:

  • variables
  • (Boolean) expressions
  • assignments
  • conditionals
  • loops

Thanks

https://xkcd.com/1652/

References

Back to Lecture Website