6
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.

0

2 Answers 2

8

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
}
Sign up to request clarification or add additional context in comments.

Comments

1
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
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.