0% found this document useful (0 votes)
18 views16 pages

Chapter 1

Uploaded by

bluephoenix
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)
18 views16 pages

Chapter 1

Uploaded by

bluephoenix
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/ 16

Programming Errors

IT1134
Chapter 02(II)
Programming Errors
• Programming errors can be categorized into three types: syntax errors, runtime
errors, and logic errors.
Syntax Errors
• Errors that are detected by the compiler are called syntax errors or compile errors. Syntax errors
result from errors in code construction, such as mistyping a keyword, omitting necessary punctuation, or
using an opening brace without a corresponding closing brace.
Common Error 1: Missing Braces
Common Error 2: Missing
Semicolons
Common Error 3: Missing
Quotation Marks
Common Error 4: Misspelling
Names
Programming Errors
• Programming errors can be categorized into three types: syntax errors, runtime errors,
and logic errors.
Runtime Errors
• Runtime errors cause a program to terminate abnormally. They occur while an application is running if the
environment detects an operation that is impossible to carry out.
• An input error occurs when the program is waiting for the user to enter a value, but the user enters a value
that the program cannot handle. Another common source of runtime errors is division by zero.
• Another common source of runtime errors is division by zero.
Programming Errors
• Programming errors can be categorized into three types: syntax errors, runtime errors,
and logic errors.
Logic Errors
• Logic errors occur when a program does not perform the way it was intended.

You will get Fahrenheit 67 degree, which is wrong. It should be 95. In C++, the division for integers is the quotient. The
fractional part is truncated. So 9 / 5 is 1. To get the correct result, you need to use 9.0 / 5, which results in 1.8
Numeric Type Conversions
• Floating-point numbers can be converted into integers using explicit casting

• C++ also allows you to convert a value from one type to another explicitly by using a casting operator.

static_cast<type>(value)
cout << static_cast<int>(1.7); int i = (int)5.4;
cout << static_cast<double>(1) / 2;
double d = 4.5;
int i = static_cast<int>(d); // i becomes 4, but d is
unchanged
Evaluating Expressions and Operator
Precedence
• C++ expressions are evaluated in the same way as arithmetic expressions
Evaluating Expressions and Operator
Precedence
• Operators contained within pairs of parentheses are evaluated first.
• Parentheses can be nested, in which case the expression in the inner
parentheses is evaluated first.
• When more than one operator is used in an expression, the following operator
precedence rule is used to determine the order of evaluation.
• Multiplication, division, and remainder operators are applied next. If an
expression contains several multiplication, division, and remainder operators,
they are applied from left to right.
• Addition and subtraction operators are applied last. If an expression contains
several addition and subtraction operators, they are applied from left to right.
Evaluating Expressions and Operator
Precedence
Character Data Type and Operations
• The character data type, char, is used to represent a single
character. A character literal is enclosed in single quotation
marks.
Casting between char and Numeric Types
• When a floating-point value is cast into a char, the floating-point value is first cast into an int, which is
then cast into a char.

char c = 65.5; // 65 is assigned to variable c

cout << c; // variable c is character A


• When a char is cast into a numeric type, the character’s ASCII is cast into the specified numeric type.
For example:

int i = 'A’; // The ASCII code of character A is


assigned to i

cout << i; // variable i is 65

Functions: int(‘A’), char(65)


Note that the static_cast<char>(value) operator explicitly casts a numeric value into a character.

You might also like