class: big, middle # Engineering 1020: Introduction to Programming .title[ .lecture[Lecture 6:] .title[If statements] ] .footer[[/lecture/6/](/lecture/6/)] --- # Last time <img src="https://upload.wikimedia.org/wikipedia/commons/9/91/LampFlowchart.svg" alt="A simple flowchart" align="right" width="300"/> ### Exercise (just for fun): > Draw a flowchart! ### Any process that has: * decisions * actions ### _Explain_ it to someone ??? This exercise doesn't need to be submitted --- # Control flow in programming ### Conditional control flow > either do this or else do that, based on a _condition_ ### Looping > do this over and over, as long as a _condition_ is satisified --- # Flowchart: promotion decisions .center[ <img src="../flowcharts/term-3.png" width="600" alt="Term 3 promotion decisions"/> ] --- # Control flow in Python ### Conditional control flow: ```python if condition: do stuff elif another condition: do something else else: do some other stuff ``` ### Note new _keywords_ and indentation ??? A condition is either true or false: it's an expression that evaluates to a Boolean value. --- # Exercise* .footnote[ * Just for fun, not for submission ] ### Make [python.py](../6/python.py) use conditional control flow <img src="../flowcharts/if-knights.png" align="right" alt="A flowchart for not-so-brave knights" width="400"/> ### Use this logic, or... * What if the knight has a particularly awesome name? * What if the swallow can't travel the necessary distance in time? --- # Example ## Critical buckling stress $$ F_{cr} = \begin{cases} (0.658) ^ {\lambda_c^2} F_y & \lambda_c \leq 1.5 \cr \frac{0.877}{\lambda_c^2} F_y & \lambda_c > 1.5 \end{cases} $$ ??? In structural engineering, the Critical Buckling Stress for a column can be determined using this equation in the Load and Resistance Factor Design method, in which $ \lambda\_c $ is the _slenderness ratio_, a measure of how slender the column is, and $ F\_y $ is the _yield force_ at which the column's material will fail. --- # An academic example > Convert a numeric mark into a letter grade -- ### Note: _operator chaining_ ??? Python allows us to combine comparators together like a mathematical _range_ ($3 < x < 5$). --- # A musical exercise > Write a script that prompts for one input, a year from 1958 onwards, and prints out the "top" or "best" song of that year's decade (use [this list](https://en.wikipedia.org/wiki/List_of_Billboard_Hot_100_chart_achievements_by_decade#Songs_by_total_weeks_at_number_one) if helpful). --- class: big, middle (here endeth the lesson)