0% found this document useful (0 votes)
3 views69 pages

TDT4102 - Lecture 02 - Value Added Syntax

Uploaded by

jentoft.sondre
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)
3 views69 pages

TDT4102 - Lecture 02 - Value Added Syntax

Uploaded by

jentoft.sondre
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/ 69

Lecture 2: Value Added Syntax

Bart Iver van Blokland

10.03.2022 - 1 Course TDT4102 – Lecture 2 Pausemusikk: Jean Michel Jarre


Last time..

Project configuration

Program compilation

The main function

Writing to the terminal using cout

Debugging

10.03.2022 - 2 Course TDT4102 – Lecture 2


Today
Basics of the C++ Language

Variables and data types

Language elements

10.03.2022 - 3 Course TDT4102 – Lecture 2


You are challenged by Ekans!
14.01.2025
01:43 p.m. - 4 Course TDT4102 – Lecture 1
Ekans sent out Python!
14.01.2025
01:43 p.m. - 5 Course TDT4102 – Lecture 1
Go CPLUSPLUS!
14.01.2025
01:43 p.m. - 6 Course TDT4102 – Lecture 1
PYTHON Lv31

CPLUSPLUS Lv79

What will CPLUSPLUS do? > FIGHT LANGS

14.01.2025 ITEM RUN


01:43 p.m. - 7 Course TDT4102 – Lecture 1
PYTHON Lv31

CPLUSPLUS Lv79

> AVAILABLE LIBRARIES


What will CPLUSPLUS do?
TIGHT CONTROL
14.01.2025
01:43 p.m. - 8 Course TDT4102 – Lecture 1 SPEEED
PYTHON Lv31

CPLUSPLUS Lv79

CPLUSPLUS used AVAILABLE LIBRARIES!

14.01.2025
01:43 p.m. - 9 Course TDT4102 – Lecture 1
PYTHON Lv31

CPLUSPLUS Lv79

CPLUSPLUS used AVAILABLE LIBRARIES!

14.01.2025
It’s not very effective..
01:43 p.m. - 10 Course TDT4102 – Lecture 1
PYTHON Lv31

CPLUSPLUS Lv79

What will PYTHON do? > FIGHT LANGS

14.01.2025 ITEM RUN


01:43 p.m. - 11 Course TDT4102 – Lecture 1
PYTHON Lv31

CPLUSPLUS Lv79

What will PYTHON do? > NO NEED TO COMPILE


AVAILABLE LIBRARIES
14.01.2025
01:43 p.m. - 12 Course TDT4102 – Lecture 1 PIP INSTALL
PYTHON Lv31

CPLUSPLUS Lv79

PYTHON used NO NEED TO COMPILE!

14.01.2025
01:43 p.m. - 13 Course TDT4102 – Lecture 1
PYTHON Lv31

CPLUSPLUS Lv79

PYTHON used NO NEED TO COMPILE!

14.01.2025
It’s not very effective..
01:43 p.m. - 14 Course TDT4102 – Lecture 1
PYTHON Lv31

CPLUSPLUS Lv79

What will CPLUSPLUS do? > FIGHT LANGS

14.01.2025 ITEM RUN


01:43 p.m. - 15 Course TDT4102 – Lecture 1
PYTHON Lv31

CPLUSPLUS Lv79

What will CPLUSPLUS do? AVAILABLE LIBRARIES

TIGHT CONTROL
14.01.2025
01:43 p.m. - 16 Course TDT4102 – Lecture 1 > SPEEED
PYTHON Lv31

CPLUSPLUS Lv79

CPLUSPLUS used SPEEEED!

14.01.2025
01:43 p.m. - 17 Course TDT4102 – Lecture 1
PYTHON Lv31

CPLUSPLUS Lv79

It’s SUPER effective!!

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

10.03.2022 - 20 Course TDT4102 – Lecture 2


Today
Basics of the C++ Language

Variables and data types

