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

Module 5: Conditional

statements/Control Structures
Module 5: Conditional statements/
Control Structures

www.cognixia.com
www.cognixia.com
Module 5: Conditional statements/Control Structures

 If Statements
 While construct
 For Statements
 Looping Techniques over List, dictionary
 The range() Function
 Break and continue Statements
 Pass Statements
o Switch statement
Module 5: Conditional statements/Control Structures

 If Statements
What is an if statement?
An if statement tests for a condition, and then responds to that condition. If the condition is true, then
whatever action is listed next gets carried out. You can test for multiple conditions at the same time,
and respond appropriately to each condition.

 if statement
 if..else statements
 nested if statements
 if-elif ladder
Module 5: Conditional statements/Control Structures

The syntax of the if statement is:

 If Statements if expression:
statement(s)
The syntax of the if statement is:
The syntax of the if...elif statement is:
if expression:
if expression1:
statement(s) statement(s)

The syntax of elif expression2:

if..else is: statement(s) elif


expression3:
if expression: statement(s)

statement(s) else:
statement(s)
else:
statement(s)
Module 5: Conditional statements/Control Structures

 While Loop
What is while loop in Python?
The while loop in Python is used to iterate over a block of code as long as the test expression
(condition) is true.
We generally use this loop when we don't know beforehand, the number of times to
iterate.
Python interprets any non-zero value as True. None and 0 are interpreted as False.
Syntax of while Loop in Python n = int(input("Enter n: ")) n while loop with else
while test_expression: = 10 sum = 0 i = 1 while i while test_expression:
<= n: Body of while
Body of while sum = sum + i Else:
i = i+1 body of while failed
print("The sum
is", sum)
Module 5: Conditional statements/Control Structures

 For Loop
What is for loop in Python?
The for loop in Python is used to iterate over a sequence (list, tuple, string) or other iterable objects. Iterating over a sequence is called
traversal.

Example:
Syntax of for Loop
languages = ["C", "C++", "Perl", "Python"] for
for iterator_var in sequence:
x in languages:
statements(s) print(x)

Here, iterator_var is the variable that takes the value of the item inside the sequence on each iteration.

Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the
code using indentation.
Module 5: Conditional statements/Control Structures

 Looping Techniques over List, dictionary,Tuple,String


# Iterating over a list
# Iterating over a String
# Iterating over a list
print("\nString Iteration") primes = [2, 3, 5, 7]
print("List Iteration") s = “Tableau"

l = [“Java", “Tableau", “Python"] for for i in s : for prime in primes:


i in l: print(i) print(prime)
# Iterating over dictionary
print(i) print("\nDictionary Iteration")
def main():
stocks = {
# Iterating over a tuple (immutable) 'IBM': 146.48,
'MSFT':44.11,
print("\nTuple Iteration") 'CSCO':25.54
}
t = (“Java", “Tableau", “Python") for i
#print out all the keys
in t: for c in stocks:
print(c)
print(i) #print key n values
for k, v in stocks.items():
print("Code : {0}, Value : {1}".format(k, v))
if __name__ == '__main__':
main()
Module 5: Conditional statements/Control Structures

 The range() Function


The range() function is an in-built function in Python, and it returns a range object. First, let’s see what it looks like.
a. One Parameter

 For an argument n, the function returns integer values from 0 to n-1. range(stop)
b. Two Parameters

 Now, we’ll try different combinations for two parameters. range(start,stop)


c. Three Parameters

 Finally, the range() function can also take a third parameter. This is for the interval. range(start,stop,interval)

for i in range(6): print(i*2)

for i in range(1,10,0):

print(i)

Traceback (most recent call last):

File "<pyshell#2>", line 1, in <module>

for i in range(1,10,0):

ValueError: range() arg 3 must not be zero


Module 5: Conditional statements/Control Structures

 Break and continue Statements, and else clauses on


Loops
a. Break Statement b. Continue Statement c. Else Clauses
 The break statement is used to exit the  If you need to exit just the current  you may use the else clause with
current loop (while and for loops). The scope of break iteration of the loop rather exiting the loop the while and for loops. Just like in if
statement is only the current loop. entirely, you may use statement, the else statement in while
the continue statement of Python. loop executes as the condition becomes
x = 10 false
x = 10
x=5
while x <= 100: while x <= 100:
while x <= 20:
if x == 30: break x = x + 10
print (x)
print (x) x if x == 30: x = x+5

= x + 10 continue else:

print ("The print


("While
value of loop
variable conditio
n
is :", x) became
false!")

You might also like