質問

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.

役に立ちましたか?

解決

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
}

他のヒント

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
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top