Hi all,

The topic is my question. I’ll explain it more. Just look at the following C++ code segment,

if(number_1 == number_2);
	{
		std::cout << number_1 << " == " << number_2 << std::endl;
	}

Note that, I have put a semicolon just after the right parentheses. It’s not a syntax error, because at the compile time it not gives an error. So, it should be a logic error. I found that semicolon causes the body of the “if” statement to be empty, from a book. So, there can’t be a output like “5 == 5”, if my input are 5 and 5 on the following code.

#include <iostream>	
//using namespace std;

int main()
{
	int number_1;	
	int number_2;	

	std::cout << "Enter two numbers:\t";
	std::cin >> number_1 >> number_2 ;

	if(number_1 == number_2);
	{
		std::cout << number_1 << " == " << number_2 << std::endl;
	}

	if(number_1 != number_2)
	{
		std::cout << number_1 << " != " << number_2 << std::endl;
	}
	return 0;
}

So where I’m going wrong, my code or my scrap on that book explanation.

Dani AI

Generated

is exactly right: the stray semicolon is a complete (empty) statement, so the if executes that statement and then the following block is just a normal compound statement that always runs. In C++, ; by itself is a valid statement often called a null or empty statement. See the if statement rules here for context: cppreference: if statement.

This pitfall shows up with loops too. For example, the block below is not part of the loop and executes once, after the loop finishes:

for (int i = 0; i < n; ++i) ;  // empty loop body
{
    use_once();                // runs once, not n times
}

If you truly want an empty body, make that intent obvious with a comment right on the semicolon, or prefer an explicit empty block:

while (condition) { /* intentionally empty */ }

Practical ways to prevent this bug:

  • Turn on warnings that catch empty controlled statements and misleading indentation.
    • GCC/Clang: -Wall -Wextra -Wpedantic -Werror -Wempty-body -Wmisleading-indentation (GCC warning options).
    • MSVC: enable /W4; it triggers warning C4390 for this pattern ().
  • Enforce braces even for single statements. Linters can help; for example, clang-tidy’s readability-braces-around-statements check will flag missing braces and make this class of mistake much harder to write (clang-tidy rule).

, that is why you still saw output like 4 == 5: the comparison guarded only the empty statement; the block printed unconditionally. Enabling the warnings above would make the compiler tell you about it immediately.

Recommended Answers

All 2 Replies

The semicolon tells the 'if statement' to do nothing when it is placed directly after it. In other words: if the statement is true do: ';' (nothing)
When the program passes that point it comes to:

{
     std::cout << number_1 << " == " << number_2 << std::endl;
 }

This part will now always be executed, because there is no longer a condition attached to it :)

So just remove the semicolon and you're back in business

That mean just print a line, I've test it using my inputs as 4 and 5. Still it give the line 4 == 5. That is 100% correct and I got the point.

Now I know why I'm confusing on this, because at the beginning I used the same inputs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.