Introduction To Programming
Introduction To Programming
Introduction to Programming
In this module, you will take the first steps on your journey to becoming an amazing
computer programmer by learning how you can instruct a computer to perform
interesting actions and solve complex problems that would take human minds years
to solve.
During the lectures, you are going to learn how to create a set of instructions that a
computer will execute, a program. You will learn how to speak the language of
computers by using programming languages. Also, you will learn how data is used to
store information that your programs can use to solve problems, and how operators
are used to transform (change) your data. Further, you will learn about user-defined
datatypes such as Structures, Unions and Enumerations.
● Learn what data data types are, their importance, and their application in
programming.
● Discover the functionality and structure of operators, and their importance,
and their application in programming.
● Gain insights into constants and literals, understanding how to create and
use them
● Learn user defined datatypes/structures such as: Structures, Unions and
Enumerations
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.
1. In C++, data types are classified into primitive types (such as integers,
characters, and floating points) and abstract types, which are user-defined.
2. Variables in C++ store data and are declared with a specific data type; their
names must follow naming conventions and are used to manage data in
memory.
3. C++ uses cout for output and cin for input, with stream operators (<< and
>>) to handle data display and retrieval.
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
A data type is a classification of data that tells the compiler or interpreter how the
programmer intends to use the data.
● Integer (int): Used for storing whole numbers (100, 20, 4, and so on...).
● Character (char): Used for storing a single character (4, b, $, and so
on...).
● Boolean (bool): Used for storing boolean or logical values (true or false).
Represented in memory as 1 for true and 0 for false.
● Floating point (float): Used for storing values with a decimal point
(3.1415927). Can have up to 7 decimal places.
● Double floating point (double): Used for storing values with a decimal
point (3.141592653589794). Can have up to 15 decimal places.
Data type modifiers are used with primitive data types to modify the length of data
that a particular data type can hold. Data type modifiers available in C++ are:
● Signed
● Unsigned
● Short
● Long
● Signed, unsigned, long and short modifiers can be used for int.
● Signed and unsigned can be used for char.
● Long can be used for double.
Section 2: Variables
Variables are used to store information that can be referenced and manipulated by a
computer program. It is called a variable because we can change the value stored. It
is a way of labelling data with a descriptive name, so our program is easier to
understand.
Variables are like containers that hold information. The purpose of them is to label
and store data in computer memory (RAM). Different types of variables require
different amounts of memory, and have a specific set of operations that can be
applied to them. Imagine a large warehouse with lots of storage bays, tables,
shelves, rooms etc. We can store different types of items in each of these places.
A variable has a valid C++ data type, like the primitive data types we just saw.
Examples are:
○ int: number = 3;
○ float: value = -23.45;
○ bool: finished = false;
○ char: letter = ‘a’;
Variable naming
It can also be used to retrieve the value stored (e.g., circumference = 2* PI * radius).
A name cannot begin with a number, and there are some defined naming
conventions for C++ variables, but there is no particular standard naming
convention, so you should use the naming convention designated by your company
or team.
Scope of a Variable
There are two different types of variables based on the scope.
○ Global Variable
○ Local Variable
Output in C++ is done via cout and the stream insertion operator << chains items.
In the above example, the endl can be used to produce a new line. Whenever an
endl is printed, the cursor moves to the beginning of the next line without any visible
output.
We can also use \n, which denotes a newline character just like endl. Similarly, you
could use \t which denotes tab character, to move the cursor to the next tab position.
Input in C++ is done via cin and the stream extraction operator >> reads input from
the keyboard and stores the value into a variable.
#include <iostream>
int main()
Topic 2. Operators
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: Operators
In the following example, ‘+’ is the addition operator and the variables numOne
and numTwo are operands. The addition operator tells the compiler to add both
numOne and numTwo.
#include <iostream>
int result = 0;
C++ has many built-in operators and they can be classified as:
● Arithmetic operators
● Relational operators
● Logical Operators
● Assignment operators
● Increment and Decrement operators
● Bitwise operators
● Special operators
Arithmetic Operators
- x-y Subtracts the second operand (y) from the first operand
(x)
% x%y The modulus operator divides the first operand (x) by the
second operand (y) and returns the remainder.
Assignment Operators
Relational Operators
Are used for comparing two operands, the result of a relational operator is a boolean
value that is either true or false.
Symbol Usage Explanation
<= x <= y Checks if the value of x is less than or equal to the value
of y.
Logical Operators
These take boolean values as input and return boolean values as output. Here, the x
and y are boolean values.
&& x && y Logical AND, returns true when both conditions are
satisfied. Otherwise, it returns false.
!y
() Parentheses L -> R
<, <=, >, >= Less than, Less than equal to, greater than, greater than
equal to
1. Constants in C++ are fixed values that do not change during program
execution and are defined using the const keyword
2. Literals are specific values directly written into the code, such as numbers or
characters, that represent fixed data of a particular type.
3. Structures and unions in C++ are user-defined types for grouping data, with
structures allowing different types of data to be combined and unions sharing
the same memory space for multiple data types, while enums define a set of
named integer constants for better code readability and management.
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
int x = 5;
Both literals and variables have a value, and they both have a type. The type of a
variable is given in its declaration. The type of a literal is assumed from the value
and format of the literal itself.
Types of Literals
● Integer
● Floating-point (includes float and double)
● Character
● String
● Boolean
Defining constants
Constants are treated just like regular variables except that their values cannot be
modified after their definition. Use a constant variable to avoid accidentally changing
a variable that should not change.
There are two simple ways in C++ to define constants—by using:
● const keyword
● #define preprocessor
const
We can use the const prefix to declare constants with a specific data type. The
syntax for creating a constant variable is:
The constant variables should be initialised with a value when the variable is
created.
If we try to change the value of the constant variable after its initialisation, we will get
an error
#define preprocessor
#define PI 3.141592653589793238
Structures
Structure is a user-defined data type. It is used to combine different data types into
one unit.
Unions
Unions are user-defined data types. Similar to structures, they can declare and
contain members of different data types. How they differ from a structure is that
member variables in a union share the same memory location.
Enumerations (Enums)
Enums are user-defined data types that contain named integral constants. Enums
help to assign constants into a set of names, which makes programs more readable.
Further Resources
The resources in this section are for further exploration, providing additional
information for deeper insights.
Expand All
Panels
Collapse All
Panels
Reading: Converting unsigned integers to signed integers can lead to hidden
bugs
This blog post demonstrates the dangers of converting between two data types that
you learnt about in this week’s lectures, signed and unsigned integers. It is
exceptionally useful to understand how your programs might have accidental
problems, and this is a common source of problems.
Reference: Pluralsight (2016) 'Converting unsigned integers to signed integers can lead to hidden
bugs', available at:
https://www.pluralsight.com/blog/software-development/convert-unsigned-int-to-signed-int [accessed
16/09/2024]
C++ Unions