Control Structures
1 / 17
Control Structures 1 / 17 Structured Programming Any algorithm can - - PowerPoint PPT Presentation
Control Structures 1 / 17 Structured Programming Any algorithm can be expressed by: Sequence - one statement after another Selection - conditional execution (not conditional jumping) Repetition - loops Weve already seen sequences
1 / 17
◮ Sequence - one statement after another ◮ Selection - conditional execution (not conditional jumping) ◮ Repetition - loops
2 / 17
◮ those who know binary, and ◮ those who don’t.
3 / 17
◮ bool literals: True and False ◮ bool variables ◮ expressions formed by combining non-~bool~ expressions with
◮ expressions formed by combining bool expressions with logical
4 / 17
◮ Equal to: ==, like = in math
◮ Remember, = is assignment operator, == is comparison operator!
◮ Not equal to: !=, like = in math ◮ Greater than: >, like > in math ◮ Greater than or equal to: >=, like ≥ in math
5 / 17
◮ boolean False ◮ None ◮ integer 0 ◮ float 0.0 ◮ empty string ~""~ ◮ empty list [] ◮ empty tuple () ◮ empty dict {} ◮ empty set set()
6 / 17
◮ or only evaluates second operand if first operand is False ◮ and only evaluates second operand if first operand is True
◮ True and False ◮ True and 0 ◮ True and [] ◮ True and None ◮ type(True and None) ◮ False or 1 ◮ True or 1
7 / 17
◮ boolean_expression is not enclosed in parentheses ◮ else: not required
8 / 17
9 / 17
10 / 17
11 / 17
◮ “repeat . . . while (or until) some condition is true” (looping) or ◮ “for each element of this array/list/etc. . . . ” (iteration)
◮ while loop ◮ for iteration statement
12 / 17
13 / 17
◮ iteration means visiting the elements of an iterable data structure
◮ animal is assigned to each element of the iterable list of animals in
◮ notice that the loop variable re-assigned an existing variable
14 / 17
◮ break terminates execution of a loop ◮ optional else clause executes only of loop completes without
15 / 17
16 / 17
17 / 17