Language elements

10.03.2022 - 21 Course TDT4102 – Lecture 2


Variables
int variableName; int variableName = 5;

Name Can optionally be initialised to a value


Type (this one is an integer) (and you really should)


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

10.03.2022 - 22 Course TDT4102 – Lecture 2


Variables
int variableName;
cout << "The value of variableName is: " << variableName << endl;


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:

[6/13] Compiling C++ object program.p/main.cpp.o


../main.cpp: In function ‘int main()’:
../main.cpp:9:17: warning: ‘variableName’ is used uninitialized [-Wuninitialized]
9 | cout << variableName << endl;
| ^~~~~

10.03.2022 - 23 Course TDT4102 – Lecture 2


Variables
int variableName = 10; ●
Variables should be initialised
int anotherVariable{6}; with a value to avoid weird errors

variableName = 5;

The = operator overwrites the
value of the variable

variableName = anotherVariable + 3; ●
The values of variables can be
used in calculations

Prints: ●

std::cout << variableName << endl; 9


std::cout << anotherVariable << endl; 6
The contents of variables are
thus independent!
10.03.2022 - 24 Course TDT4102 – Lecture 2
Data types
int variableName;

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!

10.03.2022 - 25 Course TDT4102 – Lecture 2


Data Types Category Data Type

Integers

Real numbers

Miscellaneous

10.03.2022 - 26 Course TDT4102 – Lecture 2


Data types: integers
int variableName;

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

10.03.2022 - 27 Course TDT4102 – Lecture 2


The effects of data types: Minecraft’s far lands
The far lands in beta 1.7.3 were
caused by an integer overflow.

The terrain generator multiplies


all block coordinates by
approximately 171.

A part of this algorithm converts


that number into an integer.

After 12,550,224 blocks this


causes an integer overflow!
The far lands

Perlin noise, used


in early versions of
minecraft
10.03.2022 - 28 Course TDT4102 – Lecture 2
Data types: real numbers
double variableName;

Format of a 32-bit float:


sign (1 bit) exponent (8 bits) mantissa (23 bits)
0 10000101 00010100000000000000000


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

● Example: 32-bit float can only represent increments of


0.25 for numbers between ~2 100 000 and ~4 200 000
10.03.2022 - 30 Course TDT4102 – Lecture 2
Data types: real numbers
double variableName;


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”).

10.03.2022 - 31 Course TDT4102 – Lecture 2


The effects of data types: Minecraft’s far lands
Movement was choppy in one
direction but not in the other.
x-coordinate: > 12 million
Why? Movement is choppy
The higher a floating point
number gets, the larger the
distance between numbers they
can represent.
z-coordinate: almost 0
The game stores coordinates as Movement is smooth
a 64-bit double, but at some point
converted them to a 32-bit float

Around 12 million, 32-bit floats


can only represent integers

10.03.2022 - 32 Course TDT4102 – Lecture 2


Data Types Category Data Type These are the data types that are
char usually enough for most programs.

Integers
int

Real numbers
double
bool
string
Miscellaneous enum class
struct and class
void (no data type)

10.03.2022 - 33 Course TDT4102 – Lecture 2


Data Types Category Data Type
char
unsigned char
short
unsigned short
Integers
int
unsigned int
long long
unsigned long long
float
Real numbers
double
bool
string
Miscellaneous enum class
struct and class
void (no data type)

10.03.2022 - 34 Course TDT4102 – Lecture 2


Data Types Category Data Type Number of bytes
char 1 One byte is 8 bits
unsigned char 1
short 2 One bit is a zero or one
unsigned short 2
Integers Example: 01001011
int 4
unsigned int 4 Number of bytes
long long 8 influences the number of
unsigned long long 8 values a type can store,
float 4 and how much space it
Real numbers
double
occupies
8
bool usually 1
string variable
Miscellaneous enum class usually 4
struct and class variable
void (no data type) not applicable

