Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Flow statements

Similar presentations


Presentation on theme: "Control Flow statements"— Presentation transcript:

1 Control Flow statements
Generally we don't want linear collections of instructions. No user interaction. Code / calculation pathway can't act on previous calculations. Large amounts of repeated code. In general we want to make control flow more sophisticated.

2 Control Flow statements
Branching: If-this-do-this. Looping: Do this many times with slight differences. Procedure calls: Do this with some new information, and return an answer. Sockets: Read/write code from/to somewhere else. Objects: Make a toolkit to do a specific job. Libraries: Group toolkits for easy use. We'll talk about the first two here. We'll talk about the others later.

3 if compound statement Compound statements are one or more clauses, the inside of which must be indented if not all on one line. if condition: # Clause header. # do this # Suite of statements. # do this This line always done or (rarer): if condition: do this; do this First some terminology (see above). Although you can normally put several statements on a line separated by semicolons, so you would imagine that: if condition: do this; do this translated to: if condition: do this; do this The semi-colon "binds" the statements more tightly than the colon, so they act as a suite:

4 Example if a < 10: print("a less than 10") print("a assessed") or if (a < 10): Parentheses not needed, but can make things clearer, especially with multiple operators. Again, don't rely on precedence.

5 if-else if condition: # do this else: This line always done
If-else allows us to put in an either-or statement, rather than just one statement that may or may not occur depending on a transition.

6 Example if a < 10: print("a less than 10") else: print("a greater than 10") print("a assessed")

7 The if-else-if ladder if condition: # do this elif condition: else:
This line always done The if-elif ladder is a way of picking from several different options.

8 Example if day <= 5: print("Weekday") elif day == 6: print("Saturday") else: print("Sunday") But you have to watch for inefficiencies. This is ok, but imagine we had an elif for every day. The probability, all other things being equal, is equal for every day. For some days, therefore, you would have to traverse a lot of the list, making multiple decisions on the way, which is inefficient. This structure is best used when there is a higher probability of a condition you can put near the top. Other languages have a switch/case statement, which is less efficient than a single if-else, but more efficient that a ladder. Python doesn't have this: Though you can build similar if you have a massive number of choices: Of these stackoverflow solutions, this seems a nice one: def f(x): return { 'a': 1, 'b': 2 }.get(x, 9)

9 Nested compound statements
if a < 10: if b < 10: print("a and b less than 10") else: print("b greater than 10, a less") print ("a and b assessed") Note that to avoid ambiguity as to what the else links to, you can't do this all on one line.

10 Conditions Best to think of these as having to evaluate to either True or False. a = 2 if (a == 2): # True if (a != 2): # False if (a != 3): # True if not (a == 2) : # False a = True if (a): # True if not (a) : # False a = False if (a): # False if not (a): # True So, we've been talking about conditions, and you've seen some examples, but they can be much more complicated.

11 Boolean operators if (a == 2) or (b == 3): # If a == 2 OR b == 3 if (a == 2) and (b == 3): # If a == 2 AND b == 3 OR and AND can therefore shortcut if the first condition is respectively true (for OR) or false (for AND). Although this is possible: if not a is None: do instead: if a is not None: Note that empty sequences are false, so this is recommended by the docs: if not seq: if seq:

12 Conditional quirks x < y < z # Is (x < y) and (y < z). x < y > z # Is fine.

13 Ternary operator x if condition else y Which means: x if condition; y if not condition. For example: a = 10 b = "less than 5" if a < 5 else "more than five" print(b) The Python FAQ gives this nice example: x, y = 50, 25 small = x if x < y else y In C-style languages, there's a construction for this which is: b = (a < 5) ? "less than 5": "more than five";


Download ppt "Control Flow statements"

Similar presentations


Ads by Google