3. Python Decision Making
Decision making structures require that the
programmer specify one or more conditions to be
evaluated or tested by the program, along with a
statement or statements to be executed if the condition is
determined to be true, and optionally, other statements to
be executed if the condition is determined to be false.
4. Types of decision making
statements
IF statements
IF...ELSE statements
Nested IF statements
6. IF...ELSE statements Flow Diagram:
Test Exp
FALSE
If Block
TRUE
Body of Else
EXIT
The else statement is an optional statement and there could be at most only one
else statement following if .
7. LOOPS
A loop statement allows us to execute a statement or
group of statements multiple times
Python programming language provides following types
of loops to handle looping requirements
While loop
For loop
8. While Loop
Example:
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
print "Good bye!"
9. For Loop
If a sequence contains an expression list, it is
evaluated first. Then, the first item in the sequence is
assigned to the iterating variable iterating_var. Next, the
statements block is executed. Each item in the list is
assigned to iterating_var, and the statement(s) block is
executed until the entire sequence is exhausted.
Example:
for letter in 'Python':
print 'Current Letter :', letter
String
10. Loop Control Statements:
Loop control statements change execution from its
normal sequence. When execution leaves a scope, all
automatic objects that were created in that scope are
destroyed.
break statement
continue statement
pass statement
12. continue statement
The continue statement in Python returns the control
to the beginning of the while loop. The continue
statement rejects all the remaining statements in the
current iteration of the loop and moves the control back
to the top of the loop.
for letter in 'Python':
if letter == 'h':
continue
continue statement
print 'Current Letter :', letter
13. pass statement
The pass statement in Python is used when a statement
is required syntactically but you do not want any
command or code to execute.
The pass statement is a null operation; nothing happens
when it executes.
for letter in 'Python':
if letter == 'h':
pass
print 'This is pass block'
print 'Current Letter :', letter