Control Structures and Data Structures I
Control Structures and Data Structures I
The process of creating paths that your program can follow is achieved with
conditional statements. This is the basis of AI (Artificial Intelligence) as your program
is now able to make decisions for itself based on the data you provide it!
During the lectures, you will learn about the many conditional statements that C++
provides, such as the If condition statement that lets your program execute
instructions only if some data is a specific value. If it is not that specific value you can
instruct your program to execute other instructions using an else conditional
statement.
The material in this module relates to the following subject learning outcomes:
SLO 2: Gain the ability to create and modify basic data structures such as
arrays and linked lists, along with performing operations like insertion,
deletion, and traversal.
Student Expectations
During this module, you are expected to spend time working through the prescribed
material prior to attending class. This will enable a more valuable discussion to take
place in online tutorials or conferences, where you will be able to discuss concepts,
present ideas and explore information collaboratively in greater depth.
4 hours for facilitated study. This includes guided facilitated study, watching or
attending course lectures (live or pre-recorded), responding to facilitator feedback.
8 hours for personal study. This includes reading through course material,
undertaking learning activities and exploring additional related resources.
Learner Guide
The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All
Panels
Collapse All
Panels
This module introduces you to the conditional statements that C++ provides—if, if
else, if … else, switch, ?:, and also how to nest these conditions amongst each other.
To use these statements you will need to use relational operators which you learnt
about in the previous module, and we will recap again.
By combining both relational operators and conditional statements you can make
your programs make decisions! To understand the decisions that the program makes
you will learn about flowcharts which will provide a visual way of representing the
decisions and the outcomes.
if statement
Example:
{
cout << “You may enter!\n”;
}
if...else statement
Depending on the outcome of the condition, the if...else statement causes one of the
two possible statement(s) to execute.
{
cout << “You may enter!\n”;
}
else
{
cout << “You aren't old enough.\n”;
}
A switch case statement is used when we have multiple conditions that lead to
different outcomes. We can use lengthy if...else if...else, but it gets messy with more
than a few possible outcomes.
The switch case is a clean and efficient method of handling multiple conditions.
Example:
switch (age)
case 16:
break;
case 18:
break;
case 50:
break;
default:
break;
Topic 2. Loops
1. The for loop in C++ is used for iterating over a sequence of values with a
known number of iterations, defined by initialisation, condition, and increment
expressions.
2. The while loop repeatedly executes a block of code as long as a specified
condition remains true, suitable for scenarios where the number of iterations
is not known in advance.
3. The do-while loop guarantees that the block of code will run at least once
before checking the condition, making it useful for cases where an initial
execution is necessary.
Learner Guide
The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All
Panels
Collapse All
Panels
Section 1: Loops
This topic introduces you to the three types of loop statements that C++
provides—for, while, do while. Like conditional statements, you need to use relational
operators. By combining both relational operators and loop statements you can
instruct your programs to execute a set of instructions as many times as you like.
You can also instruct the program to stop a loop by using special keywords in C++,
which we will soon cover.
Imagine a game that runs until the player closes it, to achieve this you will tell the
computer to run a sequence of instructions over and over again, using a loop, until
the player presses the quit button. Loop statements allow you to specify how many
times you want the computer to run a specific set of instructions. Flowcharts will
again be demonstrated to visualise how the computer will loop over your instructions.
So far, we have been writing statements that get executed sequentially, one
statement followed by another. Loops allow a programmer to repeatedly execute a
block of statements until a condition is satisfied.
Let’s say we want to print “Hello, world!” 5 times. We could do it sequentially, like
this:
Or we can use loops to make our code leaner, more efficient and DRY.
Loops are one tool you can use to keep code DRY.
Loop types
for loop
Example:
int main()
{
return 0;
}
while loop
while (condition)
{
statement(s);
}
Example:
int main()
{
int m = 1;
return 0;
do...while loop
Syntax:
do
{
statement(s);
} while (condition);
Example:
int main()
{
int m = 1;
do
{
cout << "Value of m: " << m << endl;
m++;
} while (m <= 6);
return 0;
}
Nested loops
The body of a loop can contain another loop known as a nested loop. The inner loop
must be completed each time through the outer loop.
In the following example, the ‘row’ loop starts first, then enters the ‘column’ loop and
executes the statements inside the column loop until it reaches 5 loops or iterations.
Then, it exits out of the column loop and goes through the row loop again as the
steps are repeated until the number of iterations through the ‘row’ loop reaches 5.
Sometimes, you might want to skip the execution of a loop for a certain condition or
terminate it immediately without checking the condition. In C++ there are two
statements to specifically alter the normal flow of a loop. They are:
● break
● continue
For example, we want to loop through the data of all aged people except those aged
55. Or if we want to find the first person aged 20. In these scenarios, break and
continue statements can be used.
The Learner Guide provides the core knowledge for this topic. Work through the
materials and be prepared to discuss these in your forums, tutorials or classes.
Expand All
Panels
Collapse All
Panels
Data structures serve as the backbone for organising information in the realm of
programming. Much like how a library arranges books on shelves, data structures
help you organise and store data in your programs. Understanding how to properly
utilise these structures will set you on a path towards becoming a proficient
programmer.
Different tasks call for different types of organisation. In the world of data structures,
you have several options to fit your specific needs:
Linear Structures
● Arrays: Fixed in size, arrays offer quick access but fall short when it
comes to dynamic resizing. They’re best for situations where you know the
size of the data set in advance.
Hierarchical Structures
● Trees: Imagine a tree with a trunk, branches, and leaves. A tree structure
starts with a root node, which branches off into several sub-nodes, and so
on. Trees are commonly used in databases to facilitate quick data
retrieval.
● Graphs: Unlike trees, graphs can have nodes that are interconnected in
complex ways, allowing for a more flexible representation of relationships
between elements. They’re commonly used in network routing and social
networking applications.
Tabular Structures
● Hash Tables: These are akin to a magical library where you whisper the
name of a book and it appears before you. Hash tables use a unique key
to store data, offering almost instantaneous data lookup.
Further Reading
The choice of a good data structure makes it possible to perform a variety of critical
operations effectively. A data structure is not only used for organising data but also
for processing, retrieving, and storing data. Explore further the different categories of
data structures and the most commonly used types.
Reference: Geeks for Geeks n.d., Introduction to Data Structures, Geeks for Geeks, accessed 30
January 2024, <https://www.geeksforgeeks.org/introduction-to-data-structures/>
● Efficiency
The right data structure optimises both time and memory. In turn, this
makes your program run more swiftly, making tasks like data retrieval and
storage more effective.
● Ease of Operations
Data structures make it simpler to add, remove, or modify data. As your
programs grow more complex, this flexibility becomes increasingly
important.
● Data Organisation
When data is well-organised, your algorithms run more smoothly, making
your programs more reliable and user-friendly.
Section 3: QuickShop Case Study
source: freepik
Background
As the inventory grew, the existing system struggled to maintain performance levels,
causing delays in various processes including stock checks and billing. Realising the
critical role of data structures in efficient data management, QuickShop decided to
revamp its inventory system.
The Challenge
The central challenge was to design a system that could handle a vast and dynamic
range of products while ensuring quick and accurate data retrieval. The system
needed to facilitate seamless additions and removals of items, track inventory levels
in real-time, and offer rapid search capabilities to enhance the customer experience
at the checkout points.
The Strategy
Implementation
● Increased Efficiency
The system allowed for quicker data access and manipulation,
considerably reducing the time taken for inventory management and billing
processes.
● Ease of Scalability
As QuickShop expanded, adding new products and categories to the
inventory became a seamless process, enabling the supermarket to grow
without being hindered by its systems.
Further Resources
The resources in this section are for further exploration, providing additional
information for deeper insights.
Expand All
Panels
Collapse All
Panels
Reading: C++ Decision-Making with if, if...else
This article explains in detail, with examples, how to effectively use the if, if...else
and Nested if...else decision-making statements in C++.
Reference: Programiz, 2024, 'C++ if, if...else and Nested if...else', Programiz (online), accessed 16
Sept 2024, <https://www.programiz.com/cpp-programming/if-else>
Reference: Programiz, 2024, 'C++ switch..case Statement', Programiz, accessed 16 Sept 2024,
<https://www.programiz.com/cpp-programming/switch-case>
Reference: Programiz, 2024, 'Flow Control', C++ Programming, Programiz, accessed 16 Sept. 2024,
<https://www.programiz.com/cpp-programming/>
" that covers all of the concepts from this and previous modules.
Reference: CodeBeauty, 2021, 'Data Structures Explained', YouTube, accessed 16 Sept. 2024,
<https://www.youtube.com/watch?v=_T42E9RkWVQ>