• Keine Ergebnisse gefunden

Control Structures

N/A
N/A
Protected

Academic year: 2022

Aktie "Control Structures"

Copied!
116
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Departement Informatik

Basics of CS

Control Structures

Hans-Joachim Böckenhauer Dennis Komm

Autumn 2021 – October 20, 2021

(2)

Strings

Repetition

(3)

Strings

Strings are “lists of characters” (there are differences) Characters correspond (mostly) to keys on keyboard

Strings are written in quotation marks

Access to single characters using brackets (“read-only”) String word = "HELLO WORLD"

word[0] is first character word[1] is second character . . .

word[len(word)-1] is last character

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 1 / 36

(4)

Strings – Operators

Addition

Numbers: Arithmetic operation

Lists and strings: Merging (there is no append for strings)

daten = [1, 4, 6]

daten2 = daten + [5]

satz = "Guten Tag"

satz2 = satz + ", Urs"

satz3 = satz2 + "."

Multiplication

Numbers: Arithmetic operation Lists and strings: Merging

daten = [2, 3] * 5

satz = "HA" * 3

(5)

Strings – Operators

Addition

Numbers: Arithmetic operation

Lists and strings: Merging (there is no append for strings)

daten = [1, 4, 6]

daten2 = daten + [5]

satz = "Guten Tag"

satz2 = satz + ", Urs"

satz3 = satz2 + "."

Multiplication

Numbers: Arithmetic operation Lists and strings: Merging

daten = [2, 3] * 5 satz = "HA" * 3

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 2 / 36

(6)

Characters – The Unicode Table

0–18 19–37 38–56 57–75 76–94 95–113 114–127

Dec. Char. Dec. Char. Dec. Char. Dec. Char. Dec. Char. Dec. Char. Dec. Char.

0 NUL 19 DC3 38 & 57 9 76 L 95 _ 114 r

1 SOH 20 DC4 39 ’ 58 : 77 M 96 ‘ 115 s

2 STX 21 NAK 40 ( 59 ; 78 N 97 a 116 t

3 ETX 22 SYN 41 ) 60 < 79 O 98 b 117 u

4 EOT 23 ETB 42 * 61 = 80 P 99 c 118 v

5 ENQ 24 CAN 43 + 62 > 81 Q 100 d 119 w

6 ACK 25 EM 44 , 63 ? 82 R 101 e 120 x

7 BEL 26 SUB 45 - 64 @ 83 S 102 f 121 y

8 BS 27 ESC 46 . 65 A 84 T 103 g 122 z

9 HT 28 FS 47 / 66 B 85 U 104 h 123 {

10 LF 29 GS 48 0 67 C 86 V 105 i 124 |

11 VT 30 RS 49 1 68 D 87 W 106 j 125 }

12 FF 31 US 50 2 69 E 88 X 107 k 126 ~

13 CR 32 SP 51 3 70 F 89 Y 108 l 127 DEL

14 SO 33 ! 52 4 71 G 90 Z 109 m . . .

15 SI 34 "’ 53 5 72 H 91 [ 110 n

16 DLE 35 # 54 6 73 I 92 \ 111 o

17 DC1 36 $ 55 7 74 J 93 ] 112 p

18 DC2 37 % 56 8 75 K 94 ˆ 113 q

(7)

Characters – The Unicode Table

Use functions ord() and chr() ord(x) returns position of character x in Unicode table chr(y) returns character at position y in Unicode table

x = input("Enter a character: ")

print("The character", x, "is at position", ord(x))

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 4 / 36

(8)

Exercise – Print Characters

Write a program that

outputs the first 26 uppercase letters uses a for-loop to this end

Recall

The letter A is located at position 65 in

the Unicode table

(9)

Exercise – Print Characters

for i in range(65, 91):

print(chr(i))

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 6 / 36

(10)

Caesar Encryption

(11)

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 7 / 36

(12)

Gallia est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam, qui ipsorum lingua Celtae,

nostra Galli appellantur.

(13)

Gallia est omnis divisa in partes tres, quarum unam incolunt Belgae, aliam Aquitani, tertiam, qui ipsorum lingua Celtae,

nostra Galli appellantur.

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 7 / 36

(14)

Insecure channel

(15)

Symmetric Encryption

Sender Recipient

plaintext

Encryption

