Question

if(someCondition)
  int a=10;//Compilation Error
else if(SomeOtherCondition){
int b=10;//no compilation Error
}

Why this happening.Why there is compilation error in first case. If i put braces then no compilation error but for if statement braces are optional if it's one statement.

Was it helpful?

Solution

You need to define scope of the int a in if statement and it will be defined with curly braces {}.

if(someCondition){
  int a=10; // works fine
}else if(SomeOtherCondition){
  int b=10; //works fine
}

OTHER TIPS

if(someCondition)
  int a=10;//Compilation Error - you have to define the scope of int. what scope does it have here? so {} are necessary
else if(SomeOtherCondition){
int b=10;//no compilation Error
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top