Top Banner
Fundamentals of Programming (Python) Control Structures Ali Taheri Sharif University of Technology Spring 2018 Some slides have been adapted from “Python: How to Program – 1 st Edition”
18

Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Oct 08, 2020

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Fundamentals of Programming(Python)

Control Structures

Ali TaheriSharif University of Technology

Spring 2018

Some slides have been adapted from “Python: How to Program – 1st Edition”

Page 2: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Outline1. Control Structures

2. Boolean Expressions

3. Conditionals

4. While Loop

5. For Loop

2ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 3: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Control StructuresSequential order◦ Statements are executed in the order they are written

Transfer of control◦ A program executes a statement other than the following one

◦ Do using control structures

3 control structures◦ Sequential structure

◦ Selection structure (Conditionals)

◦ Repetition structure (Loops)

3ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 4: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Boolean ExpressionsOperation Meaning

x == y Tests if x is equal to y

x != y ……... x is not equal to y

x > y ……... x is greater than y

x < y ……... x is less than y

x >= y ……... x is greater than or equal to y

x <= y ……... x is less than or equal to y

4ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Operation Meaning

x and y Tests if both x and y are True

x or y ……... either x or y are True

not x ……... x is False

Page 5: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Conditionals

if structure◦ It is a single entry, single exit structure

◦ Allows a program to perform an action only if a statement is true

◦ Otherwise the action is skipped

5ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Condition

Action(s)false

true

Page 6: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Conditionals

Example◦ Find min and max of two numbers

6ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

x = input("Enter first number: ")

min = float(x)

y = input("Enter second number: ")

max = float(y)

if x > y:

min, max = max, min

print("Min:", min)

print("Max:", max)

Page 7: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Conditionals

if/else structure◦ Double selection statement

◦ Allows the programmer to perform an action when a condition is true

◦ An alternate action is preformed when the action is false

7ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Condition

Action(s) of True CaseAction(s) of False Case

false true

Page 8: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Conditionals

Example◦ Find min and max of two numbers

8ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

x = input("Enter first number: ")

x = float(x)

y = input("Enter second number: ")

y = float(y)

if x < y:

print("Min:", x)

print("Max:", y)

else:

print("Min:", y)

print("Max:", x)

Page 9: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Conditionals

if/elif/else structure◦ Multiple selection statement

◦ This is used in place of nested if/else statements

◦ The final else statement is optional◦ It is used as a default action should all other

statements be false

9ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

condition a true

false

.

.

.

false

false

condition z

default action(s)

true

true

condition b

case a action(s)

case b action(s)

case z action(s)

Page 10: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Conditionals

Example◦ Find min and max of two numbers (check for equality)

10ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

x = input("Enter first number: ")

x = float(x)

y = input("Enter second number: ")

y = float(y)

if x < y:

print("Min:", x)

print("Max:", y)

elif x > y:

print("Min:", y)

print("Max:", x)

else:

print("Min=Max=", x)

Page 11: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Repetition StructuresRepetition Structures (Loops)◦ Allow a program to repeat an action while a statement is true

Deterministic Repetition◦ Implemented using for loop

Non-Deterministic Repetition◦ Implemented using while loop

11ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 12: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Repetition Structureswhile loop◦ The action is contained within the body of the loop

◦ Can be one or more than one action

◦ Condition should evaluate to false at some point

◦ Creates a infinite loop and program hangs

12ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Condition

Action(s)

True

False

Page 13: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

While Loop

Example◦ Class average grade (end with sentinel value)

13ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

total = 0.0

counter = 0

grade = float(input("Enter next grade: "))

while grade >= 0:

total += grade

counter += 1

grade = float(input("Enter next grade: "))

average = total / counter

print("Average Grade: %.2f" % average)

Page 14: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

While LoopThe break statement◦ Used to make a loop stop looping

◦ The loop is exited and no more loop code is executed

14ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

total = 0.0

counter = 0

while True:

grade = float(input("Enter next grade: "))

if grade < 0:

break

total += grade

counter += 1

average = total / counter

print("Average Grade: %.2f" % average)

Page 15: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Repetition StructuresCounter-Controlled Repetition◦ The counter

◦ A named variable to control the loop

◦ Initial value

◦ That which the counter starts at

◦ Increment

◦ Modifying the counter to make the loop eventually terminate

◦ Condition

◦ The test that the counter must pass in order to continue looping

15ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

Page 16: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

Repetition Structuresfor loop◦ Used to implement counter-

controlled repetition structure

16

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

counter < max

Action(s)

True

False

counter ← initial

counter ← counter + step

for counter in range(initial, max, step):

# Action(s)

Page 17: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

For Loop

Example◦ Class average grade (with known number of grades)

17ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

total = 0.0

number = int(input("Enter number of grades: "))

for counter in range(0, number, 1):

grade = float(input("Enter grade %d: " % (counter + 1)))

total += grade

average = total / number

print("Average Grade: %.2f" % average)

Page 18: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/2/ce153-4/resources... · Control Structures Sequential order Statements are executed in the order

For LoopThe continue statement◦ Used to continue the looping process

◦ All following actions in the loop are not executed◦ But the loop will continue to run

Example◦ Even numbers between 0 and 50 not dividable by 3

18ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]Spring 2018

for counter in range(0, 51, 2):

if counter % 3 == 0:

continue

print(counter, end=", ")