TDT4102 - Lecture 02 - Value Added Syntax
TDT4102 - Lecture 02 - Value Added Syntax
CPLUSPLUS Lv79
CPLUSPLUS Lv79
CPLUSPLUS Lv79
14.01.2025
01:43 p.m. - 9 Course TDT4102 – Lecture 1
PYTHON Lv31
CPLUSPLUS Lv79
14.01.2025
It’s not very effective..
01:43 p.m. - 10 Course TDT4102 – Lecture 1
PYTHON Lv31
CPLUSPLUS Lv79
CPLUSPLUS Lv79
CPLUSPLUS Lv79
14.01.2025
01:43 p.m. - 13 Course TDT4102 – Lecture 1
PYTHON Lv31
CPLUSPLUS Lv79
14.01.2025
It’s not very effective..
01:43 p.m. - 14 Course TDT4102 – Lecture 1
PYTHON Lv31
CPLUSPLUS Lv79
CPLUSPLUS Lv79
TIGHT CONTROL
14.01.2025
01:43 p.m. - 16 Course TDT4102 – Lecture 1 > SPEEED
PYTHON Lv31
CPLUSPLUS Lv79
14.01.2025
01:43 p.m. - 17 Course TDT4102 – Lecture 1
PYTHON Lv31
CPLUSPLUS Lv79
14.01.2025
01:43 p.m. - 18 Course TDT4102 – Lecture 1
PYTHON has fainted!
14.01.2025
01:43 p.m. - 19 Course TDT4102 – Lecture 1
Today
Basics of the C++ Language
●
Variables and data types
●
Language elements
●
Variables allow you to store values
●
A variable is a “space” that
stores a single value
●
Stored values remain the same
until updated
●
Used all the time!
●
Work the same as in Python
●
Unlike Python, it is possible to create a variable
without assigning a value to it
●
The contents will be undefined!
●
Best practice: always initialise your variables!
●
The compiler will warn you about this:
variableName = 5;
●
The = operator overwrites the
value of the variable
variableName = anotherVariable + 3; ●
The values of variables can be
used in calculations
Prints: ●
Name
Data type (this one is an integer)
●
Why do we care about data types?
●
Which data types exist?
●
How do we choose which one to use?
Demonstration time!
Integers
Real numbers
Miscellaneous
Name
Integer data type
●
Stores whole numbers (0, 42, -273, 1337, ..)
●
Risk: only a certain number of binary digits (bits)
can be stored.
●
When the highest possible value is exceeded,
it loops around to the lowest possible value,
and vice versa
●
Sign: 1 if number is negative, 0 if positive
●
Exponent: which power of 2 the number lies
between (e.g. 2, 4, 8, 16, 32)
●
Negative for numbers between -1 and 1
●
Mantissa: represents how far the number lies
between the selected powers of 2
●
Linearly subdivided
●
32-bit float: subdivided into 223 numbers
10.03.2022 - 29 Course TDT4102 – Lecture 2
Data types: real numbers
double variableName;
●
Stores real numbers (3.14, -273.15)
●
Risk: after each calculation, the result is rounded to the
nearest representable number
●
Causes noise
●
Try to keep numbers close to 0 if possible
● Use double if precision matters
●
Avoid computations combining large and small
numbers
●
Note: adding two floating points together also adds their
errors
●
Usually not a problem unless you do this many times
●
Note: dividing a real number by 0 creates a special value
that can show up as Not A Number (“nan”) or Infinity (“inf”).
Integers
int
Real numbers
double
bool
string
Miscellaneous enum class
struct and class
void (no data type)
●
Types can be converted explicitly using static_cast<>():
int anInteger = 5;
float floatValue = static_cast<float>(anInteger);
●
For converting between numeric types, you can also use the type as a
function:
int anInteger = 5;
float floatValue = float(anInteger);
10.03.2022 - 44 Course TDT4102 – Lecture 2
The name «casting» comes from metallurgy!
14.01.2025
01:43 p.m. - 45 Course TDT4102 – Lecture 2
Type casting
●
Why explicit type conversion when much of it happens implicitly?
●
Types can not always be converted implicitly
●
Explicitly converting a type shows the conversion is intentional
●
And a loss of accuracy is acceptable
●
For numeric types, some calculations require type conversions to
produce correct results
●
Converting numbers to and from strings is a bit different.
We’ll look at those in a future lecture.
Boolean
Operator Description
! Boolean NOT bool inverse = !(3 > 2);
cout << inverse << endl; // 0
&& Boolean AND
|| Boolean OR
10.03.2022 - 50 Course TDT4102 – Lecture 2
Operators
●
C++ does not have Python’s // operator
●
When both operands are integers, integer division is
used. Otherwise, always float division.
●
A float in Python is always 64-bit. In C++ that would be
called a double.
●
The order in which operators are evaluated follows
algebraic rules.
●
Good practice: do not use the == and != operators with
floating point numbers.
if(condition) {
cout << "condition is true!" << endl;
} else {
cout << "condition is false." << endl;
}
●
If statements allow you to run a piece of code based
on a boolean true/false condition
14.01.2025
01:43 p.m. - 56 Course TDT4102 – Lecture 2
If statements
bool condition = true;
bool alternateCondition = true;
if(condition) {
cout << "condition is true!" << endl;
} else if(alternateCondition) {
cout << "condition is false." << endl;
cout << "alternateCondition is true!" << endl;
} else {
cout << "condition is false." << endl;
cout << "alternateCondition is also false." << endl;
}
●
Multiple statements can be chained
14.01.2025
01:43 p.m. - 57 Course TDT4102 – Lecture 2
Today
Basics of the C++ Language
●
Variables
●
Data types
●
Type casting
●
Operators
●
Text input
●
If statements
●
Loops
●
Functions
}
●
The while loop:
int i = 0;
while(i < 10) {
i++;
}
●
The do while loop: ●
Loops are interchangeable: each
int i = 0; can be rewritten as another type.
do { ●
For example, all the shown loops
i++; repeat their loop body 10 times!
} while(i < 10);
10.03.2022 - 59 Course TDT4102 – Lecture 2
Loops
●
Where should you use each loop type?
●
The for loop:
When you know in advance how many
iterations you need
●
The while loop:
When you don’t know in advance how
many iterations you need
●
The do while loop:
When you need the loop body to be
executed at least once
for(int i= int i = 0;
while(i < 10) {
}
i++;
●
Definition: what does the function do? void functionA() {
}
●
Contains code surrounded by { }
void functionB() {
cout << "I am function B" << endl;
}
Example 1
14.01.2025
01:43 p.m. - 67 Course TDT4102 – Lecture 2
That’s all for assignment 1!
14.01.2025
01:43 p.m. - 68 Course TDT4102 – Lecture 2
•
Images used:
https://www.weld2cast.com/sand-casting/
http://vignette1.wikia.nocookie.net/antagonists/images/f/f7/Darth_Vader.png/revision/latest?cb
=20141211094955
https://www.minecraftforum.net/forums/minecraft-java-edition/discussion/2391376-recreating-t
he-far-lands
14.01.2025
01:43 p.m. - 69 Course TDT4102 – Lecture 2