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

Lecture 03

Uploaded by

samiabye231
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)
8 views

Lecture 03

Uploaded by

samiabye231
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/ 27

CSEN2315 Programming in

Python I

Lecture 03:
Selection Control Structures
1
Outline
• Relational and Logical Operators
• if-else Statements
• if statement
• if-elif-else statement
• Algorithms

2
Relational and Logical Operators
• A condition is an expression
• Involving relational operators (such as < and >=)
• Logical operators (such as and, or, and not)
• Evaluates to either True or False

• Conditions used to make decisions


• Choose between options
• Control loops
Relational Operators
• Relational operators can be applied to
• Numbers
• Strings

• For strings, the ASCII value determines order of characters


ASCII Values
• ASCII values determine order used to compare strings
with relational operators.
• Associated with keyboard letters, characters, numerals
• ASCII values are numbers ranging from 32 to 126.

• A few ASCII values.


ASCII Values
• The ASCII standard also assigns characters to some
numbers above 126.
• A few of the higher ASCII values.

• Functions chr(n) and ord(str) access ASCII


values
Relational Operators
Condition Evaluation
• A condition is evaluated to True or False
• The result of the evaluated condition can be stored in a variable of
type bool (Boolean):
Condition Evaluation
• Example 1: Determine whether following conditions evaluate to True
or False
Condition Evaluation
• Example 2: Determine whether following conditions evaluate to True
or False
if-else Statements
• Also known as branching structures
• Allow program to decide on course of action
• Based on whether a certain condition is True or False.

• Form:
if-else Statements

## determine the larger of two numbers


Indentation
num1 = float(input('Enter the first number: '))
is
num2 = float(input('Enter the second number: '))
important!
Indentation
if num1 > num2:
is
important!largerValue = num1 #this will be executed if the condition is evaluated to True

else:

largerValue = num2 #this will be executed if the condition is evaluated to False

print('The larger value is ' + str(largerValue) +'.')

12
if-else Statements
• Example: Program finds larger of two numbers input by user.

Flowchart for the if-else statement in the Example .


if-else Statements
• Example : if-else statement in program has relational operators in its
condition.
if Statements

• The else part of an if-else statement can be omitted.


• When the condition is false:
• Execution continues with line after the if statement block
# fig02_01.py
"""Compare integers using if statements and comparison operators."""

print('Enter two integers and I will tell you',


'the relationships they satisfy.')

number1 = int(input('Enter first integer: ')) # read first integer


number2 = int(input('Enter second integer: ')) # read second integer

if number1 == number2:
print(number1, 'is equal to', number2)

if number1 != number2:
print(number1, 'is not equal to', number2)

if number1 < number2:


print(number1, 'is less than', number2)

if number1 > number2:


print(number1, 'is greater than', number2)

if number1 <= number2:


print(number1, 'is less than or equal to', number2)

if number1 >= number2:


print(number1, 'is greater than or equal to', number2)
16
if-elif-else Statement
• Allows for more than two possible alternatives.

17
if-elif-else Statements

## determine the larger of two numbers

num1 = float(input('Enter the first number: '))

num2 = float(input('Enter the second number: '))

if num1 > num2:

print(num1 , 'is larger.' )

elif num1 < num2:

print(num2 , 'is larger.' )

else:

print( 'The two numbers are equal')

18
Algorithms

19
Algorithms
• Any computing problem can be solved by executing a series of actions in a
specific order.
• An algorithm is a step-by-step procedure for solving a problem in terms of:
• the actions to execute and
• the order in which these actions execute
• Specifying the order in which statements (actions) execute in a program is called
program control.
• An algorithm can expressed in two way:
• Pseudocode
• Flowchart

20
Pseudocode
• An informal language that helps you develop algorithms without having to
worry about the strict details of programming language syntax.
• Similar to everyday English.
• Helps you “think out” a program before attempting to write it in a
programming language.
• Pseudocode normally describes only statements representing the actions that
occur (e.g., input, output or calculations) after you convert a program from
pseudocode to Python and run the program.
21
Addition-Program Pseudocode
1. Prompt the user to enter the first integer
2. Input the first integer
3. Prompt the user to enter the second integer
4. Input the second integer
5. Add first integer and second integer, store their sum
6. Display the numbers and their sum

22
Flowchart
• Graphical representation of an algorithm or a part of one.
• Drawn using rectangles, diamonds, rounded rectangles and small
circles connected by arrows called flowlines.

23
Flowchart for Sequential Execution
• Sequential Execution: statements in a program are executed in the
order in which they’re written.

24
if statement Flowchart

25
if-else statement Flowchart

26
if-elif-else statement Flowchart

27

You might also like