18
Fundamentals of Programming (Python) Control Structures Sina Sajadmanesh Sharif University of Technology Fall 2017 Some slides have been adapted from “Python: How to Program – 1 st Edition”

Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

  • Upload
    others

  • View
    12

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

Fundamentals of Programming(Python)

Control Structures

Sina SajadmaneshSharif University of Technology

Fall 2017

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/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

Outline1. Control Structures

2. Boolean Expressions

3. Conditionals

4. While Loop

5. For Loop

2SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 3: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

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)

3SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 4: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

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

4SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

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/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

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

5SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Condition

Action(s)false

true

Page 6: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

Conditionals

Example◦ Find min and max of two numbers

6SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

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/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

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

7SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

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/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

Conditionals

Example◦ Find min and max of two numbers

8SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

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/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

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

9SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

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/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

Conditionals

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

10SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

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/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

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

11SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 12: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

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

12SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Condition

Action(s)

True

False

Page 13: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

While Loop

Example◦ Class average grade (end with sentinel value)

13SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

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/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

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

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

14SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

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/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

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

15SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

Page 16: Fundamentals of Programming (Python) Control Structuresce.sharif.edu/courses/96-97/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

Repetition Structuresfor loop◦ Used to implement counter-

controlled repetition structure

16

SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

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/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

For Loop

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

17SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

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/1/ce153-12/resources...While Loop 5. For Loop Fall 2017 SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING

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

18SINA SAJADMANESH - FUNDAMENTALS OF PROGRAMMING [PYTHON]Fall 2017

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

if counter % 3 == 0:

continue

print(counter, end=", ")