ciphertext

Communication medium

(messenger, internet, . . . )

ciphertext Decryption

plaintext

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 8 / 36

(16)

Caesar Encryption

Situation

Parties A and B want to communicate over an insecure channel, this time using Caesar encryption

Shared key k as number between 1 and 25

A encrypts message by adding k to each character A sends encrypted message to B

B decrypts message by subtracting k from each character

(17)

Caesar Encryption

Shift characters by fixed value k by adding k

Example

A B C D E F G H I J K L M

W X Y Z A B C D E F G H I

N O P Q R S T U V W X Y Z

J K L M N O P Q R S T U V

Plaintext: HELLO WORLD Ciphertext: DAHHK SKNHZ

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 10 / 36

(18)

Caesar Encryption

Shift characters by fixed value k by adding k Example

A B C D E F G H I J K L M

W X Y Z A B C D E F G H I

N O P Q R S T U V W X Y Z

J K L M N O P Q R S T U V

Plaintext: HELLO WORLD

Ciphertext: DAHHK SKNHZ

(19)

Caesar Encryption

Shift characters by fixed value k by adding k Example

A B C D E F G H I J K L M

W X Y Z A B C D E F G H I

N O P Q R S T U V W X Y Z

J K L M N O P Q R S T U V

Plaintext: HELLO WORLD Ciphertext: DAHHK SKNHZ

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 10 / 36

(20)

Caesar Encryption

1. Entered letter is Unicode character between A and Z

A B . . . W X Y Z

65 66 . . . 87 88 89 90

2. Subtract 65 so that the result is between 0 and 25

A B . . . W X Y Z

0 1 . . . 22 23 24 25

3. Now add key (for instance, 3) and compute modulo 26

A B . . . W X Y Z

3 4 . . . 25 0 1 2

4. Finally add 65 to the result

A B . . . W X Y Z

68 69 . . . 90 65 66 67

(21)

Caesar Encryption

1. Entered letter is Unicode character between A and Z

A B . . . W X Y Z

65 66 . . . 87 88 89 90

2. Subtract 65 so that the result is between 0 and 25

A B . . . W X Y Z

0 1 . . . 22 23 24 25

3. Now add key (for instance, 3) and compute modulo 26

A B . . . W X Y Z

3 4 . . . 25 0 1 2

4. Finally add 65 to the result

A B . . . W X Y Z

68 69 . . . 90 65 66 67

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 11 / 36

(22)

Caesar Encryption

1. Entered letter is Unicode character between A and Z

A B . . . W X Y Z

65 66 . . . 87 88 89 90

2. Subtract 65 so that the result is between 0 and 25

A B . . . W X Y Z

0 1 . . . 22 23 24 25

3. Now add key (for instance, 3) and compute modulo 26

A B . . . W X Y Z

3 4 . . . 25 0 1 2

4. Finally add 65 to the result

A B . . . W X Y Z

68 69 . . . 90 65 66 67

(23)

Caesar Encryption

1. Entered letter is Unicode character between A and Z

A B . . . W X Y Z

65 66 . . . 87 88 89 90

2. Subtract 65 so that the result is between 0 and 25

A B . . . W X Y Z

0 1 . . . 22 23 24 25

3. Now add key (for instance, 3) and compute modulo 26

A B . . . W X Y Z

3 4 . . . 25 0 1 2

4. Finally add 65 to the result

A B . . . W X Y Z

68 69 . . . 90 65 66 67

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 11 / 36

(24)

Euclidean Division (Modulo Operation)

Using “%,” we obtain the residue of the integer division Analogously, “//” gives the part before the decimal point

10 % 3 = 1, because 9 = 3 · 3 10 % 4 = 2, because 8 = 4 · 2 12 % 3 = 0, because 12 = 4 · 3 11 // 5 = 2, because 10 = 5 · 2 23 // 4 = 5, because 20 = 4 · 5

If x % y == 0, x is divided by y

(25)

Euclidean Division (Modulo Operation)

Using “%,” we obtain the residue of the integer division Analogously, “//” gives the part before the decimal point

10 % 3 = 1, because 9 = 3 · 3 10 % 4 = 2, because 8 = 4 · 2 12 % 3 = 0, because 12 = 4 · 3 11 // 5 = 2, because 10 = 5 · 2 23 // 4 = 5, because 20 = 4 · 5

