0% found this document useful (0 votes)
26 views20 pages

CENG240-2021 Week5 Conditional and Repetitive Execution

Uploaded by

brawlstarsoc
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)
26 views20 pages

CENG240-2021 Week5 Conditional and Repetitive Execution

Uploaded by

brawlstarsoc
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/ 20

METU Computer Engineering

CEng 240 – Spring 2021


Week 5
Sinan Kalkan

Conditional and Repetitive Execution

Disclaimer: Figures without reference are from either from “Introduction to programming concepts
with case studies in Python” or “Programming with Python for Engineers”, which are both co-authored
by me.
This Week
METU Computer Engineering

 Conditional Execution
 if statement
 Conditional expression

 Repetitive Execution
 while/for statement
 break/continue statements
 Set/list comprehension

2021 S. Kalkan - CEng 240 2


METU Computer Engineering

CONDITIONAL EXECUTION

2021 S. Kalkan - CEng 240 3


Conditional Statements in Python
METU Computer Engineering

 the syntax is important!


 indentation is extremely important!
 “else”-part can be omitted!

2021 S. Kalkan - CEng 240 4


Multiple If Statements
in Python
METU Computer Engineering

2021 S. Kalkan - CEng 240 5


Example
METU Computer Engineering

2021 S. Kalkan - CEng 240 6


Multiple Nested If Statements in
Python
METU Computer Engineering

2021 S. Kalkan - CEng 240 7


Example
METU Computer Engineering

 Finding the maximum of two numbers


 Finding the maximum of three numbers

2021 S. Kalkan - CEng 240 8


Conditional Expression in Python
METU Computer Engineering

<exp-1> if <cond-exp> else <exp-2>

Note that this is an expression not a


statement!!

2021 S. Kalkan - CEng 240 9


METU Computer Engineering

REPETITIVE EXECUTION

2021 S. Kalkan - CEng 240 10


while statement
METU Computer Engineering

2021 S. Kalkan - CEng 240 11


for statement
METU Computer Engineering

2021 S. Kalkan - CEng 240 12


Example
METU Computer Engineering

 Count the occurrences of characters in a


string, e.g.
"aaaaaaxxxxmyyyaaaassssssssstttuivvvv”

2021 S. Kalkan - CEng 240 13


Break statements
METU Computer Engineering

2021 S. Kalkan - CEng 240 14


“break” example
METU Computer Engineering

2021 S. Kalkan - CEng 240 15


Continue statements
METU Computer Engineering

 <var> will
point to
the next
item in the
list.

2021 S. Kalkan - CEng 240 16


Set and list comprehension
METU Computer Engineering

 [x**3 for x in range(8)]


 {x**3 for x in range(8)}

2021 S. Kalkan - CEng 240 17


Exercises from the book
METU Computer Engineering

 https://pp4e-workbook.github.io/

2021 S. Kalkan - CEng 240 18


Final Words:
Important Concepts
METU Computer Engineering

 Variables; Aliasing problem; Naming Variables


 Actions for interacting with the environment
 Comments, pass statements
 Actions in packages (libraries)
 Writing your actions (interpreter vs.
script/modules)

2021 S. Kalkan - CEng 240 19


METU Computer Engineering

THAT’S ALL FOLKS!


STAY HEALTHY

2021 S. Kalkan - CEng 240 20

You might also like