Lecture 2 Decision Making If-Else Statements Lecture Notes PDF

Summary

These lecture notes cover decision making in Python programming using if-else statements, including examples and flowcharts to illustrate the concepts. The notes cover various aspects of conditional logic, such as if, elif, and else statements, and demonstrate how to implement them with code. The content is suitable for an undergraduate-level introductory programming class.

Full Transcript

Structure Programming By Dr. Safa Elaskary Dr. Manar Elshazly Fall 2024-2025 1 2 Arithematic operation 3 Comparator operators 4 Lecture 2 : Decision Making: If-Else Statements 1. What i...

Structure Programming By Dr. Safa Elaskary Dr. Manar Elshazly Fall 2024-2025 1 2 Arithematic operation 3 Comparator operators 4 Lecture 2 : Decision Making: If-Else Statements 1. What is decision making? 2. Flowchart Symbols Overview 3. Python Conditional Statements 4. Flowchart vs. Code 5 1. What is Decision Making? ◦ Decision making in programming: ▪ Involves choosing different paths based on conditions. ◦ Conditional statements: ▪ Guide the flow of a program by allowing it to make decisions and execute specific blocks of code based on whether conditions are True or False. 6 2.Flowchart Symbols Overview Flowchart Symbols ◦ Oval: Represents the Start or End of the process. ◦ Diamond: Represents a decision point (e.g., True/False or Yes/ No). ◦ Rectangle: Represents actions or steps to be executed. ◦ Arrows: Indicate the flow direction from one step to another. 7 3.Python Conditional Statements Use if, else, and elif to manage decision-making in code. ◦ These statements evaluate specified conditions. ◦ Execute corresponding blocks of code based on whether the conditions are True or False. 8 Simple Flowchart Example ◦ This flowchart illustrates the decision process for determining if a person can ride a rollercoaster based on their height. ◦ Components: ◦ Start (Oval) ◦ Input Height (Rectangle) ◦ Decision: "Is height ≥ 120 cm?" (Diamond) ◦ Yes: "Allow to Ride" (Rectangle) → End (Oval) ◦ No: "Deny Access" (Rectangle) → End (Oval) 3.1. The if Statement Syntax in python: #code if condition: This code executes a block of code if the condition is True. 10 3.1. The if Statement Example: #code age = 18 if age >= 18: print("You are an adult.") This example checks if the variable age is greater than or equal to 18, and if so, it prints a message indicating that the person is an adult. 11 3.2. The else Statement Syntax in python: #code if condition: # code if condition is True else: # code if condition is False This code executes an alternative block of code if the condition is False. 12 3.2. The else Statement ◦ Example: #code age = 16 if age >= 18: print("You are an adult.") else: print("You are a minor.") In this example, if the variable age is less than 18, the program will print a message indicating that the person is a minor. 13 3.2. The else Statement ◦ Example: #code age = 16 if age >= 18: print("You are an adult.") else: print("You are a minor.") In this example, if the variable age is less than 18, the program will print a message indicating that the person is a minor. 14 3.3. The elif Statement ◦ Syntax in python: #code if condition: # code if condition is True elif condition: # code if this new condition is True else: # code if all previous conditions are False This code tests multiple conditions sequentially. 15 3.3. The elif Statement ◦ Example: #code grade = 85 if grade >= 90: print("Excellent") elif grade >= 75: print("Good") else: print("Needs Improvement") In this example, the program evaluates the grade and prints a corresponding message based on the defined conditions. 16 4.Flowchart vs. Code Flowchart to code Example : Check if a student's grade falls into different categories (Excellent, Good, Needs Improvement). 17 4.Flowchart vs. Code Flowchart to code Example : #code grade = 85 # Example input if grade >= 90: print("Excellent") elif grade >= 75: print("Good") else: print("Needs Improvement") 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 Try to draw this flowchart Example : Number Comparison This example presents a flowchart and corresponding Python code for comparing two numbers to determine which is larger. 33 Try to convert 34 35

Use Quizgecko on...
Browser
Browser