If x % y == 0, x is divided by y

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 12 / 36

(26)

Euclidean Division (Modulo Operation)

Using “%,” we obtain the residue of the integer division Analogously, “//” gives the part before the decimal point

10 % 3 = 1, because 9 = 3 · 3 10 % 4 = 2, because 8 = 4 · 2 12 % 3 = 0, because 12 = 4 · 3 11 // 5 = 2, because 10 = 5 · 2 23 // 4 = 5, because 20 = 4 · 5

If x % y == 0, x is divided by y

(27)

Exercise – Caesar Decryption

Write a program that

runs through a given string decrypts each letter with a key k tries out each key k

uses the following formula e = (v − 65 − k) % 26 + 65

Decrypt the ciphertext DLUUQLTHUKSHBAOLBYLRHYBMANPIALZZJOVNNP

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 13 / 36

(28)

Exercise – Caesar Decryption

for k in range(0, 26):

for item in ciphertext:

print(chr((ord(item) - 65 - k) % 26 + 65), end="") print()

for k in range(0, 26):

for i in range(0, len(ciphertext)):

print(chr((ord(ciphertext[i]) - 65 - k) % 26 + 65), end="")

print()

(29)

Logical Values

Boolean Values and Relational Operators

(30)

Boolean Values and Variables

Boolean expressions can take on one of two values F or T

F corresponds to “false” T corresponds to “true”

George Boole [Wikimedia]

Boolean variables in Python

represent “logical values” Domain {False, True} Example

b = True # Variable with value True

(31)

Boolean Values and Variables

Boolean expressions can take on one of two values F or T F corresponds to “false”

T corresponds to “true”

George Boole [Wikimedia]

Boolean variables in Python

represent “logical values” Domain {False, True} Example

b = True # Variable with value True

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 15 / 36

(32)

Boolean Values and Variables

Boolean expressions can take on one of two values F or T F corresponds to “false”

T corresponds to “true”

George Boole [Wikimedia]

Boolean variables in Python

represent “logical values” Domain {False, True} Example

b = True # Variable with value True

(33)

Boolean Values and Variables

Boolean expressions can take on one of two values F or T F corresponds to “false”

T corresponds to “true”

George Boole [Wikimedia]

Boolean variables in Python represent “logical values”

Domain {False, True}

Example

b = True # Variable with value True

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 15 / 36

(34)

Boolean Values and Variables

Boolean expressions can take on one of two values F or T F corresponds to “false”

T corresponds to “true”

George Boole [Wikimedia]

Boolean variables in Python represent “logical values”

Domain {False, True}

Example

b = True # Variable with value True

(35)

Relational Operators

x < y (smaller than)

number type × number type → {False, True}

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 16 / 36

(36)

Relational Operators

x < y (smaller than)

b = (1 < 3) # b =

(37)

Relational Operators

x < y (smaller than)

b = (1 < 3) # b = True

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 16 / 36

(38)

Relational Operators

x >= y (greater than)

x = 0

b = (x >= 3) # b =

(39)

Relational Operators

x >= y (greater than)

x = 0

b = (x >= 3) # b = False

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 16 / 36

(40)

Relational Operators

x == y (equals)

x = 4

b = (x % 3 == 1) # b =

(41)

Relational Operators

x == y (equals)

x = 4

b = (x % 3 == 1) # b = True

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 16 / 36

(42)

Relational Operators

x != y (unequal to)

x = 1

b = (x != 2 * x - 1) # b =

(43)

Relational Operators

x != y (unequal to)

x = 1

b = (x != 2 * x - 1) # b = False

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 16 / 36

(44)

Logical Values

Boolean Functions and Logical Operators

(45)

Boolean Functions in Mathematics

Boolean function

f : {F, T} 2 → {F, T}

F corresponds to “false”

T corresponds to “true”

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 17 / 36

(46)

ab

“logical and”

f : {F, T} 2 → {F, T}

F corresponds to “false”

T corresponds to “true”

a b ab

F F F

F T F

T F F

T T T

(47)

Logical Operator and

a and b (logical and)

{False, True} × {False, True}{False, True}

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 19 / 36

(48)

Logical Operator and

a and b (logical and)

n = -1 p = 3

c = (n < 0) and (0 < p) # c =