10.03.2022 - 35 Course TDT4102 – Lecture 2


Data Types Category Data Type Lowest value Highest value
char -128 127
unsigned char 0 255
short -32,768 32,767
unsigned short 0 65,535
Integers
int -2,147,483,648 2,147,483,647
unsigned int 0 4,294,967,295
long long -9,223,372,036,854,775,808 9,223,372,036,854,775,807
unsigned long long 0 18,446,744,073,709,551,615
float -∞ ∞
Real numbers
double -∞ ∞
bool not applicable not applicable
string not applicable not applicable
Miscellaneous enum class not applicable not applicable
struct and class not applicable not applicable
void (no data type) not applicable not applicable

10.03.2022 - 36 Course TDT4102 – Lecture 2


Data Types
4 questions:

Why are there different integer and real types?

Why are there signed and unsigned integers?

What happens if the integer values go out of range?

How do I decide what data type to use?

10.03.2022 - 37 Course TDT4102 – Lecture 2


Data Types
4 questions:

Why are there different integer and real types?

We sometimes work with data which uses a specific
number of bytes per value.

Smaller data types may result in better performance

Why are there signed and unsigned integers?

What happens if the integer values go out of range?

How do I decide what data type to use?

10.03.2022 - 38 Course TDT4102 – Lecture 2


Data Types
4 questions:

Why are there different integer and real types?

Why are there signed and unsigned integers?

Modern processors support both

Use signed for day-to-day computations

Use unsigned for manipulating bits,
or to show a value is always positive

What happens if the integer values go out of range?

How do I decide what data type to use?

10.03.2022 - 39 Course TDT4102 – Lecture 2


Data Types
4 questions:

Why are there different integer and real types?

Why are there signed and unsigned integers?

What happens if the integer values go out of range?

The value “loops around”, so probably bad things!

How do I decide what data type to use?

10.03.2022 - 40 Course TDT4102 – Lecture 2


Data Types
4 questions:

Why are there different integer and real types?

Why are there signed and unsigned integers?

What happens if the integer values go out of range?

How do I decide what data type to use?
● Need an integer? You usually want to use int

● Need a real number? You usually want to use double

● bool and string should be obvious

10.03.2022 - 41 Course TDT4102 – Lecture 2


Data Types

Quiz: which data type?

10.03.2022 - 42 Course TDT4102 – Lecture 2


Today
Basics of the C++ Language

Variables

Data types

Type casting

Operators

Text input

If statements

Loops

Functions

(this will be all you need to complete assignment 1)

10.03.2022 - 43 Course TDT4102 – Lecture 2


Type casting

Casting is conversion of data types

This can lead to precision or data loss, but the compiler can warn you

Types are converted implicitly if no accuracy is lost

Rule of thumb: if a type uses more bytes, you can safely convert to it
● For example, char to int or float to double


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.

10.03.2022 - 46 Course TDT4102 – Lecture 2


Today
Basics of the C++ Language

Variables

Data types

Type casting

Operators

Text input

If statements

Loops

Functions

(this will be all you need to complete assignment 1)

10.03.2022 - 47 Course TDT4102 – Lecture 2


Operators

Roughly two main categories (that we use in this course):

Arithmetic operators: compute stuff

Logic operators: compare stuff

10.03.2022 - 48 Course TDT4102 – Lecture 2


Arithmetic operators
Calculation Assignment Examples
Operator Description Operator Description int sum = 5 + 3;
cout << sum << endl; // 8
+ Addition += Increment by
- Subtraction -= Decrement by sum++;
cout << sum << endl; // 9
* Multiplication *= Multiply by
/ Division /= Divide by sum *= 3;
cout << sum << endl; // 27
% Modulo %= Remainder of
= Assignment
++ Increment by 1
-- Decrement by 1

10.03.2022 - 49 Course TDT4102 – Lecture 2


