Java में if-else statement स्थिति की जांच करता है अगर शर्त सही है तो यह if block को executes करता है. अन्यथा else block को executes करता है. अगर सिंपल भाषा में कहा जाये तो if else Statement में जब condition 'true' होती है तब if का statement execute होता है और अगर condition 'false' होती है तो else वाला statement execute होता है।
If else statement भी IF statement की तरह ही होते है. If statement कोड के एक निश्चित section को implemented करता है. यदि परीक्षण expression का मूल्यांकन सही है if statement में वैकल्पिक else statement हो सकता है. और यदि else statement के अंदर कोड को executes किया जाता है तो परीक्षण expression गलत है।
if(condition) { Statement(s); } else { Statement(s); }
अगर स्थिति सही है, तो if के अंदर का बयान executes होता है, और यदि स्थिति गलत है तो else के अंदर दिए गए statement executes होता है।
public class IfElseExample { public static void main(String args[]){ int num=120; if( num < 40 ){ System.out.println("num is less than 40"); } else { System.out.println("num is greater than or equal 40"); } } }