2013-04-08 11 views
6
if(someCondition) 
    int a=10;//Compilation Error 
else if(SomeOtherCondition){ 
int b=10;//no compilation Error 
} 

ऐसा क्यों हो रहा है। पहले मामले में संकलन त्रुटि क्यों है, में परिवर्तनीय घोषणा। यदि मैं ब्रेसिज़ डालता हूं तो कोई संकलन त्रुटि नहीं होती है, लेकिन यदि स्टेटमेंट ब्रेसिज़ वैकल्पिक हैं तो यह एक कथन है।यदि धारा

उत्तर

8

आपको int a के if statement में दायरे को परिभाषित करने की आवश्यकता है और इसे घुंघराले ब्रेसिज़ {} के साथ परिभाषित किया जाएगा।

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

धन्यवाद इस सही कारण नहीं है, मुझे लगता है – Krushna

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 
}