Skip to main content
CBE Nexus · Undergraduate Computer Science

Chapter 2: Programming Fundamentals and Problem Solving

Learn algorithms, pseudocode, flowcharts, variables, data types, expressions, selection, iteration, functions, arrays, debugging, testing and introductory Python programming.

Back to Computer Science course guide

Chapter overview and learning outcomes

This chapter develops the conceptual and practical foundations of programming fundamentals and problem solving. It connects theory with implementation, evaluation and responsible computing practice.

1. Programming languages and translators

A programming language provides rules for expressing computation. High-level languages offer abstractions that improve readability and portability; lower-level languages expose more hardware detail. A compiler translates source code into another representation before execution, while an interpreter executes or evaluates instructions through a runtime. Many modern environments combine compilation, bytecode and just-in-time optimisation. Syntax defines valid structure; semantics defines what a valid program means.

Worked application: Consider a realistic computing task, identify its assumptions and constraints, then trace the relevant process using a small example. Record intermediate states, expected results and possible failure cases. Compare at least one alternative approach and explain the trade-offs in correctness, performance, maintainability and security.

Study focus: Define the key terms, reproduce the concept diagram, explain the mechanism in sequence and solve a short problem without relying on memorised wording.

2. Problem specification and algorithm design

Before coding, define the problem, inputs, outputs, constraints, assumptions and success criteria. An algorithm is a finite, unambiguous sequence of steps that transforms inputs into outputs. Good algorithms handle normal, boundary and invalid cases. A problem statement should be converted into testable requirements. Separate the desired result from implementation details so that alternative algorithms and languages can be compared.

Worked application: Consider a realistic computing task, identify its assumptions and constraints, then trace the relevant process using a small example. Record intermediate states, expected results and possible failure cases. Compare at least one alternative approach and explain the trade-offs in correctness, performance, maintainability and security.

Study focus: Define the key terms, reproduce the concept diagram, explain the mechanism in sequence and solve a short problem without relying on memorised wording.

3. Pseudocode and flowcharts

Pseudocode expresses algorithm logic in structured plain language without requiring a particular programming language. Flowcharts use standard symbols: ovals for start or end, rectangles for processes, diamonds for decisions and arrows for flow. Both tools expose missing branches and ambiguous steps before code is written. Keep each step precise, use meaningful names and ensure every path reaches a defined outcome.

Worked application: Consider a realistic computing task, identify its assumptions and constraints, then trace the relevant process using a small example. Record intermediate states, expected results and possible failure cases. Compare at least one alternative approach and explain the trade-offs in correctness, performance, maintainability and security.

Study focus: Define the key terms, reproduce the concept diagram, explain the mechanism in sequence and solve a short problem without relying on memorised wording.

4. Variables, constants and data types

A variable is a named reference to a value that may change during execution. A constant represents a value intended not to change. Common types include integers, floating-point numbers, strings, booleans and collection types. Type systems help determine valid operations and catch errors. Explicit conversion may be necessary when combining values. Names should communicate meaning, and scope determines where a name can be accessed.

Worked application: Consider a realistic computing task, identify its assumptions and constraints, then trace the relevant process using a small example. Record intermediate states, expected results and possible failure cases. Compare at least one alternative approach and explain the trade-offs in correctness, performance, maintainability and security.

Study focus: Define the key terms, reproduce the concept diagram, explain the mechanism in sequence and solve a short problem without relying on memorised wording.

5. Expressions, operators and precedence

Expressions combine values, variables and operators to produce results. Arithmetic operators include addition, subtraction, multiplication, division and remainder. Relational operators compare values; logical operators combine conditions. Operator precedence determines evaluation order, but parentheses improve clarity and reduce mistakes. Integer division, floating-point rounding and type conversion can produce unexpected results, so numerical assumptions should be tested.

Worked application: Consider a realistic computing task, identify its assumptions and constraints, then trace the relevant process using a small example. Record intermediate states, expected results and possible failure cases. Compare at least one alternative approach and explain the trade-offs in correctness, performance, maintainability and security.