(49)

Logical Operator and

a and b (logical and)

n = -1 p = 3

c = (n < 0) and (0 < p) # c = True

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 19 / 36

(50)

ab

“logical or”

f : {F, T} 2 → {F, T}

F corresponds to “false”

T corresponds to “true”

a b ab

F F F

F T T

T F T

T T T

(51)

ab

“logical or”

f : {F, T} 2 → {F, T}

F corresponds to “false”

T corresponds to “true”

a b ab

F F F

F T T

T F T

T T T

The logical or is always inclusive: a or b or both

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 20 / 36

(52)

Logical Operator or

a or b (logical or)

{False, True} × {False, True}{False, True}

(53)

Logical Operator or

a or b (logical or)

n = 1 p = 0

c = (n < 0) or (0 < p) # c =

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 21 / 36

(54)

Logical Operator or

a or b (logical or)

n = 1 p = 0

c = (n < 0) or (0 < p) # c = False

(55)

¬b

“logical not”

f : {F, T} → {F, T}

F corresponds to “false”

T corresponds to “true”

b ¬b

F T T F

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 22 / 36

(56)

Logical Operator not

not b (logical not)

{False, True}{False, True}

(57)

Logical Operator not

not b (logical not)

n = 1

a = not (n < 0) # a =

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 23 / 36

(58)

Logical Operator not

not b (logical not)

n = 1

a = not (n < 0) # a = True

(59)

Logical Values

Precedences

(60)

Precedences

not b and a

m

(not b) and a

(61)

Precedences

not b and a m

(not b) and a

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 24 / 36

(62)

Precedences

a and b or c and d

m

(a and b) or (c and d)

(63)

Precedences

a and b or c and d m

(a and b) or (c and d)

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 24 / 36

(64)

Precedences

a or b and c or d

m

a or (b and c) or d

(65)

Precedences

a or b and c or d m

a or (b and c) or d

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 24 / 36

(66)

Precedences

b = 7 + x < y and y != 3 * z or not b

Binary arithmetic operators bind the strongest (multiplication and division first, then addition and subtraction)

These bind stronger than relational operators (and first, then or) These bind stronger than the unary logical operator not

These bind stronger than binary logical operators (and first, then or) These bind stronger than the assignment operator

It is often useful to use parentheses even if redundant

(67)

Precedences

b = (7 + x) < y and y != (3 * z) or not b

Binary arithmetic operators bind the strongest (multiplication and division first, then addition and subtraction)

These bind stronger than relational operators (and first, then or) These bind stronger than the unary logical operator not

These bind stronger than binary logical operators (and first, then or) These bind stronger than the assignment operator

It is often useful to use parentheses even if redundant

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 25 / 36

(68)

Precedences

b = ((7 + x) < y) and (y != (3 * z)) or not b

Binary arithmetic operators bind the strongest (multiplication and division first, then addition and subtraction)

These bind stronger than relational operators (and first, then or)

These bind stronger than the unary logical operator not

These bind stronger than binary logical operators (and first, then or) These bind stronger than the assignment operator

It is often useful to use parentheses even if redundant

(69)

Precedences

b = ((7 + x) < y) and (y != (3 * z)) or (not b)

Binary arithmetic operators bind the strongest (multiplication and division first, then addition and subtraction)

These bind stronger than relational operators (and first, then or) These bind stronger than the unary logical operator not

These bind stronger than binary logical operators (and first, then or) These bind stronger than the assignment operator

It is often useful to use parentheses even if redundant

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 25 / 36

(70)

Precedences

b = (((7 + x) < y) and (y != (3 * z))) or (not b)

Binary arithmetic operators bind the strongest (multiplication and division first, then addition and subtraction)

These bind stronger than relational operators (and first, then or) These bind stronger than the unary logical operator not

These bind stronger than binary logical operators (and first, then or)

These bind stronger than the assignment operator

It is often useful to use parentheses even if redundant

(71)

Precedences

b = ((((7 + x) < y) and (y != (3 * z))) or (not b))

Binary arithmetic operators bind the strongest (multiplication and division first, then addition and subtraction)

These bind stronger than relational operators (and first, then or) These bind stronger than the unary logical operator not

These bind stronger than binary logical operators (and first, then or) These bind stronger than the assignment operator