Logic operators
Value comparison
Operator Description Examples
< Is less than
bool lessThan = 5 < 4;
<= Is less than or equal to cout << lessThan << endl; // 0
== Is equal to
bool notEqual = 3 != 9;
!= Is not equal to cout << notEqual << endl; // 1
>= Is greater than or equal to
> Is greater than

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.

10.03.2022 - 51 Course TDT4102 – Lecture 2


Today
Basics of the C++ Language

Variables

Data types

Type casting

Operators

Text input

If statements

Loops

Functions

(this will be all you need to complete assignment 1)

10.03.2022 - 52 Course TDT4102 – Lecture 2


There’s a way cin..

Just like cout, there’s a cin

Uses >> to read a value into a variable

The variable you read into can be of any of the common data types.

cout << "What is your favourite food? " << endl;


string food;
cin >> food;
cout << "That's awesome, " << food << " is also my favourite!" << endl;

10.03.2022 - 53 Course TDT4102 – Lecture 2


There’s a way cin..
● cin only reads one word or one number at a time!
For example: entering “1.5 2.3 3.8 4.4” here will print “1.5”:
double value;
cout << "Write some numbers: ";
cin >> value;
cout << "Value: " << value << endl;
If you use cin again in this example, “value” becomes 2.3

You can use the getline() function to read a line:
string message;
cout << "Write a message: ";
getline(cin, message);
cout << "The message was: " << message << endl;

10.03.2022 - 54 Course TDT4102 – Lecture 2


Today
Basics of the C++ Language

Variables

Data types

Type casting

Operators

Text input

If statements

Loops

Functions

(this will be all you need to complete assignment 1)

10.03.2022 - 55 Course TDT4102 – Lecture 2


If statements
bool condition = true;

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

(this will be all you need to complete assignment 1)

10.03.2022 - 58 Course TDT4102 – Lecture 2


Loops

There are three different loop variations in C++

The for loop:
for(int i = 0; i < 10; i++) {

}

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

10.03.2022 - 60 Course TDT4102 – Lecture 2


Loops

for(int i= int i = 0;
while(i < 10) {

}
i++;

In practice, the for loop is the


one used most (by far)
14.01.2025
01:43 p.m. - 61 Course TDT4102 – Lecture 2
Today
Basics of the C++ Language

Variables

Data types

Type casting

Operators

Text input

If statements

Loops

Functions

(this will be all you need to complete assignment 1)

10.03.2022 - 62 Course TDT4102 – Lecture 2


Functions

Functions are pieces of code that can be run on demand

Functions allow reusing functionality across your program

And if you fix a bug in a function used in multiple
places, you have fixed it everywhere!

Functions allow abstraction: as long as the function
behaves the same way, you can change how it works.

10.03.2022 - 63 Course TDT4102 – Lecture 2


Functions
Parameter list
Return data type Function name (can be empty)
void means
“does not return a value”
void someFunction(int parameter, bool another) {

10.03.2022 - 64 Course TDT4102 – Lecture 2


Functions
Values are returned using int add(int a, int b) {
a return statement. return a + b;
}

Functions are called by void printAnswer() {


writing the name, followed cout << add(21, 21) << endl;
by any parameter values }
separated using commas.

Returned values van be


used directly

10.03.2022 - 65 Course TDT4102 – Lecture 2


Functions

Declaration versus Definition:

Declaration: what does the function look like? void functionA();

Does not have code surrounded by { }


Definition: what does the function do? void functionA() {
}

Contains code surrounded by { }

10.03.2022 - 66 Course TDT4102 – Lecture 2


Functions #include "std_lib_facilities.h"

Functions must be declared void functionA() {


cout << "I am function A" << endl;
before they can be used. }

Order matters: the program void functionB();


is analysed top to bottom
int main() {
functionA();
functionB();
return 0;
}

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!

Remember to start as soon as possible!

There will likely be many people who will try to


run their code on INGInious close to the
deadline!

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

You might also like