15
Mar
A tip for writing conditional statements
Related Blog Items
- C++ Basics Tutorial - Lesson 3
- Writing a simple make file with example
- C++ Advanced Tutorial - Lesson 6
- L-Value and R-Value Expressions
- ANSI C Vs K&R C
Did you know that the following if statement is semantically right, but could logically be wrong?
if ( i = 2 )
{
/* do something */
}
Tip:
The compiler may only produce a waring: "Possibly incorrect assignment", but we may ignore it. To avoid such a mistake, just reverse the two identifiers.
if ( 2 == i )
{
/* do something */
}
If ‘==’ is replaced by an ‘=’, the compiler will give an error saying "Lvalue required".
Note: This post is just to provide a hint and may not suit to your coding style.
Popularity: 4%
You need to log on to convert this article into PDF
Related Blog Items - C++ Basics Tutorial - Lesson 3
- Writing a simple make file with example
- C++ Advanced Tutorial - Lesson 6
- L-Value and R-Value Expressions
- ANSI C Vs K&R C
Related Blog Items
- C++ Basics Tutorial - Lesson 3
- Writing a simple make file with example
- C++ Advanced Tutorial - Lesson 6
- L-Value and R-Value Expressions
- ANSI C Vs K&R C
No Comments
No comments yet.