Study focus: Define the key terms, reproduce the concept diagram, explain the mechanism in sequence and solve a short problem without relying on memorised wording.

6. Selection and decision structures

Selection executes different statements depending on a condition. The if, elif and else structure supports mutually exclusive branches; nested conditions can express more complex decisions. Conditions should be explicit and cover all relevant cases. Avoid duplicated logic where a clearer combined condition or function is possible. Test true and false branches, boundary values and unexpected input.

Worked application: Consider a realistic computing task, identify its assumptions and constraints, then trace the relevant process using a small example. Record intermediate states, expected results and possible failure cases. Compare at least one alternative approach and explain the trade-offs in correctness, performance, maintainability and security.

Study focus: Define the key terms, reproduce the concept diagram, explain the mechanism in sequence and solve a short problem without relying on memorised wording.

7. Iteration and loops

Iteration repeats a block of statements. A for loop commonly iterates over a sequence or known range; a while loop repeats while a condition remains true. Every loop should have a clear termination condition. Off-by-one errors, infinite loops and incorrect updates are common defects. Loop invariants can help reason about correctness: a property that remains true before and after each iteration.

Worked application: Consider a realistic computing task, identify its assumptions and constraints, then trace the relevant process using a small example. Record intermediate states, expected results and possible failure cases. Compare at least one alternative approach and explain the trade-offs in correctness, performance, maintainability and security.

Study focus: Define the key terms, reproduce the concept diagram, explain the mechanism in sequence and solve a short problem without relying on memorised wording.

8. Functions, parameters and return values

A function packages a task behind a named interface. Parameters receive input; a return value communicates the result. Functions improve reuse, readability, testing and separation of concerns. A good function has a focused responsibility and a clear contract. Distinguish local variables from global state and avoid hidden side effects when possible. Decomposition allows large problems to be solved and tested in smaller units.

Worked application: Consider a realistic computing task, identify its assumptions and constraints, then trace the relevant process using a small example. Record intermediate states, expected results and possible failure cases. Compare at least one alternative approach and explain the trade-offs in correctness, performance, maintainability and security.

Study focus: Define the key terms, reproduce the concept diagram, explain the mechanism in sequence and solve a short problem without relying on memorised wording.

9. Collections: strings, lists and dictionaries

Strings represent text and support indexing, slicing and operations such as search and replacement. Lists store ordered collections and can be modified. Tuples are commonly used for fixed ordered groupings. Dictionaries associate keys with values and support efficient lookup by key in typical implementations. Choose structures according to required operations, ordering, mutability and data relationships.

Worked application: Consider a realistic computing task, identify its assumptions and constraints, then trace the relevant process using a small example. Record intermediate states, expected results and possible failure cases. Compare at least one alternative approach and explain the trade-offs in correctness, performance, maintainability and security.

Study focus: Define the key terms, reproduce the concept diagram, explain the mechanism in sequence and solve a short problem without relying on memorised wording.

10. Debugging, testing and error handling

Debugging is the systematic process of locating and correcting defects. Reproduce the problem, isolate the cause, inspect state, form a hypothesis and verify the fix. Testing compares actual behaviour with expected behaviour. Unit tests target small components; integration tests examine interactions; system and acceptance tests examine broader requirements. Exceptions should be handled deliberately, not hidden by broad empty catch blocks.

Worked application: Consider a realistic computing task, identify its assumptions and constraints, then trace the relevant process using a small example. Record intermediate states, expected results and possible failure cases. Compare at least one alternative approach and explain the trade-offs in correctness, performance, maintainability and security.

Study focus: Define the key terms, reproduce the concept diagram, explain the mechanism in sequence and solve a short problem without relying on memorised wording.

11. Readable, secure and maintainable code

Readable code uses meaningful identifiers, consistent formatting, small functions, comments that explain intent and clear documentation. Maintainability also depends on modular design, version control and automated tests. Validate external input, avoid hard-coded secrets, use parameterised database queries and keep dependencies updated. Security should be considered during design rather than added only after deployment.

