Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable declarations following if statements

An issue came up on another forum and I knew how to fix it, but it revealed a feature of the compiler peculiar to me. The person was getting the error "Embedded statement cannot be a declaration or labeled statement" because they had a declaration of a variable following an if statement with no brackets. That was not their intent, but they had commented out the line of code immediately following the if statement, which made the variable declaration the de facto line of code to execute. Anyway, that's the background, which brings me to this.

The following code is illegal

if (true)     int i = 7; 

However, if you wrap that in brackets, it's all legal.

if (true) {     int i = 7; } 

Neither piece of code is useful. Yet the second one is OK. What specifically is the explanation for this behavior?

like image 922
Anthony Pegram Avatar asked Mar 22 '10 23:03

Anthony Pegram


People also ask

Can you declare variables in 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.

How do you use variables in IF statements?

If you're new to the syntax that's used in the code sample, if (int i = 5) { is a perfectly valid way of declaring and defining a variable, then using it inside the given if statement. It allows us to write terser, clearer code, while also avoiding limiting the scope of a variable.

Can you declare variables in an 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 declare a variable in an if statement Python?

python allows creation of variables in an if... elif... else... clause, so that they can be used also outside the conditional block. This is also true for nested conditionals.


1 Answers

The C# language specification distinguishes between three types of statements (see chapter 8 for more details). In general you can have these statements:

  • labeled-statement - my guess that this is for the old-fashioned goto statement
  • declaration-statement - which would be a variable declaration
  • embedded-statement - which includes pretty much all the remaining statements

In the if statement the body has to be embedded-statement, which explains why the first version of the code doesn't work. Here is the syntax of if from the specification (section 8.7.1):

if ( boolean-expression ) embedded-statement
if ( boolean-expression ) embedded-statement else embedded-statement

A variable declaration is declaration-statement, so it cannot appear in the body. If you enclose the declaration in brackets, you'll get a statement block, which is an embedded-statement (and so it can appear in that position).

like image 109
Tomas Petricek Avatar answered Oct 16 '22 19:10

Tomas Petricek