Chapter 15 - Selection Structures for Decision Making
- Authors
- Ms. Samavi Salman
- Dr. Rao Muhammad Adeel Nawab
Quick Recap
- Quick Recap – Software Life Cycle
In previous Chapter, I presented the following main concepts:
- Software Life Cycle
- Software Life Cycle is defined as a Step-by-Step process to completely and correctly develop a Software with high-quality to serve the humanity for Raza (رضا) of Allah
- Phases of Software Life Cycle
- The four main Phases of Software Life Cycle are
- Development Phase
- Testing Phase
- Application Phase
- Feedback Phase
- The four main Phases of Software Life Cycle are
- Phases of Software Life Cycle Cont…
- Each Phase of Software Life Cycle is further divided into multiple Steps, which are given below:
- Development Phase
- Step 1: Completely and Correctly Understand the Real-world Problem
- Step 1.1: Real-world Problem
- Step 1.2: Real-world Problem Description
- Step 1.3: Your Job – As a Software Developer
- Step 2: Can we treat the Real-world Problem (Step 1.2) as a Programming Problem?
- Step 2.1: Check Whether the Real-world Problem can be treated as a Programming Problem?
- Step 2.2: Convert Real-world Problem Description (Step 1.2) into Programming Problem Description
- Step 3: Completely and Correctly Understand the Programming Problem
- Step 3.1: Completely and Correctly Understand the Programming Problem Description
- Step 3.2: Requirements Gathering
- Step 3.3: Requirements Analysis
- Step 4: Plan and Design Solution to the Programming Problem
- Step 4.1: Identify Input-Processing-Output (from Step 3.1, 3.2 and 3.3)
- Step 4.2: Write down Algorithm for Software to be Developed (based on Step 4.1)
- Step 4.3: Write Down Pseudo Code for Software to be Developed (based on Step 4.2)
- Step 4.4: Design and Draw Flow Chart(s) for Software to be Developed (based on Step 4.3)
- Step 4.5: Select Suitable Programming Environment to Write Software (Code)
- Step 5: Implementation Phase
- Step 5.1: Write Software (based on Step 4.2, 4.3 and 4.4)
- Step 1: Completely and Correctly Understand the Real-world Problem
- Testing Phase
- Step 1: Check the Quality of Software (Developed in Step 5.1 of the Development Phase) before Deploying it in the Real-world (i.e., Application Phase)
- Step 1.1: Extract Input and Output from your Software (implemented in Step 5.1 of the Development Phase)
- Step 2: Write Suitable and High-Quality Test Cases
- Step 2.1: Write Test Cases for Correct Normal Input
- Step 2.2: Write Test Cases for Wrong Input
- Step 2.3: Write Test Cases for Correct Exceptional Input
- Step 3: Write Output Values to be produced against Input of each Test Case (called Actual Output Values)
- Step 4: Run Software for each Test Case Separately
- Step 4.1: Give Input (of each Test Case) to Software and Execute it to produce Output against each Test Case (called Software Output Values)
- Step 4.2: Record the Software Output Values for all Test Cases
- Step 5: Check the Performance of Software for all Test Cases
- Step 5.1: For each Test Case, compare Actual Output Value with Software Output Value
- Step 6: Check Quality of Software i.e., Whether Software is Working Correctly or Not?
- Step 1: Check the Quality of Software (Developed in Step 5.1 of the Development Phase) before Deploying it in the Real-world (i.e., Application Phase)
- Application Phase
- Step 1: Convert Software into an Executable File
- Step 1.1: Write Down System Settings
- Step 1.1.1: Select the Operating System for which you want to Create an Executable File
- Step 1.1.2: Select the Programming Language for which you want to Create an Executable File
- Step 1.1.3: Select the IDE / Framework which you will use to Create an Executable File
- Step 1.2: Use your System Settings to Step by Step Convert Software into an Executable File
- Step 1.1: Write Down System Settings
- Step 2: Deploy Executable File (created in Step 1.2) on Client Machine
- Step 3: Verify the Deployment of Software on Client Machine
- Step 1: Convert Software into an Executable File
- Feedback Phase
- Step 1: After Software Deployment, take regular Feedback from
- Domain Experts and Users on Deployed Software
- Step 2: Make a List of Possible Improvements based on Feedback received
- Step 3: Improve your Software based on List of Possible Improvements (made in Step 2)
- Step 1: After Software Deployment, take regular Feedback from
- Development Phase
- Each Phase of Software Life Cycle is further divided into multiple Steps, which are given below:
Selection Structure in Python
- Real-world Problem
- Definition
- A Real-world Problem is defined as a matter or situation regarded as unwelcome or harmful and needing to be deal with and overcome
- Purpose – Why Solve a Real-world Problem?
- To improve the quality of human life
- Example
- Real-world Problem
- You are walking in rain and getting wet
- A Possible Solution
- Use an Umbrella
- Real-world Problem
- Programming Problem
- Definition
- To analyze the program before writing it is known as Programming Problem
- Purpose
- The main purpose of Programming Problem is to identify the following parameters from the given Program:
- Identify the input
- Identify Purpose of Program and Operators performed on a Program
- Display the Output
- The main purpose of Programming Problem is to identify the following parameters from the given Program:
- Question
- Can we transform Real-World problem into a Programming Problem?
- Answer
- Yes
- The input, processing and output phenomena should be clear and identified from the Task Description
- Yes
Recap - Python Control Structures
- Recap - Python Control Structures
- In Python Programming, three main Python Control Structures are
- Sequential Structure
- Selection Structure
- Repetition Structure
- Sequential Structure
- Definition
- A Sequence Structure executes a Sequence of Python Statements one by one in the order in which they are written
- Purpose
- The main purpose of Sequential Structure it to
- Execute Python Statements one by one in the order
- The main purpose of Sequential Structure it to
- In Programming languages based on Sequential Structures
- Some of the popular and widely used Programming languages which execute their Programs using Sequential structure are
- Java
- C
- C++
- Python
- FORTRAN
- Basic
- PL1
- Pascal
- Some of the popular and widely used Programming languages which execute their Programs using Sequential structure are
- Selection Structure
- Definition
- A Selection Structure (a.k.a. Conditional Structure) is a feature of Python Programming Language that is used to perform different actions / tasks based on Decision Making (whether a specific Condition is True or False)
- Purpose
- The main purpose of Selection Structures is
- Decision Making
- The main purpose of Selection Structures is
- Selection Structures in Python
- Selection Structures in Python Programming Language are
- if
- elif
- else
- Selection Structures in Python Programming Language are
- Repetitive Programming Task
- A Repetitive Programming Task is defined as a Programming Task in which there is little variation from one item to the next item of the Programming Task
- Repetition Structure
- Definition
- A Repetition Structure is a feature of Python Programming Language that is used to perform Repetitive Python Programming Tasks
- Purpose
- The main purpose of Repetition Structure it to perform Repetitive Programming Task
- Repetition Structure in Python
- Repetition Structure in Python Programming Language are
- for loop
- while loop
- do-while loop
Decision Making in Python
- Chapter Focus
- The main focus of this Chapter is on
- Selection Structures
- For details on Repetition Structures
- See Chapter 15 – Loops in Python
- Decision
- Definition
- Decision can be defined as an act of Selecting / Choosing one action from several alternatives
- Decision-Making
- Definition
- Decision-Making is defined as the process of making Decision
- Purpose
- The main purpose of Decision-Making is to
- Take Most Suitable Actions to Perform Various Real-world Tasks
- The main purpose of Decision-Making is to
- Importance
- Every day we make several Decisions to perform various Real-world Tasks (or solve various Real-world Problems)
- For Example
- Either drink tea, coffee or green tea in a meeting
- Either got School by bus or car
- Applications
- Right Decision at Right Time help us to
- Become a Balanced and Characterful Person
- Right Decision at Right Time help us to
- Quote on Decision Making
- I remember a famous Quote on Decision Making
- There is nothing as Right Decision or Wrong Decision. I Take a Decision and Prove it to be Right 😊
- When There is a Will There is a Way 😊
- Never Ever Give Up 😊
- Never Ever Lose Hope 😊
- Decision Making in Python
- Question
- How we can perform Decision Making in Python?
- Answer
- Using Selection Structure
- Decision Making in Python
- Definition
- In Python Program, Outcome of a Condition (Relational Expression, logical Expressions, Boolean Expressions and Arithmetic Expressions) is used to make a Decision (i.e., which Block of Code will be executed)
Condition in Python
- Condition in Python
- Question
- How to Write a Condition (Relational Expression) in Python?
- Answer
- Condition (Relational Expression) in Python is written as follows
- Left Hand Side (LHS) RELATIONAL OPERATOR Right Hand Side (RHS)
- Condition (Relational Expression) in Python is written as follows
- Note
- LHS can have Constant / Variable / Expression
- RHS can have Constant / Variable / Expression
- Relational Operator can be
- ==
- !=
- <
- >
- <=
- >=
- Conditionals in Python
- Definition
- A statement that controls the flow of execution depending on some condition
- Purpose
- The main purpose of Conditional Statements is to perform different operations depending upon the condition
- Importance
- A conditional statement is used to determine whether a certain condition exists before code is executed
- Conditional statements can help improve the efficiency of Program
- Suitable to Use
- They are used to control the execution of our program
- Types of Conditionals in Python
- If Statement
- Elif Statement
- Else Statement
- Conditions in Python
- Definition
- The Boolean Expression is a conditional statement which determines which block will be executed
- Purpose
- Conditions control the flow of execution of Programs based on conditionals
- Suitable to Use
- Conditions are used when User will have more than one choice
- Example of Conditions
- Equal to (==)
- Greater than (>)
- Less than (<)
- Greater than Equal to (>=)
- Less than Equal to (<=)
- Not Equal to (!=)
- Examples of Conditions
- 5 > 10
- False
- 2 < 5
- True
- 0 != 10
- True
- 3 == 6
- False
- 4 >= 10
- True
- 5 <= 2
- False
- 5 <= 5
- True
- 5 + 2 == 9
- False
- 2 ** 4 == 15
- False
- 9 / 3 == 3
- True
The operators used above as Example of Conditions are collectively named as Relational Operator (will be discussed below)
- Relational Operator
- Definition
- Relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities
- Expression using Relational Operator
- The Relational Operator comprises of three Parts
- Part 01: Left Hand Side (LHS) of Relational Operator (==,!=,>,<,>=,<=)
- Part 02: Relational Operator (==,!=,>,<,>=,<=)
- Part 03: Right Hand Side (RHS) of Relational Operator (==,!=,>,<,>=,<=)
- Syntax
Left Hand Side (LHS) (Relational Operator) Right Hand Side (RHS)
- Note
- On LHS (of Relational Operator (==,!=,>,<,>=,<=)), we can only have a Variable
- On RHS (of Relational Operator (==,!=,>,<,>=,<=)), we may have a
- Constant / Variable / Expression / Function
- Note
- For details on Expressions
- See Chapter 7 – Operators and Expressions
- For details on Functions
- See Chapter 17 – Functions in Python
- See Chapter 17 – Functions in Python
- For details on Expressions
- Syntax – Relational Operator Cont.…
Left Hand Side (LHS) (Relational Operator) Right Hand Side (RHS)
Variable = Constant / Variable / Expression
- Example – Relational Operator
- positive = number > 0
- negative = number < 0
- marks = 90 < marks =< 100
- even = number % 2 == 0
- odd = number % 2 == 1
- Conditions in Python
- Question
- What Output will be returned by Conditions in Python
- Answer
- Boolean Expression (True or False)
Chapter Summary
- Chapter Summary
In this Chapter, I presented the following main concepts:
- Real-world Problem
- Definition
- A Real-world Problem is defined as a matter or situation regarded as unwelcome or harmful and needing to be deal with and overcome
- Purpose – Why Solve a Real-world Problem?
- To improve the quality of human life
- Definition
- Programming Problem
- Definition
- To analyze the program before writing it is known as Programming Problem
- Purpose
- The main purpose of Programming Problem is to identify the following parameters from the given Program:
- Identify the input
- Identify Purpose of Program and Operators performed on a Program
- Display the Output
- The main purpose of Programming Problem is to identify the following parameters from the given Program:
- Definition
- Python Control Structures
- In Python Programming, three main Python Control Structures are
- Sequential Structure
- Selection Structure
- Repetition Structure
- In Python Programming, three main Python Control Structures are
- Sequential Structure
- Definition
- A Sequence Structure executes a Sequence of Python Statements one by one in the order in which they are written
- Purpose
- The main purpose of Sequential Structure it to
- Execute Python Statements one by one in the order
- The main purpose of Sequential Structure it to
- Definition
- Selection Structure
- Definition
- A Selection Structure (a.k.a. Conditional Structure) is a feature of Python Programming Language that is used to perform different actions / tasks based on Decision Making (whether a specific Condition is True or False)
- Purpose
- The main purpose of Selection Structures is
- Decision Making
- The main purpose of Selection Structures is
- Definition
- Repetition Structure
- Definition
- A Repetition Structure is a feature of Python Programming Language that is used to perform Repetitive Python Programming Tasks
- Purpose
- The main purpose of Repetition Structure it to perform Repetitive Programming Task
- Definition
- Decision
- Definition
- Decision can be defined as an act of Selecting / Choosing one action from several alternatives
- Definition
- Decision-Making
- Definition
- Decision-Making is defined as the process of making Decision
- Purpose
- The main purpose of Decision-Making is to
- Take Most Suitable Actions to Perform Various Real-world Tasks
- The main purpose of Decision-Making is to
- Definition
- Decision Making in Python
- Definition
- In Python Program, Outcome of a Condition (Relational Expression, logical Expressions, Boolean Expressions and Arithematic Expressions) is used to make a Decision (i.e., which Block of Code will be executed)
- Definition
- Conditionals in Python
- Definition
- A statement that controls the flow of execution depending on some condition
- Purpose
- The main purpose of Conditional Statements is to perform different operations depending upon the condition
- Definition
- Conditions in Python
- Definition
- The Boolean Expression is a conditional statement which determines which block will be executed
- Purpose
- Conditions control the flow of execution of Programs based on conditionals
- Definition
- Relational Operator
- Definition
- Relational operator is a programming language construct or operator that tests or defines some kind of relation between two entities
- Definition
- If Statement
- Definition
- if statement is an expression that can be evaluated as True or False and is called a conditional test
- It executes a set of statements conditionally, based on the value of a logical expression
- Definition
- Purpose
- The main purpose if statement is that it takes the form of an if test, followed by one or more optional elif (“else if”) tests and a final optional else block
- Python if Statement is used for decision-making operations
- The main purpose if statement is that it takes the form of an if test, followed by one or more optional elif (“else if”) tests and a final optional else block
- Else Statement
- Definition
- else condition can be optionally used to define an alternate block of statements to be executed if the boolean expression in the if condition evaluates to False
- Definition
- Purpose
- In programming languages, The else keyword is used in conditional statements (if statements), is an alternative statement that is executed if the result of a previous test condition evaluates to false
- Elif Statement
- Definition
- The elif is short for else if. It allows us to check for multiple expressions. If the condition for if is False , it checks the condition of the next elif block and so on. If all the conditions are False , the body of else is executed
- Purpose
- The main purpose is that the elif condition is used to include multiple conditional expressions after the if condition or between the if and else conditions. The elif block is executed if the specified condition evaluates to True
- Definition
- Nested IF
- Definition
- if statements inside if statements, this is called nested if statements
- Purpose
- The main purpose of Nested if statements are often used when you must test a combination of conditions before deciding on the proper action
- Definition
In Next Chapter
- In Next Chapter
- In Sha Allah, in the next Chapter, I will present a detailed discussion on
- Repetition Structure in Python