0% found this document useful (0 votes)
8 views14 pages

Lecture 04

Uploaded by

Maxi Brad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views14 pages

Lecture 04

Uploaded by

Maxi Brad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Computer Science 1001

Lecture 4

Lecture Outline

• Flow control: selection

– CS1001 Lecture 4 –
Control Logic

• So far we have seen programs with statements


executed sequentially from top to bottom.

• However, many algorithms will require:


– control over the order in which statements are
executed;
– control over which of a group of statements is to
be executed; or
– control over whether certain statements are
executed at all.

– CS1001 Lecture 4 – 1
What happens if a negative value is
entered?
## Calculate the circumference of a circle from its radius
## Step 1: Ask for the radius
## Step 2: Apply the circumference formula
## Step 3: Print out the results

from math import *

radius = input("Enter the radius: ")


radius = float(radius)

circumference = 2 * pi * radius

print("The circumference is",circumference)

– CS1001 Lecture 4 – 2
Unexpected input

• In the above example, we would like to check the


value that the user enters.

• If the radius is non-negative then we can go ahead


and calculate the circumference. That is,

if radius >= 0 then circumference = 2 * pi * r

• This can be illustrated in a flowchart as:

False
radius >= 0?

True

circumference = 2*pi*r
print("The circumference...

– CS1001 Lecture 4 – 3
Conditional control flow

• In python this can be written as:

if radius >= 0:
circumference = 2 * pi * radius
print("The circumference is",circumference)

• The if statement is a compound statement that


has a header (the if line) and a statement block.

• The statement block contains statements that are


to be executed if the condition in the header is true.

• Note that indentation is required within the


statement block.

– CS1001 Lecture 4 – 4
Logical/Boolean expressions

• A logical expression is an expression that evaluates


to a Boolean value (True or False).

• To write logical expressions we often require


relational operators. For example, the following
operators are available in Python:
Operator Name Example Result
< less than 8<0 False
<= less than or -1<=2 True
equal to
> greater than 7>9 False
>= greater than or 6>=6 True
equal to
== equal to 3==-3 False
!= not equal to ‘hi’!=‘bye’ True

• Note that the “equal to” operator has two equal


signs. A single equal sign is for assignment.

• Relational operators can also be used on strings.

– CS1001 Lecture 4 – 5
If statements

• An if statement can be used if a given sequence of


statements is to be executed or bypassed depending
on the result of a logical expression. In general we
have:

if boolean-expression:
statement(s)-if-expression-is-true

• An if statement can be illustrated as:

Boolean False
expression

True

Statement(s)

– CS1001 Lecture 4 – 6
If-else statements
• If the condition in an if statement (radius>=0 for
example) is False then the statements within the
if are not executed, and execution continues from
the next statement outside of the if (not indented).

• In the case where we wish to execute a group


of statements under one condition, but a different
group of statements otherwise then the if-else
statement can be used:
if boolean-expression:
statement(s)-for-the-true-case
else:
statement(s)-for-the-false-case

True False
Boolean
expression

Statement(s) Statement(s)
for true case for false case

– CS1001 Lecture 4 – 7
If-else statements

• For the circle circumference case we could use an


if-else statement as follows:

## Calculate the circumference of a circle from its radius


## Step 1: Ask for the radius
## Step 2: Apply the circumference formula
## Step 3: Print out the results

from math import *

radius = input("Enter the radius: ")


radius = float(radius)

if radius >= 0:
circumference = 2 * pi * radius
print("The circumference is",circumference)
else:
print("Negative input")

– CS1001 Lecture 4 – 8
Example: subtraction

• Suppose we would like to write a Python program


to allow a first-grader to practice subtraction.

• To begin, we could use the following algorithm:


1. Generate two single-digit integers (num1 and
num2).
2. If num1 < num2 then swap num1 with num2.
3. Ask the student to answer the question
"What is num1-num2?.
4. Check the student’s answer and inform them
whether it was correct, and if it was incorrect,
display the correct answer.

– CS1001 Lecture 4 – 9
The random module

• To generate the two integers, we can make use of a


function in the random module.

• The function randint(a,b) returns a random


integer n such that a <= n <= b.

• The random module also contains a function called


random() that returns a random floating point
number in the range [0.0, 1.0).

• For a list of functions available in the


random module see https://docs.python.org/
3/library/random.html#module-random.

– CS1001 Lecture 4 – 10
Example: subtraction
# Generate two single-digit integers num1 and num2
# If num1 < num2, swap num1 with num2
# Ask the student to answer ’What is num1 - num2?
# Check the student’s answer and display whether the answer is correct

from random import *

# generate two random integers


num1 = randint(0,9)
num2 = randint(0,9)

# swap the numbers if necessary


if num1 < num2:
temp = num1
num1 = num2
num2 = temp

answer = int(input("What is " + str(num1) + " - " + str(num2) + "?"))


result = num1 - num2

if answer == result :
print("Correct!", num1, "-", num2, "=", answer)
else:
print("Incorrect", num1, "-", num2, "is not", answer,
"it is", result)

Sample output:
What is 9 - 7?2
Correct! 9 - 7 = 2

– CS1001 Lecture 4 – 11
What is 5 - 4?2
Incorrect 5 - 4 is not 2 it is 1

– CS1001 Lecture 4 – 12
Logical operators
• Logical expressions can be combined with the use
of logical operators not, and, and or.
Operator Example Result
and (2<5) and (2>7) False
or (2<5) or (2>7) True
not not(2==5) True

• The following truth tables illustrate the behaviour


of these operators.

A B A and B A B A or B
T T T T T T
T F F T F T

F T F F T T
F F F F F F

A not A
T F
F T

– CS1001 Lecture 4 – 13

You might also like