It’s Too Easy To Make This Simple, But Worse, Logical Error In Programming

Want To Be Part Of my 7-Week's Lessons To Help You Propel Your Objectives & Eradicate Frustrations In Your Life?
Subscribe FREELY Right Now - click here!

C-programming-language random number

This article is relevant to almost any languages, namely C, C++, Java, C#, PHP..etc
I will be using C++ for example illustrations

The Two Operators Which Create Confusion To Beginners

Before we begin with the title, it is important that you understand the following Operators:
1) The assigment operator
Example:

int x = 10; //This statement assigns the value 10 to variable

Meaning, some memory location will be reserved or set aside, it will be named or referenced as the variable “x”. Then, the value “10” will be stored in that location.

2) The equality operator

Example:
x == 10; //This is an evaluation statement

Meaning, evaluation will be done to verify if really the variable “x” contains the value “10”. If the location named “x” has a value of “10”, then the statement evaluates to TRUE (1), else it is FALSE (0)

I hope this is clear now? If yes, let’s proceed further..

The Common Programming Error

Consider this piece of C++ Code:

void main()
{ int x=0; //declare a variable x and assign it a value 0 to prevent garbage values

//ask the user to input a value
cout << “Enter an integer: “;
//collect the input
cin >> x;

//an “if” statement to do something*
if (x = 5) //LOGICAL ERROR!
{ cout << “I knew it was 5!” << endl;
}
else
{ cout << “How can I possibly guess what you entered?” << endl;
}
}//end main

OUTPUT:
Enter an integer: 36
I knew it was 5!

As you can see from the output above, the result is wrong. We entered 36, and still the program is reacting as ‘if it was’ actually 5 that was entered!

What Is Wrong Then?

The “if” statement was there to do something and that something was an “evaluation”.
if-else” statements always perform one and only one thing: EVALUATION.

Now, I think you are getting the picture; whenever there is an “if-else” statement, there is an “evaluation” and whenever there is an “evaluation”, we use the “equality operator”! (Say this aloud, stick it somewhere on your walls till it becomes natural to your brain) 🙂

Further Explanation On the Error

The statement “if (x = 5)”, will always return a TRUE value. This is because an if statement is FALSE, if and only if, that statement evaluates to 0. In our case, the statement says, x=5. Hence, x is not 0. Therefore, if() evaluates to TRUE.
That is why the code body of the “if” statement will still get executed although this is not what we ‘intended‘ the program to do.

NOTE:

This type of error is called a “logical error” and is one of the worse error in programming since there is no way for the compiler to catch that error for you!
Compile-time errors are caught!
Run-time errors are also caught!
But, logical errors can never be caught!
So be careful! 😉
(I think now after reading the above, you will never ever make such an error) 😛

The Full Correct Code:

void main()
{ int x=0; //declare a variable x and assign it a value 0 to prevent garbage values

//ask the user to input a value
cout << “Enter an integer: “;
//collect the input
cin >> x;

//an “if” statement to do something*
if (x == 5) //GOOD!
{ cout << “I knew it was 5!” << endl;
}
else
{ cout << “How can I possibly guess what you entered?” << endl;
}
}//end main

OUTPUT:
Enter an integer: 36
How can I possibly guess what you entered?

If you liked this article, do subscribe to not miss other exciting tutorials 😉
And I would also love to hear your feedback, point of view or any suggestion.

Recommended Reading:

=> Random Numbers in C programming

Speak Your Mind

*