MJ

COMP121L_Decision_Making

Lesson Overview

  • Institution: Manuel S. Enverga University Foundation, Lucena City

  • Course: Computer Fundamentals and Programming 2

  • Instructor: Hannah Shimara P. Santonil

  • Focus: Decision structures and Boolean logic in Python programming

Learning Outcomes

  • Understand the basic concepts of Python programming language

  • Learn about:

    • The if statement

    • The if-else statement

    • Comparing strings

    • Nested decision structures and the if-elif-else statement

    • Logical operators

    • Boolean operators

Introduction to Decision Structures

  • Definition: Decision structures allow a program to execute actions based on specific conditions.

  • Types of decisions:

    • If statement: Single alternative

    • If-else statement: Dual alternative

    • Case structure: Multiple alternative decisions

The if Statement

  • Concept: Creates a decision structure allowing multiple execution paths based on Boolean expression truth.

  • A set of statements executes when a specified condition is true.

Control Structures

  • Control Structure: Logical design dictating the execution order of statements.

  • Sequence Structure: Executes statements in the order written.

  • Decision (Selection) Structure: Executes statements based on the existence of specific conditions.

Representation

  • The diamond symbol represents a true/false condition in decision structures.

  • Single Alternative Structure: Only one path of execution based on condition.

Python Syntax

  • Syntax Example: In Python, written as:

    if condition:
        statement
        statement

Boolean Expressions and Relational Operators

  • Relational Operator: Determines specific relationships between two values.

Example of if Statement

  • Scenario: A program to average three test scores and congratulate the user if above 95.

  • Pseudocode:

    • Input: Three test scores

    • Process: Calculate average

    • Output: Display average, congratulatory message if > 95

Sample Program

  • Program: test_average.py

  • Inputs:

    • Test 1 score (e.g., 82)

    • Test 2 score (e.g., 76)

    • Test 3 score (e.g., 91)

  • Displays average and congratulates if average ≥ 95.

The if-else Statement

  • Concept: Executes one block if the condition is true, another if false.

  • Provides two execution paths based on the condition.

Syntax Example

  • Python:

    if condition:
        statements
    else:
        statements

Example: Payroll Program

  • Scenario: Calculate gross pay with overtime.

  • Conditions:

    • If hours > 40, compute overtime.

    • Include a pay rate in calculation.

Comparing Strings

  • Concept: Test string values to create decision structures.

  • Example Program: Compare input password to a predefined correct password.

Nested Decision Structures and if-elif-else Statement

  • Concept: Tests multiple conditions with nested structures.

  • Syntax:

    if condition_1:
        statements
    elif condition_2:
        statements
    else:
        statements
  • Especially useful for grading or range scenarios.

Logical Operators

  • Concept: Used to connect multiple Boolean expressions.

  • Types:

    • and: True if both conditions are true.

    • or: True if at least one condition is true.

    • not: Reverses the truth of a Boolean expression.

Sample Use Cases

  • Example: Check if values fall within a range using and and or operators.

Boolean Variables

  • Concept: Can represent values True or False, often used as flags for conditions.

  • Usage Example:

    if sales_quota_met == True:
        print('You have met your sales quota!')

Conclusion

  • Boolean logic and decision structures form the fundamental basis for control flow in programming, enabling conditional execution and logical reasoning in code.