Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

CONDITIONALS

REVIEW: BRANCHING
PROGRAMS

The simplest branching statement is a


conditional
REVIEW: CONDITIONAL
EXECUTION
Writing programs, we almost always need
the ability to check conditions and change
the behavior of the program accordingly.
Code

Test

True False
Block Block

Code
GRADE
Score >=85 ==> A
Score >=70 but <85 ==> B
Score >=50 but <70 ==> C
Score >=40 but <50 ==> D
Score < 40 ==> F
DEFENSIVE PROGRAMMING

Defensive programming is a form of defensive


design intended to ensure the continuing
function of a piece of software under unforeseen
circumstances.
INPUT

>>> name = input(‘Enter your name: ’)


FUNCTION
Check the validity of passed arguments

def add(a,b):
print(a+b)
assert
def add(a,b):
# check if a is int/float
# check if b is int/float
print(a+b)
UPDATING VARIABLES
>>> x = 5
# increment by one
>>> x = x + 1 >>> x += 1
# decrement by one
>>> x = x - 1 >>> x += 1
CASE STUDY: AVERAGE OF
THREE NUMBERS
 Average of 5

 Average of 20

 Average of 1000

 Average of unknown number


CASE STUDY: FIND FACTORS
OF A GIVEN NUMBER
 Given the number is 2

 Given the number is 10

 Given the number is 124,989 …


ITERATION
Computers/ Robots/ Computing machines
are often used to automate repetitive tasks.
REPETITION STRUCTURES

The repetition structure causes a statement


or set of statements to execute repeatedly.
ITERATION
A generic iteration mechanism
begins with a test. CHECK

If the test evaluates to True,


program executes the body once
Statements

and then goes back to reevaluate


the test.
LOOPS

 A loop repeats a sequence of statements

 Two types

 while loop

 for loop
THE while LOOP

The while Loop is a Pretest Loop, which means


it tests its condition before performing an
iteration.
THE while LOOP

while(condition):
<statements>
THE while LOOP
More formally, here is the flow of execution for a while
statement:
I. Evaluate the condition, yielding True or False.
II. If the condition is false, exit the while statement
and continue execution at the next statement.
III. If the condition is true, execute the body
EXAMPLE 1

Print numbers from 1 to 10


EXAMPLE 2: ACCUMULATOR
LOOP
Add numbers from 1 to 100
break STATEMENT

Sometimes you don’t know it’s time to end


a loop until you get half way through the
body.
break STATEMENT

When break statement is executed, the


current loop iteration is stopped and the
loop is exited.
continue STATEMENT
When continue statement is executed, the
current loop iteration is skipped and the
loop execution resumes with the next
iteration of the current, innermost loop
statement.
pass STATEMENT

In Python, every def statement, if


statement, or loop statement must have a
body (i.e., a nonempty indented code
block).
pass STATEMENT

if n % 2 == 0:
pass
else:
print(n)
INFINITE LOOPS

while(True):
print(‘hi’)
INFINITE LOOPS

x=1
while(x > 0):
x += 1
COMMON
PITFALLS
UNINTENTIONAL INFINITE
LOOP
x = 10
while(x > 0):
x += 1
OFF-BY-ONE ERROR(OB1)
A logic error that occurs in
programming when an iterative loop
iterates one time too many or too few.
OB1
Arises when programmer makes mistakes
such as
 Fails to take account that a sequence starts at
zero rather than one
 Using “less than or equal to” in place of “less
than” …

You might also like