It is often useful to use parentheses even if redundant

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 25 / 36

(72)

Precedences

b = ((((7 + x) < y) and (y != (3 * z))) or (not b))

Binary arithmetic operators bind the strongest (multiplication and division first, then addition and subtraction)

These bind stronger than relational operators (and first, then or) These bind stronger than the unary logical operator not

These bind stronger than binary logical operators (and first, then or) These bind stronger than the assignment operator

It is often useful to use parentheses even if redundant

(73)

Control Structures

(74)

Control Flow

So far. . .

Up to now linear (from top to bottom)

for loop to repeat blocks

x = int(input("Input: "))

for i in range(1, x+1):

print(i*i)

(75)

Control Flow

So far. . .

Up to now linear (from top to bottom) for loop to repeat blocks

x = int(input("Input: "))

for i in range(1, x+1):

print(i*i)

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 26 / 36

(76)

Control Structures

Selection Statements

(77)

Selection Statements

Implement branches if statement if-else statement

if-elif-else statement (later)

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 27 / 36

(78)

if Statement

if condition:

statement

x = int(input("Input: ")) if x % 2 == 0:

print("even")

If condition is true,

then statement is executed statement:

arbitrary statement

body of the if -Statement

condition: Boolean expression

(79)

if Statement

if condition:

statement

x = int(input("Input: ")) if x % 2 == 0:

print("even")

If condition is true,

then statement is executed statement:

arbitrary statement body of the if -Statement condition: Boolean expression

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 28 / 36

(80)

if Statement

if condition:

statement

x = int(input("Input: ")) if x % 2 == 0:

print("even")

If condition is true,

then statement is executed

statement:

arbitrary statement

body of the if -Statement

condition: Boolean expression

(81)

if Statement

if condition:

statement

x = int(input("Input: ")) if x % 2 == 0:

print("even")

If condition is true,

then statement is executed statement:

arbitrary statement body of the if -Statement condition: Boolean expression

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 28 / 36

(82)

if-else Statement

if condition:

statement1 else:

statement2

x = int(input("Input: ")) if x % 2 == 0:

print("even") else:

print("odd")

If condition is true,

then statement1 is executed, otherwise statement2 is executed

condition: Boolean expression statement1:

body of the if-branch statement2:

body of the else-branch

(83)

if-else Statement

if condition:

statement1 else:

statement2

x = int(input("Input: ")) if x % 2 == 0:

print("even") else:

print("odd")

If condition is true,

then statement1 is executed, otherwise statement2 is executed

condition: Boolean expression statement1:

body of the if-branch statement2:

body of the else-branch

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 29 / 36

(84)

if-else Statement

if condition:

statement1 else:

statement2

x = int(input("Input: ")) if x % 2 == 0:

print("even") else:

print("odd")

If condition is true,

then statement1 is executed, otherwise statement2 is executed

condition: Boolean expression statement1:

body of the if-branch statement2:

body of the else-branch

(85)

if-else Statement

if condition:

statement1 else:

statement2

x = int(input("Input: ")) if x % 2 == 0:

print("even") else:

print("odd")

If condition is true,

then statement1 is executed, otherwise statement2 is executed

condition: Boolean expression statement1:

body of the if-branch statement2:

body of the else-branch

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 29 / 36

(86)

Layout

x = int(input("Input: "))

if x % 2 == 0:

print("even") else:

print("odd")

(87)

Layout

x = int(input("Input: "))

if x % 2 == 0:

print("even") else:

print("odd")

Indentation

Indentation

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 30 / 36

(88)

if-else Statement

Attention when using == or =

(89)

if-else Statement

Attention when using == or =

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 31 / 36

(90)

Control Structures

while Loops

(91)

while Loops

while condition:

statement

statement:

arbitrary statement body of the while loop condition: Boolean expression

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 32 / 36

(92)

while Loops

while condition:

statement Indentation

statement:

arbitrary statement

body of the while loop

condition: Boolean expression

(93)

while Loops

while condition:

statement

condition is evaluated True : iteration starts

statement is executed False : while loop ends

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 33 / 36

(94)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

(95)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

i = 1

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 34 / 36

(96)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

i = 1 i <= 2?

(97)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

i = 1 true

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 34 / 36

(98)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

i = 1 true s = 1

(99)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

i = 1 true s = 1

