Practical Programming -L03
Practical Programming -L03
How It Works:
Statements are executed in the order they appear.
There is no branching or repetition; the program flows from one
statement to the next.
(i) Sequential Structure:
Start
Statement 1
Statement 2
Statement 3
End
(ii) Selection Structure (Decision-Making)
• The selection structure allows the program to choose between
multiple paths of execution based on a logical condition.
How It Works:
• A condition is evaluated.
• If the condition is true, the program executes the sequence of
statements under the "Yes" branch.
• If the condition is false, the program executes the sequence of
statements under the "No" branch.
(ii) Selection Structure (Decision-Making)
Relational Operators for Conditions:
Operator Meaning
> Greater than • The comparison in the condition
>= Greater than or equal to can be between:
• A variable and another variable.
< Less than • A variable and a constant.
<= Less than or equal to • An expression and a variable.
== Equal to • An expression and a constant.
!= Not equal to
Types:
• Single Selection: IF-THEN (executes a block of code if the condition is
true).
• Double Selection: IF-THEN-ELSE (chooses between two paths based on
the condition).
• Multiple Selection: CASE or SWITCH (chooses among multiple paths
based on the value of an expression).
(ii) Selection Structure (Decision-Making)
• <= Less than or equal to
• == Equal to
• != Not equal to
• The comparison in the condition can be between:
• A variable and another variable.
• A variable and a constant.
• An expression and a variable.
• An expression and a constant.
(ii) Selection Structure
IF-THEN IF-THEN-ELSE
(iii) Looping Structure (Repetition)
• The looping structure allows the program to repeat a set of statements
multiple times based on a condition.
How It Works:
• A condition is evaluated at the beginning (or end) of the loop.
• If the condition is true, the loop body (sequence of statements) is executed.
• The loop continues until the condition becomes false.
Types:
• Pre-Test Loop: The condition is evaluated before executing the loop body
(e.g., WHILE loop).
• Post-Test Loop: The condition is evaluated after executing the loop body
(e.g., REPEAT-UNTIL loop).
• Count-Controlled Loop: The loop runs for a fixed number of iterations (e.g.,
FOR loop).
(iii) Looping Structure (Repetition)
5. Flowchart Design Examples
Example 1: Determining the square-root of the variable x
5. Flowchart Design Examples
Example 2: Determining if a Number is Positive or Negative
5. Flowchart Design Examples
Problem: Find the maximum number in a list.
Algorithm from Lecture 2:
1. Start
2. Read the list of numbers.
3. Set the first number as the maximum.
4. Compare each number with the current maximum.
5. If a number is greater, update the maximum.
6. Repeat until the end of the list.
7. Print the maximum number.
8. Stop.
Start
No Yes
If number > max
End
5. Flowchart Design Examples
Problem: Calculating the Average of Numbers
Algorithm from Lecture 2:
1. Input: A list of numbers, numbers = [n1, n2, n3, ……,nk].
2. Steps:
• Initialize a variable total_sum to 0.
• Initialize a variable count to 0.
• Iterate through each number in the list:
• Add the current number to total_sum.
• Increment count by 1.
• Calculate the average by dividing total_sum by count
• Return the average.
3. Output: The average of the numbers in the list.
Start
total_sum = 0
Count = 0
No Yes
For each number in list
End
5. Flowchart Design Examples
Problem: Write an algorithm to sort a list of numbers in ascending order.
Algorithm from Lecture 2:
a. Input: A list of numbers, numbers = [n1, n2, n3, ……,nk].
b. Steps:
1. Start with the first element of the list.
2. Compare the current element with the next element.
3. If the current element is greater than the next element, swap
them.
4. Move to the next element and repeat step 2-3 until the end of list.
5. Repeat the entire process for the list until no swaps are needed.
6. Return the sorted list
c. Output: Sorted list.
5. Flowchart Design Examples
Draw a flow chart to write the output for a simple calculator that
performs addition (+), or subtraction (-), or multiplication (*), or
division (/), on two numbers entered in the following manner: First
number, symbol of operation, second number. If the user enters a
symbol which is not one of the previous operations, the following
message should be displayed "INVALID OPERATION".
6. Flowchart Best Practices
Use Consistent Symbols:
• Ovals for start/end.
• Rectangles for processes.
• Diamonds for decisions.
Label All Shapes:
• Clearly describe what each shape represents.
Keep It Simple:
• Avoid unnecessary complexity.
Use Arrows for Flow:
• Clearly show the direction of control flow.
7. Tools to Create Flowcharts
Draw.io:
• Free online tool for creating flowcharts.
Lucidchart:
• Web-based tool with collaboration features.
Microsoft PowerPoint/Word:
• Use shapes and arrows to manually create flowcharts.