Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's wrong with declaring a variable inside if's condition?

Perhaps I am getting rusty (have been writing in Python recently).

Why does this not compile?

if ( (int i=f()) == 0)

without the () around the int i=f() I get another, much more reasonable error of i is not being boolean. But that's why I wanted the parentheses in the first place!

My guess would be that using the parentheses makes it into an expression, and that declaration statements are not allowed in an expression. Is it so? And if yes, is it one of the C++'s syntax quirks?

BTW, I was actually trying to do this:

if ( (Mymap::iterator it = m.find(name)) != m.end())
    return it->second;
like image 239
davka Avatar asked Jan 22 '12 13:01

davka


People also ask

Can you declare a variable inside an if statement?

Java allows you to declare variables within the body of a while or if statement, but it's important to remember the following: A variable is available only from its declaration down to the end of the braces in which it is declared.

What happens if you declare a variable inside of a function?

variables declared inside a function can only be accessed inside that function. think of each function as a box and the lid can only be opened from inside: the function can grab what's outside or put something out, but you can't reach in its box.

Can you declare variable in IF statement C?

No. That is not allowed in C. According to the ANSI C Grammar, the grammar for the if statement is IF '(' expression ')' statement . expression cannot resolve to declaration , so there is no way to put a declaration in an if statement like in your example.

Can you define a variable inside an if statement JavaScript?

Can you define variables inside an IF statement? What I did there, was re-define a JavaScript variable inside the IF statement. But you can create a new variable right inside the IF statement parenthesis.


2 Answers

You can declare a variable in the if statement in C++ but it is restricted to be used with direct initialization and it needs to convert to a Boolean value:

if (int i = f()) { ... }

C++ doesn't have anything which could be described as "declaration expression", i.e. [sub-] expressions declaring a variable.

Actually, I just looked up the clause in the standard and both forms of initialization are supported according to 6.4 [stmt.select] paragraph 1:

...
condition:
   expression
   attribute-specifier-seqopt decl-specifier-seq declarator = initializer-clause
   attribute-specifier-seqopt decl-specifier-seq declarator braced-init-list
...

That is, it is also be possible to write:

if (int i{f()}) { ... }

Obviously, this only works in C++2011 because C++2003 doesn't have brace-initialization.

like image 99
Dietmar Kühl Avatar answered Oct 14 '22 01:10

Dietmar Kühl


There's a problem with scope.

Consider the following code:

if ((int a = foo1()) || (int b = foo2()))
{
    bar(b);
}

Is b declared inside the block? What if foo1() returns true?

like image 27
Ilya Kogan Avatar answered Oct 14 '22 02:10

Ilya Kogan