i = 2

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 34 / 36

(100)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

i = 1 true s = 1

i = 2 i <= 2?

(101)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

i = 1 true s = 1

i = 2 true

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 34 / 36

(102)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

i = 1 true s = 1

i = 2 true s = 3

(103)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

i = 1 true s = 1

i = 2 true s = 3

i = 3

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 34 / 36

(104)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

i = 1 true s = 1

i = 2 true s = 3

i = 3 i <= 2?

(105)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

i = 1 true s = 1

i = 2 true s = 3

i = 3 false

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 34 / 36

(106)

while Loops

s = 0 i = 1

while i <= 2:

s = s + i i = i + 1

i condition s

i = 1 true s = 1

i = 2 true s = 3

i = 3 false s = 3

(107)

Incrementation of Variables

Use simplified syntax for changing values of variables

n = n + 1 is written as n += 1 n = n + i is written as n += i n = n - 15 is written as n -= 15 n = n * j is written as n *= j n = n ** 4 is written as n **= 4 . . .

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 35 / 36

(108)

Incrementation of Variables

Use simplified syntax for changing values of variables n = n + 1 is written as n += 1

n = n + i is written as n += i

n = n - 15 is written as n -= 15

n = n * j is written as n *= j

n = n ** 4 is written as n **= 4

. . .

(109)

Incrementation of Variables

Use simplified syntax for changing values of variables n = n + 1 is written as n += 1

n = n + i is written as n += i

n = n - 15 is written as n -= 15 n = n * j is written as n *= j n = n ** 4 is written as n **= 4 . . .

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 35 / 36

(110)

Incrementation of Variables

Use simplified syntax for changing values of variables n = n + 1 is written as n += 1

n = n + i is written as n += i n = n - 15 is written as n -= 15

n = n * j is written as n *= j

n = n ** 4 is written as n **= 4

. . .

(111)

Incrementation of Variables

Use simplified syntax for changing values of variables n = n + 1 is written as n += 1

n = n + i is written as n += i n = n - 15 is written as n -= 15 n = n * j is written as n *= j

n = n ** 4 is written as n **= 4 . . .

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 35 / 36

(112)

Incrementation of Variables

Use simplified syntax for changing values of variables n = n + 1 is written as n += 1

n = n + i is written as n += i n = n - 15 is written as n -= 15 n = n * j is written as n *= j n = n ** 4 is written as n **= 4

. . .

(113)

Incrementation of Variables

Use simplified syntax for changing values of variables n = n + 1 is written as n += 1

n = n + i is written as n += i n = n - 15 is written as n -= 15 n = n * j is written as n *= j n = n ** 4 is written as n **= 4 . . .

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 35 / 36

(114)

The Jump Statements break

break

Immediately leave the enclosing loop

Useful in order to be able to break a loop “in the middle”

s = 0

while True:

x = int(input("Enter a positive number, abort with 0: ")) if x == 0:

break

s += x

print(s)

(115)

The Jump Statements break

break

Immediately leave the enclosing loop

Useful in order to be able to break a loop “in the middle”

s = 0

while True:

x = int(input("Enter a positive number, abort with 0: ")) if x == 0:

break s += x print(s)

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 36 / 36

(116)

Thanks

for your attention

Referenzen

ÄHNLICHE DOKUMENTE

Basics of Computer Science for Human Medicine – Introduction to the Course Autumn 2021 Böckenhauer, Komm 1 / 36.?.

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 2 / 21...

Basics of Computer Science for Human Medicine – Lists, strings, loops Autumn 2021 Böckenhauer, Komm 1 / 21.

Basics of Computer Science for Human Medicine – Control Structures Autumn 2021 Böckenhauer, Komm 7 / 36.

Basics of Computer Science for Human Medicine – Functions and return values Autumn 2021 Böckenhauer, Komm 2 / 26... but can in general not be

Basics of Computer Science for Human Medicine – Functions and return values Autumn 2021 Böckenhauer, Komm 1 / 26.

Basics of Computer Science for Human Medicine – Reading in Data and Sorting Autumn 2021 Böckenhauer, Komm 7 / 28... Then the left algorithm is faster ï Loop is left after

Basics of Computer Science for Human Medicine – numpy, matplotlib Autumn 2021 Böckenhauer, Komm 4 /