Professional Documents
Culture Documents
L11 - Implicit Type Conversions-1
L11 - Implicit Type Conversions-1
L11 - Implicit Type Conversions-1
Type Conversions
14 March 2024
Layout Rules
Good and consistent code layout
enhances code readability.
If code is readable, it is inherently more
understandable and consequently
reliable and maintainable.
Syntax Errors
Syntax errors occur when the code does
not conform to the syntax rules of the
programming language.
In C++, syntax errors prevent the
compiler from understanding your code
and result in compilation failures.
Syntax errors are usually straightforward
to identify because they violate the
language’s grammar rules.
COS132 Types, Variables and Input 9 / 24
Recap
Recap of Lectures 04 - 06
Type Conversions
Code Example
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3
4 i n t main ( )
5 {
6 i n t cout = 5;
7 i n t team = 4 ;
8 c o u t << \n ”The sum i s ”<< c o u t + team ;
9 return 0;
10 }
Semantic Errors
Semantic errors occur when the code
executes without crashing, but it does
not produce the expected result due to
incorrect logic or flawed understanding
of the problem.
Unlike Syntax errors, Semantic errors do
not violate the syntax rules, so they do
not result in compilation failures.
COS132 Types, Variables and Input 12 / 24
Recap
Recap of Lectures 04 - 06
Type Conversions
Code Example
1 #i n c l u d e <i o s t r e a m >
2 u s i n g namespace s t d ;
3
4 i n t main ( ) {
5 i n t num1 = 5 ;
6 c h a r num2 = ’ 7 ’ ;
7
8 int r e s u l t = num1 + num2 ;
9
10 c o u t << ”The sum i s : ” << r e s u l t ;
11 return 0;
12 }
Widening Conversions
A widening conversion occurs when
converting a data type with a smaller
range to a data type with a larger range,
typically without loss of data.
Things to note
Narrowing conversions may lead to
unintended behaviour or loss of
precision.
Using explicit type conversions through
casting operators can help make the
conversion process more transparent and
explicit in the code, reducing the
likelihood of errors.
COS132 Types, Variables and Input 24 / 24