It includes various types of programming errors: Syntax, Semantic, Logical, Runtime Error with the help of C++ programs. Also, discussed how to fix these errors.
For better understanding, subscribe following YouTube channel:
https://youtu.be/PIOCmaYdSCg
2. Programming Error or bug
Unexpected output produced by program is called Error or bug
ERROR BUG
Found by
Developer
Found by
Tester
Debugging
Process of finding and removing errors in the program is called debugging
4. SYNTAX ERROR
-- It occurs due to the violation of syntax or grammatical rules
-- These errors are detected at compile time.
Missing semicolon and
datatype
Error in expression
//C++ program to illustrate syntax error
#include<iostream.h>
void main()
{
x = 10; // Error: Undeclared identifier x
int y = 15;
cout <<“value of x and y” <<x<<y
//Error : ';' expected
}
5. SYNTAX ERROR
-- It occurs due to the violation of one or more grammar rules.
-- These errors are detected at compile time.
C++ program to illustrate syntax error
#include<iostream.h>
void main()
{
int x = ( 3 + 5; // missing closing parenthesis ‘)’
int y = 3 + * 5; // missing argument between '+'
and ‘*’
}
Error in expression
Missing semicolon and
datatype
6. SEMANTIC ERROR
-- It occurs when the statements written in the program are not
meaningful to the compiler.
-- Difficult to detect because their syntax is correct but their
meaning is wrong.
//C++ program to illustrate semantic error
#include<iostream.h>
void main()
{
int a, b, c;
a + b = c;
//Error: lvalue required as left operand of
assignment
}
7. SEMANTIC ERROR
-- It occurs when the statements written in the program are not
meaningful to the compiler.
-- Difficult to detect because their syntax is correct but their
meaning is wrong.
//C++ program to illustrate semantic error
#include<iostream.h>
int main ()
{
int i;
i++; //Error: The variable ‘i’ is not
initialized
}
8. SEMANTIC ERROR
-- It occurs when the statements written in the program are not
meaningful to the compiler.
-- Difficult to detect because their syntax is correct but their
meaning is wrong.
C++ program to illustrate semantic error
#include<iostream.h>
void main()
{
int i= “Hello”
//Error: cannot convert 'char *' to 'int'
}
9. LOGICAL ERROR
-- It occurs because of wrong programming designing
-- Program with logical error are executed. But, does not generate the
requested result.
//C++ program to illustrate logical error
#include<iostream.h>
int main ()
{
for( ; ; )
{
cout<< "This loop will run forever” ;
}
return 0;
}
Infinite Loop
Errors in performed
computation
10. LOGICAL ERROR
-- It occurs because of wrong programming designing
-- Program with logical error are executed. But, does not generate the
requested result.
//C++ program to illustrate logical error
#include<iostream.h>
int main () // program: sum of two numbers
{
int a, b;
c= a-b;
cout<< “sum of two number is”<< c;
// This will always return wrong value wrt
specification required was sum of two numbers
}
Errors in performed
computation
Infinite Loop
11. RUNTIME ERROR
-- It occurs during program execution after successful compilation
--It occurs due to performing illegal operations.
-- It leads to abnormal termination of the program.
//C++ program to illustrate runtime error
#include<iostream.h>
void main()
{
int n = 9, div = 0;
div = n/0; // Error: number is divided by 0,
cout<<“The result is”<<div;
//This program abnormally terminates
}
12. RUNTIME ERROR
-- It occurs during program execution after successful compilation
--It occurs due to performing illegal operations.
-- It leads to abnormal termination of the program.
//C++ program to illustrate runtime error
#include<iostream.h>
void main()
{
char *ptr;
*ptr= 3; // Error: point illegal memory space
}
Hello Friends. in previous video we learned the concept of oops plus coding in c++, now we will discuss what all errors we face while running a program.
Unexpected output produced by program is called Error or bug.
Error is found by developer during coding where as bugs are found by the tester when the project is done.
Some of the errors do not let program to be compiled and executed. For that debugging is required: debugging is a process of finding and removing errors in the program
The most common errors can be broadly classified as follows.
Let us now understand these errors with example using c++ and also how to fix them.
First is syntax error: It occurs due to the violation of syntax or grammatical rules which is detected at compile time. Most frequent syntax errors are:
--Missing datatype: datatype of x is missing which will generate an error: Undeclared identifier x
--Missing semicolon: as you can see cout statement misses semicolon and in c++ it is mandatory to end the statement with ; so it will generate error.
--second is: error in the expression: here in second statement: closing parenthesis for the expression is missing and in fourth statement: arguments between the operators.
Now : Semantic error: It occurs when the statements written in the program are not meaningful to the compiler. It is difficult to detect because their syntax is correct but their meaning is wrong. These are detected either at compile time or runtime. Lets take example:
-- Wrong statement: If some expression is given at the left side of assignment operator, this may generate semantic error.
Next is Use of non-initialized variable: variable i has not been assigned any value before incrementing which will cause an error.
Another is type incompatibility: Assigning a string value to integer type variable will produce an error as compiler can not convert string into integer.
Fortunately, compiler generally catches both syntax and semantic errors and generate warnings or errors, so you easily identify and fix the problem
Third type of error is Logical error. It occurs because of wrong programming design. Though program is executed as there is no syntax or semantic error but do not generate the desired results.
--First is Infinite loop: When the conditional expression is absent, it is assumed to be true, leading to endless loop which is undesirable result. You can terminate an infinite loop by pressing Ctrl + C keys.
Next is: errors in performed computation. Like here we want the addition as the output of two numbers but due to coding wrong expression i.e. subtraction, we got the wrong result. Manually the code should be checked to locate logical errors.
Last is the Runtime error: It occurs during program execution after successful compilation due to illegal operations which leads to abnormal termination. The general cause of Run time errors is because your program is trying to perform an operation that is impossible to carry out.
--Like: Division error: i.e. dividing a no. by 0 which will terminate the program abnormally.
Next is pointing to null pointer: If you try to access or declare a null pointe: it will generate an error.
Exception handling is used to avoid such runtime errors.
Finally concluding, first we discussed what is programming error and bugs. Then we discussed types of error with examples with way of finding and removing error. That’s all friends, if you like the video please click the like button and for more such technical videos please subscribe the channel.