0% found this document useful (0 votes)
5 views

Practical Programming -L04

The document provides an overview of pseudocode, highlighting its importance in algorithm design and communication. It covers basic constructs such as SEQUENCE, IF-THEN-ELSE, WHILE, and FOR, along with rules for writing effective pseudocode. Additionally, it includes real-world examples, common mistakes to avoid, and practical exercises for learners.

Uploaded by

Jaafar Abbakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Practical Programming -L04

The document provides an overview of pseudocode, highlighting its importance in algorithm design and communication. It covers basic constructs such as SEQUENCE, IF-THEN-ELSE, WHILE, and FOR, along with rules for writing effective pseudocode. Additionally, it includes real-world examples, common mistakes to avoid, and practical exercises for learners.

Uploaded by

Jaafar Abbakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

University of Juba

Programming with C++


Practical Training
Lecture 4: Pseudocode
Overview
1.Introduction to Pseudocode
2.Basic Constructs of Pseudocode
3.Rules for Writing Effective Pseudocode
4.Input, Output, and Assignment
5.Real-World Examples
6.Interactive Practice Problems
7.Common Mistakes and How to Avoid Them
8.Summary and Next Steps
9.Homework and Challenges
1. Introduction to Pseudocode
What is Pseudocode?
• Pseudocode is a simple, human-readable way to describe the
steps of an algorithm.
• It is not actual code but acts as a blueprint for writing code in
any programming language.
• Think of it as a bridge between your ideas and the actual
implementation in a programming language.
1. Introduction to Pseudocode
Why Use Pseudocode?
• Helps in planning and designing algorithms before coding.
• Reduces errors by clarifying logic.
• Makes it easier to communicate ideas with others.
• Provides a structured way to break down complex problems into
manageable steps.
2. Basic Constructs of Pseudocode
• Pseudocode uses six main constructs to describe the flow of an
algorithm.
• These constructs are written in uppercase for clarity:
2. Basic Constructs of Pseudocode
SEQUENCE: Steps executed in order, one after the other.
Example:
Step 1: Boil water
Step 2: Add tea leaves
Step 3: Add milk
Step 4: Serve tea
2. Basic Constructs of Pseudocode
IF-THEN-ELSE: Conditional statements that execute different steps
based on a condition.
Example:
IF temperature > 30 THEN
WRITE "It's hot outside."
ELSE
WRITE "It's cool outside."
END IF
2. Basic Constructs of Pseudocode
WHILE: A loop that repeats steps as long as a condition is true.
Example:
WHILE hunger == true DO
Eat a snack
END WHILE
2. Basic Constructs of Pseudocode
REPEAT-UNTIL: A loop that repeats steps until a condition
becomes true.
Example:
REPEAT
Study for exam
UNTIL confidence_level > 80
2. Basic Constructs of Pseudocode
FOR: A loop that repeats steps a specific number of times.
Example:
FOR i = 1 TO 10 DO
WRITE "Hello"
END FOR
2. Basic Constructs of Pseudocode
CASE: A generalization of IF-THEN-ELSE for multiple conditions.
Example:
CASE grade OF
'A': WRITE "Excellent"
'B': WRITE "Good"
'C': WRITE "Average"
ELSE: WRITE "Needs improvement"
END CASE
3. Rules for Writing Effective Pseudocode
1. Capitalize Constructs: Always capitalize keywords like IF, WHILE, FOR,
etc.
2. Use Indentation: Show hierarchy and improve readability.
• Example:
IF x > 0 THEN
WRITE "Positive"
ELSE
WRITE "Negative"
END IF
3. One Statement Per Line: Keep the pseudocode clean and easy to follow.
4. Use Clear Variable Names: Use meaningful names like total_score instead
of ts.
5. Avoid Language-Specific Syntax: Keep pseudocode independent of any
programming language.
6. End Multi-Line Sections: Use END IF, END WHILE, etc., to close blocks.
4. Input, Output, and Assignment
• Input: Use READ to get input from the user.
Example:
READ age

• Output: Use WRITE to display results.


Example:
WRITE "Your age is", age

• Assignment: Use ← or = to assign values to variables.


Example:
total ← 0
total = total + 10
5. Real-World Examples
Example 1: Sum of Two Numbers
READ num1, num2
sum ← num1 + num2
WRITE "The sum is", sum
Example 2: Check if a Number is Even or Odd
READ number
IF number % 2 == 0 THEN
WRITE "Even"
ELSE
WRITE "Odd"
END IF
Example 3: Print Numbers from 1 to 10
FOR i = 1 TO 10 DO
WRITE i
END FOR
6. Interactive Practice Problems
1. Problem 1: Write pseudocode to find the maximum of three
numbers.
2. Problem 2: Write pseudocode to calculate the factorial of a
number.
3. Problem 3: Write pseudocode to check if a number is prime.
6. Interactive Practice Problems
Problem 1: Write pseudocode to find the maximum of three
numbers
IF a > b AND a > c THEN
RETURN a
ELSE IF b > a AND b > c THEN
RETURN b
ELSE
RETURN c
END IF
6. Interactive Practice Problems
Problem 2: Write pseudocode to calculate the factorial of a number.
IF n == 0 OR n == 1 THEN
RETURN 1
ELSE
RETURN n * factorial(n - 1)
END IF
6. Interactive Practice Problems
Problem 3: Write pseudocode to check if a number is prime.
IF n <= 1 THEN
RETURN FALSE
END IF

FOR i = 2 TO SQRT(n) DO
IF n % i == 0 THEN
RETURN FALSE
END IF
END FOR
7. Common Mistakes to Avoid
• Common Mistakes and How to Avoid Them
• Mistake 1: Using language-specific syntax (e.g., cout or printf).
• Solution: Stick to generic constructs like WRITE and READ.
• Mistake 2: Forgetting to capitalize constructs.
• Solution: Always capitalize keywords like IF, WHILE, FOR, etc.
• Mistake 3: Not using indentation.
• Solution: Use indentation to show hierarchy and improve
readability.
• Mistake 4: Writing overly complex pseudocode.
• Solution: Keep it simple and focus on clarity.
8. Summary
• Pseudocode is a powerful tool for designing algorithms.

• It uses constructs like SEQUENCE, IF-THEN-ELSE, WHILE,


FOR, and CASE to describe logic.

• Writing clear and concise pseudocode is essential for


translating ideas into actual code.
8. Next Steps
• Practice: Write pseudocode for simple problems like calculating
the average of numbers or finding the largest number in a list.

• Translate to Code: Convert your pseudocode into actual code


using C++ or another programming language.

• Explore More: Learn about advanced constructs like functions


and exception handling in pseudocode.
8. Next Steps
9. Quick Quiz:
• What is the purpose of pseudocode?

• Write pseudocode to check if a number is positive, negative, or


zero.

• What is the difference between a WHILE loop and a REPEAT-


UNTIL loop?
Q&A and Discussion
9. Homework Assignment
Write pseudocode for the following problems:
1. Calculate the sum of all even numbers between 1 and 100.
2. Find the smallest number in a list of 5 numbers.
3. Simulate a simple login system (check username and
password).

You might also like