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

13 Conditions

Uploaded by

vy9wmg2mzn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

13 Conditions

Uploaded by

vy9wmg2mzn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Conditional Execution

• What we have learnt in programming so far is


known as a “sequence structure”
• Sequence structures are sets of statements
Sequence Structure that execute in the order in which they
appear
• Unfortunately not all programs can be written
this way…..
Sequence Structure
• Allows your program to “ask a question” and
respond accordingly
Selection Statement • Simplest form – perform an action only if a
certain condition exists
• If the condition is not met, then the action is
not performed
Selection Statement
Selection Statements
in Python
• The trick to writing a selection statement is in
constructing a condition that matches the question
you are trying to ask the computer
• All selection statements must have a condition to
“test”
Boolean Expression • Think of conditions as “yes or no” questions. They
can only be answered by one of two options – “True”
or “False “

• Boolean expressions can be used as the condition in
an “if” statement
• They are generally formed using “relational
operators” which allow you to test to see whether a
specific relationship exists between two (or more)
Boolean Expression values
(Cont..) • ALL Boolean expressions boil down to “True” or
“False”
• Programmers often say that the expression
“evaluates” to “True” or “False”


Relational Operators

Identity operator: is
Membership Operator: in
Relational Operators
Relational Operators

Lexicographically ordered!!!
• Adding some logic
• The and keyword
• The or keyword
Boolean Expression • The not keyword
(Cont..)

• The general form of the IF statement is:

The simple • 4 parts:


“IF Statement” • If: reserved word
• Condition: a Boolean expression
• Colon (: ) : part of its syntax
• Block: The statement(s) that must be executed
if the condition is true
• *The block must be indented
The simple
“IF Statement”
(Example) •
The simple
“IF Statement”
(Example) •
• Two possibilities
• The condition determines which gets executed
• The alternatives are called branches
Alternative Execution • The general form is:
IF/ELSE statement
IF/ELSE statement
(Example)
IF/ELSE statement
(Example)
• More than two possibilities
• Need more than branches

Chained Conditionals

• There is no limit to the number of elif statements but only a single


else statement is allowed
• One condition nested within another

Nested Conditionals
(Example)
• One condition nested within another

Nested Conditionals
(Example)
• Write a program to check if a number is positive
• Write a program to check if a number is positive or
negative
• Write a program to check if a number is positive or
Practice Question negative or zero.
• Guess the number !

Nested Conditionals
(Example)
• Provide a way to simplify nested conditionals

Logical operators • Another way to do it using and operator

• Another alternative syntax:

You might also like