0% found this document useful (0 votes)
4 views

Intro to CS Lec05

The document covers the basics of conditionals and flow of execution in programming, particularly in Python. It explains the structure of conditional statements, including simple conditionals, alternative executions, chained conditionals, and nested conditionals. Additionally, it addresses common errors in conditional logic and the importance of indentation in Python code.

Uploaded by

Firo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Intro to CS Lec05

The document covers the basics of conditionals and flow of execution in programming, particularly in Python. It explains the structure of conditional statements, including simple conditionals, alternative executions, chained conditionals, and nested conditionals. Additionally, it addresses common errors in conditional logic and the importance of indentation in Python code.

Uploaded by

Firo
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Conditionals

Variables: Scratch
Review

def print_twice(param):
print(param)
print(param)
Review

>>> print_twice(1)
>>> print_twice(‘test’)
>>> print_twice(math.pi)
>>> print_twice(‘test ’ * 4)
Flow of Execution

Function definitions do not alter the flow of


execution of the program, but statements
inside the function are not executed until
the function is called.
Review: Flow of Execution

Function calls are like a detour in


the flow of execution.
Flowcharts
The start or end of the program. There may be more than
one way to complete the algorithm and there may be more
than one end box.
A process, that is doing something for example calculating
something.
An input or output, for example:
Input num1

A decision, YES or NO, or a choice of paths, for example:


Is it a weekday?

When a flowchart will not fit onto a single page we use this
shape to show how the sections of the flowchart connect
together.
Branching programs
Review: Boolean Expressions

A Boolean expression is an
expression that evaluates to produce
a result which is a Boolean value.
Review: Boolean Expressions

>>> 4%2 == 0 True

>>> 12 and True True


Review: Boolean Expressions

>>> 1 == True True

>>> 12 == True False


Truth tables
‘till now…

Straight line programs: executed one


statement after another in order in which
they appear and stop when they are out
of statements
Conditionals
Branching programs

The simplest branching statement is a


conditional
Conditional Execution

Writing programs, we almost always


need the ability to check conditions and
change the behavior of the program
accordingly.
Conditional statements
Three parts
 A test: an expression that evaluates to
True/False
 A block of code that is executed to True
 [Optional] block of code that is executed
if the test evaluates to False
Code

Test

True False
Block Block

Code
Simple conditional statement

if(<conditional statements>):
<block of code>
Tea brewing algorithm

Wait 5min
if ( tea boils ):
turn off the stove
Indentation

 Python uses indentation to delineate

blocks of code
 Most other programming languages use {}
Start

example
Input number
Divisible by two Pseudocode
Accept integer input from user
Is
Divisible?

If input divisible by two: True False

output ‘YES’ output ‘YES’

End
Example

>>> dividend, divisor = eval(input(‘Enter number: ’))

>>> print(dividend/divisor)

What problem do you anticipate?


Example

>>> dividend, divisor = eval(input(‘Enter number: ’))


>>> if (divisor != 0):
print(dividend/divisor)
Cases

if(1): if(0):
print(‘one’) print(‘zero’)
Alternative Executions

if(<conditional statements>):
<if block>
else:
<else block>
Odd–Even program
Input: x

if (x is divisible by 2):
print(‘x is even’)
else:
print(‘x is odd’)
Chained conditionals
if(<conditional statements>):
<if block>
elif(<conditional statements>):
<elif block>
else:
<else block>
Nested Conditionals
if(<conditional statements>):
if(<conditional statements>):
<if block>
else:
<else block>
else:
<else block>
Nested Conditionals

if 0 < x:
if x % 2 == 0:
print(‘x is positive even number’)
Common Errors
Common Errors
if((Value > 0) or (Value <= 10)):
print(Value)

if((Value > 0) and (Value <= 10)):


print(Value)
Common Errors

if((Value < 0) and (Value > 10)):


print(Value)
Floating point

if((1.11 - 1.10) == (2.11 -2.10)):


print(‘’)

You might also like