28
Decision Making (if) and Loops (for, while)

Decision Making (if) and Loops (for, while)

Embed Size (px)

DESCRIPTION

Decision Making (if) and Loops (for, while). Decision Making. Decision making structures require that the programmer specify one or more conditions to be evaluated or tested by the program, - PowerPoint PPT Presentation

Citation preview

Page 1: Decision  Making (if) and Loops (for, while)

Decision Making (if)and Loops (for, while)

Page 2: Decision  Making (if) and Loops (for, while)

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.

• Python programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.

• Python programming language provides following types of decision making statements.

Page 3: Decision  Making (if) and Loops (for, while)

If statements

Statement Description

if statements An if statement consists of a boolean expression followed by one or more statements.

if...else statements An if statement can be followed by an optional else statement, which executes when the boolean expression is false.

nested if statements You can use one if or else if statement inside another if or else if statement(s).

Page 4: Decision  Making (if) and Loops (for, while)

If example

var1 = 100if var1: print "1 - Got a true value" print var1

var2 = 0if var2: print "2 - Got a true value" print var2print "Good bye!"

Page 5: Decision  Making (if) and Loops (for, while)

output

• 1 - Got a true value• 100• Good bye!

Page 6: Decision  Making (if) and Loops (for, while)

if...else statementsexample 2var1 = 100if var1: print "1 - Got a true expression value" print var1else: print "1 - Got a false expression value" print var1var2 = 0if var2: print "2 - Got a true expression value" print var2else: print "2 - Got a false expression value" print var2print "Good bye!"

Page 7: Decision  Making (if) and Loops (for, while)

output

• 1 - Got a true expression value• 100• 2 - Got a false expression value

• 0• Good bye!

Page 8: Decision  Making (if) and Loops (for, while)

nested if statements example 3var = 100if var < 200: print "Expression value is less than 200" if var == 150: print "Which is 150" elif var == 100: print "Which is 100" elif var == 50: print "Which is 50"elif var < 50: print "Expression value is less than 50"else: print "Could not find true expression"print "Good bye!"

Page 9: Decision  Making (if) and Loops (for, while)

output

• Expression value is less than 200

• Which is 100• Good bye!

Page 10: Decision  Making (if) and Loops (for, while)

Loops

• There may be a situation when you need to execute a block of code several number of times.

• In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on.

• Programming languages provide various control structures that allow for more complicated execution paths.

• A loop statement allows us to execute a statement or group of statements multiple times

Page 11: Decision  Making (if) and Loops (for, while)

Types of loopLoop Type Description

while loop Repeats a statement or group of statements while a given condition is true. It tests the condition before executing the loop body.

for loop Executes a sequence of statements multiple times and abbreviates the code that manages the loop variable.

nested loops You can use one or more loop inside any another while, for or do..while loop.

Page 12: Decision  Making (if) and Loops (for, while)

while loop

count = 0while (count < 9): print 'The count is:', count count = count + 1

print "Good bye!"

Page 13: Decision  Making (if) and Loops (for, while)

• The count is: 0• The count is: 1• The count is: 2• The count is: 3• The count is: 4• The count is: 5• The count is: 6• The count is: 7• The count is: 8• Good bye!

Page 14: Decision  Making (if) and Loops (for, while)

Syntax for loop

• The syntax of a for loop look is as follows:• for iterating_var in sequence:• statements(s) • 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.

Page 15: Decision  Making (if) and Loops (for, while)

for loop

for letter in 'Python':# First Example print 'Current Letter :', letterfruits = ['banana', 'apple', 'mango']for fruit in fruits: # Second Example print 'Current fruit :', fruitprint "Good bye!"

Page 16: Decision  Making (if) and Loops (for, while)

output

• Current Letter : P• Current Letter : y• Current Letter : t• Current Letter : h• Current Letter : o• Current Letter : n• Current fruit : banana• Current fruit : apple• Current fruit : mango• Good bye!

Page 17: Decision  Making (if) and Loops (for, while)

nested loopsThe syntax for a nested for loop statement

• for iterating_var in sequence:• for iterating_var in sequence:

• statements(s)• statements(s)

Page 18: Decision  Making (if) and Loops (for, while)

nested while loop

while expression: while expression: statement(s) statement(s)

Page 19: Decision  Making (if) and Loops (for, while)

Examplefind the prime numbers from 2 to 100:

i = 2while(i < 100): j = 2 while(j <= (i/j)): if not(i%j): break j = j + 1 if (j > i/j) : print i, " is prime" i = i + 1

print "Good bye!"

Page 20: Decision  Making (if) and Loops (for, while)

output

• 2 is prime• 3 is prime• 5 is prime• 7 is prime• …• 97 is prime• Good bye!

Page 21: Decision  Making (if) and Loops (for, while)

Loop Control Statements:

Control Statement

Description

break statement Terminates the loop statement and transfers execution to the statement immediately following the loop.

continue statement

Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating.

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.

Page 22: Decision  Making (if) and Loops (for, while)

Break - example

for letter in 'Python': # First Example if letter == 'h': break print 'Current Letter :', lettervar = 10 # Second Examplewhile var > 0: print 'Current variable value :', var var = var -1 if var == 5: breakprint "Good bye!"

Page 23: Decision  Making (if) and Loops (for, while)

output

• Current Letter : P• Current Letter : y• Current Letter : t• Current variable value : 10• Current variable value : 9• Current variable value : 8• Current variable value : 7• Current variable value : 6• Good bye!

Page 24: Decision  Making (if) and Loops (for, while)

continue statement

for letter in 'Python': # First Example if letter == 'h': continue print 'Current Letter :', letter• var = 10 # Second Example• while var > 0: • var = var -1• if var%2== 0:• continue• print 'Current variable value :', var• print "Good bye!"

Page 25: Decision  Making (if) and Loops (for, while)

output

• Current Letter : P• Current Letter : y• Current Letter : t• Current Letter : o• Current Letter : n• Current variable value : 9• Current variable value : 7• Current variable value : 5• Current variable value : 3• Current variable value : 1• Good bye!

Page 26: Decision  Making (if) and Loops (for, while)

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. The pass is also useful in places where your code will eventually go, but has not been written yet (e.g., in stubs for example):

Page 27: Decision  Making (if) and Loops (for, while)

Pass - example

• for letter in 'Python': • if letter == 'h':• pass• print 'This is pass block'• print 'Current Letter :', letter

• print "Good bye!"

Page 28: Decision  Making (if) and Loops (for, while)

Pass - output

• Current Letter : P• Current Letter : y• Current Letter : t• This is pass block• Current Letter : h• Current Letter : o• Current Letter : n• Good bye!