Worked application: Consider a realistic computing task, identify its assumptions and constraints, then trace the relevant process using a small example. Record intermediate states, expected results and possible failure cases. Compare at least one alternative approach and explain the trade-offs in correctness, performance, maintainability and security.

Study focus: Define the key terms, reproduce the concept diagram, explain the mechanism in sequence and solve a short problem without relying on memorised wording.

Figure 1. Program development cycle
Specify problem and inputs→Design algorithm→Implement source code→Test and debug→Document and maintain

Conceptual learning diagram; use alongside the explanations and practical work.

Figure 2. Control structures
Sequence: ordered steps→Selection: choose a branch→Iteration: repeat a block

Conceptual learning diagram; use alongside the explanations and practical work.

Figure 3. Function call
Caller passes arguments→Function executes statements→Return value or result→Caller continues

Conceptual learning diagram; use alongside the explanations and practical work.

Practical laboratory activity

  1. Write a short problem specification, listing inputs, outputs and constraints.
  2. Draw a diagram or write pseudocode before implementation.
  3. Implement a small example in an appropriate language or tool.
  4. Test normal, boundary and invalid cases; record actual and expected results.
  5. Explain the design trade-offs and suggest one improvement.

Use authorised software and test environments. Do not run experiments on systems or data without permission.

Worked reasoning and self-check

For every problem, state the assumptions first. Work through a small input manually, record each important state transition and compare the final result with the specification. If the result differs, isolate the smallest failing example and revise the reasoning before changing code. For performance questions, identify the input-size variable and count the dominant operation rather than guessing from execution time alone.

Examination and revision questions

Answer with definitions, explanations, diagrams, calculations or code traces as appropriate. Show working for numerical and algorithmic questions.

  1. Define an algorithm.

    Show answer guidance

    A finite, unambiguous sequence of steps that transforms inputs into required outputs.

  2. What is pseudocode?

    Show answer guidance

    A language-independent way of expressing algorithm steps in structured plain language.

  3. Differentiate a compiler and interpreter.

    Show answer guidance

    A compiler translates source code before execution; an interpreter evaluates or executes code through a runtime.

  4. What is a variable?

    Show answer guidance

    A named reference used to store or access a value during program execution.

  5. What is a Boolean value?

    Show answer guidance

    A logical value that is either true or false.

  6. What is selection in programming?

    Show answer guidance

    Conditional execution of statements based on whether a condition is satisfied.

  7. Differentiate for and while loops.

    Show answer guidance

    A for loop commonly iterates over a sequence or range; a while loop repeats while a condition remains true.

  8. Why are functions useful?

    Show answer guidance

    They encapsulate tasks, promote reuse, improve testing and separate program responsibilities.

  9. What is an array or list?

    Show answer guidance

    An ordered collection of elements accessible through positions or indices.

  10. What is debugging?

    Show answer guidance

    A systematic process of locating, understanding and correcting software defects.

  11. Differentiate syntax and logic errors.

    Show answer guidance

    Syntax errors violate language grammar; logic errors allow execution but produce incorrect results.

  12. What is a test case?

    Show answer guidance

    A defined input, conditions and expected outcome used to check program behaviour.

  13. Why validate user input?

    Show answer guidance

    To reject malformed or unsafe data and preserve program correctness and security.

  14. What is a return value?

    Show answer guidance

    The result a function sends back to its caller.

  15. What is an infinite loop?

    Show answer guidance

    A loop that fails to reach its termination condition.

Revision checklist

Further study and references

Consult current university textbooks, peer-reviewed computing research, official language and standards documentation, processor or operating-system manuals, and reputable open educational resources. Check edition, publication date and technical version. Programming language behaviour, security recommendations and software tools evolve; verify details against current official documentation.

Academic and responsible-use note: These notes support undergraduate learning. Verify course requirements with your university, cite sources in assessed work, respect software licences and data privacy, and test only systems for which you have authorisation.

Return to the full Computer Science course guide

Official Education References

Use these official organisations for curriculum guidance, examinations and further learning information.