Programming Essentials in Python Section 4
Symbols Used in Flowcharts
Flowcharts use various symbols to visually represent the structure of algorithms and processes. Each symbol has a specific meaning, allowing viewers to quickly understand the flow and logic of a program.
1. Oval
- Function: Used to indicate the
STARTorENDof the program.
2. Parallelogram
- Function: Represents input and output operations. Statements like
INPUT,READ, andPRINTare typically enclosed within this shape.
3. Rectangle
- Function: Used to indicate processing operations, such as storing values or performing arithmetic calculations.
4. Diamond
- Function: Indicates a decision-making step, known as the decision box. This shape is used to test conditions, ask questions, and direct the flow based on responses.
5. Flow Lines
- Function: Arrows indicate the direction of flow in a flowchart. Every line must have an arrow to specify the process sequence.
6. Circle
- Function: Known as on-page connectors, circles help join different parts of a flowchart on the same page. They also assist in shaping complex flowcharts by connecting sections.
Flowchart Example for Decision-Making
Decision Structures in Flowcharts
There are two common ways to represent decisions in a flowchart:
Binary Questions: The condition is framed as a question with two possible outcomes:
YesNo
Binary Statements: The condition is expressed as a statement with two possible outcomes:
TrueFalse
Decision-Making in Python
In Python, the if statement is used to execute code based on a condition.
1. If Statement
The if statement executes a block of code if the condition evaluates to True.
Syntax:
# Check a condition
if condition:
statement1_here # Executes if condition is TrueExample:
# Check if a number is positive
num = 5
if num > 0:
print("The number is positive")2. If-Else Statement
The if-else structure provides an alternative set of actions if the condition is False.
Syntax:
if condition:
statement1_here # Executes if condition is True
else:
statement2_here # Executes if condition is FalseExample:
# Check if a number is positive or negative
num = -3
if num > 0:
print("The number is positive")
else:
print("The number is negative")3. Nested If Statements
Nested if statements allow for more complex decision structures by placing if statements inside other if statements.
Example:
# Find the largest of three numbers
a = 10
b = 15
c = 7
if a > b:
if a > c:
print("a is the largest")
else:
print("c is the largest")
else:
if b > c:
print("b is the largest")
else:
print("c is the largest")Algorithm to Find the Largest of Three Numbers
- Start
- Input three numbers:
a,b, andc - Check if
ais greater thanb- If yes, then check if
ais also greater thanc- If yes,
ais the largest - Otherwise,
cis the largest
- If yes,
- If no, check if
bis greater thanc- If yes,
bis the largest - Otherwise,
cis the largest
- If yes,
- If yes, then check if
- End