4 - Control Structures - if, if-else, if-elif-elif-else (1).pptx
Document Details
Uploaded by InvulnerableYellow2190
2019
Tags
Related
- Unit 1 Python Basics, Control Structures, and Other Features PDF
- Week 4 Control Structures PDF
- Introduction to Computer Science Using Python PDF
- Python Programming Quiz Questions PDF
- BSc (Hons) Electronics Ist Sem - Programming Fundamentals using Python PDF
- Data Analytics with Python - MGM University - PDF
Full Transcript
Fall 2019 Computational Thinking with @cse_bennett @csebennett Programming Control Structures Control flow is the order that instructions are executed in a program....
Fall 2019 Computational Thinking with @cse_bennett @csebennett Programming Control Structures Control flow is the order that instructions are executed in a program. A control statement is a statement that determines the control flow of a set of instructions. Types of Control: Sequential control: Instructions are executed in the order that they are written Selection control: Selectively executes the Control instructions. E.g. Decision Control Structures Iterative control: Repeatedly executes the instructions. E.g. Loops. Selection Control or Decisions (It is a control statement providing selective execution of instructions) if statements Decisio if else ns in a statements Python elif statements progra nested if m conditions If Statement Syntax: It is a selection control statement based on the if test value of a given Boolean expression expression: Expression’s value can be True or False. statement(s) We may want to do something only when a certain condition is true. if statement in python takes an expression with it. If the expression results to True then the block of statements under it is executed. If it results to False then the block is skipped and control transfers to the statements after the block. Remember to indent the statements in a block equally If Statement: Example Example: What will be the output ? 1. 4. 2. 5. 3. Example: What will be the output ? 1. 4. Output: Smaller Output: 3 is a positive number. 2. This is always printed. 5. Output: Finish 3. Output: This is also always printed. Output: yay if...else Statement a.k.a. Two way decisions What happens when the condition is untrue or false? We can mention that it in the block after the else statement. An ‘else’ statement comes right after the block after ‘if’. Syntax: if test expression: Body of if else: Body of else Example One unique aspect of Python is that the amount of indentation of each program line is significant. Header, Suite and Indentation Nested if statements (multi-way selection) You can put an if statement in the block under another if statement. This is to implement further checks. Nested if statements: Example Example: What will be the output? Example: What will be the output? if...elif...else Statement More than Replacement one to the else-if condition to statements check If condition 1 If it isn’t true, isn’t True, condition 3 condition 2 is checked. is checked. if...elif...else Statement: Example Use Syntax: if test expression: Body of if elif test expression: Body of elif else: Body of else Example: What will be the output? Example: What will be the output? Single Statement Condition Write a single statement under if Exercise Find out the Max of three numbers Maximum between 3 numbers Maximum between 3 numbers Maximum between 3 numbers MCQs 1. All if statements must contain 3. Which of the following statements is either an else or elif header. true? a) TRUE a) Statements within a suite can be b) FALSE indented a different amount. b) Statements within a suite can be indented a different amount as long 2. Which of the following as all headers in the statement that statements are true regarding it occurs in are indented the same headers in Python? amount. a) Headers begin with a c) All headers must be indented the keyword and end with a same amount as all other headers colon. in the same statement, and all b) Headers always occur in statements in a given suite must be pairs. indented the same amount. c) All headers of the same 4. The elif header allows for, compound statement must a) Multi-way selection that cannot be be indented the same accomplished otherwise amount. b) Multi-way selection as a single if MCQs: Answers 1. All if statements must contain 3. Which of the following statements is either an else or elif header. true? a) TRUE a) Statements within a suite can be b) FALSE indented a different amount. b) Statements within a suite can be indented a different amount as long 2. Which of the following as all headers in the statement that statements are true regarding it occurs in are indented the same headers in Python? amount. a) Headers begin with a c) All headers must be indented keyword and end with a the same amount as all other colon. headers in the same statement, b) Headers always occur in and all statements in a given pairs. suite must be indented the c) All headers of the same same amount. compound statement 4. The elif header allows for, must be indented the a) Multi-way selection that cannot be same amount. accomplished otherwise