Apex లో If-Else Statements అంటే ఏమిటి?
Salesforce Apex in Telugu | If, Else, Else If — మీ Program కి Decision Making Power ఇచ్చే Concept
మన Salesforce Apex Daily Series లో ఇప్పటివరకు:
- Apex అంటే ఏమిటి?
- Developer Console అంటే ఏమిటి?
- System.debug() ఎలా ఉపయోగించాలి?
- Variables అంటే ఏమిటి?
- Data Types అంటే ఏమిటి?
- Operators ఎలా ఉపయోగించాలి?
అనే Topics నేర్చుకున్నాం. ఈ రోజు మనం Apex Programming లో చాలా ముఖ్యమైన Concept అయిన If-Else Statements గురించి తెలుసుకుందాం.
ఈ Topic చాలా Important. ఎందుకంటే మనం Program కి "ఈ Condition నిజమైతే ఈ పని చేయి, లేకపోతే ఇంకో పని చేయి" అని చెప్పాలంటే If-Else ఉపయోగిస్తాం.
ముందుగా ఒక Real-Life Example చూద్దాం
మీరు ఒక సినిమా చూడటానికి వెళ్తున్నారు. Theatre లో Rule ఉంది: Age 18 లేదా అంతకంటే ఎక్కువ ఉంటే Movie చూడవచ్చు.
మీ Age 25 అయితే? → Movie చూడవచ్చు.
మీ Age 15 అయితే? → Movie చూడకూడదు.
ఇక్కడ మనం ఒక Condition Check చేస్తున్నాం — Age >= 18. Condition True అయితే ఒక పని. Condition False అయితే ఇంకో పని.
Programming లో ఇలాంటి Decision తీసుకోవడానికి If-Else ఉపయోగిస్తాం.
If Statement అంటే ఏమిటి?
చాలా సింపుల్గా: Condition True అయితే పని చేయి.
If Statement Syntax
Apexif (condition) {
// Condition True అయితే ఈ Code Execute అవుతుంది
}
ఇక్కడ...
if→ Condition Check చేస్తుంది.condition→ మనం Check చేయాలనుకుంటున్న Rule.{ }→ Condition True అయినప్పుడు Run కావాల్సిన Code.
Simple Example
ApexInteger age = 25;
if (age >= 18) {
System.debug('You are eligible');
}
ఇక్కడ Age = 25. మన Condition age >= 18. 25, 18 కంటే ఎక్కువ. కాబట్టి Condition True.
You are eligible
Condition False అయితే?
ఇప్పుడు Age ని 15 గా మార్చుదాం.
ApexInteger age = 15;
if (age >= 18) {
System.debug('You are eligible');
}
ఇక్కడ Condition False. అందుకే System.debug() లో ఉన్న Statement Execute కాదు.
అంటే If లో ఉన్న Code Condition True అయినప్పుడు మాత్రమే Run అవుతుంది.
ఇక్కడే Else అవసరం అవుతుంది
మనం ఇలా చెప్పాలనుకుంటున్నాం: Age 18 లేదా అంతకంటే ఎక్కువ అయితే Eligible అని చెప్పాలి. లేకపోతే Not Eligible అని చెప్పాలి. అప్పుడు else ఉపయోగిస్తాం.
If-Else Example
ApexInteger age = 15;
if (age >= 18) {
System.debug('You are eligible');
} else {
System.debug('You are not eligible');
}
ఇక్కడ Age 15. Condition 15 >= 18 False. కాబట్టి else Block Execute అవుతుంది.
You are not eligible
If-Else ని ఒకే Line లో అర్థం చేసుకోవాలంటే
IF = ఇది నిజమైతే ఇది చేయి.
ELSE = నిజం కాకపోతే అది చేయి.
అంతే.
Real Time Salesforce Example
ఒక Customer Account కి Annual Revenue ₹1 Crore కంటే ఎక్కువ ఉంటే అది High Value Customer అని గుర్తించాలి.
ApexDecimal revenue = 15000000;
if (revenue > 10000000) {
System.debug('High Value Customer');
} else {
System.debug('Normal Customer');
}
Revenue ₹1.5 Crore. Condition True.
High Value Customer
మరో Example – Student Marks
Student కి 40 Marks వస్తే Pass. 40 కంటే తక్కువ వస్తే Fail.
ApexInteger marks = 75;
if (marks >= 40) {
System.debug('Student Passed');
} else {
System.debug('Student Failed');
}
Student Passed
ఇంకోసారి Marks 30 అయితే?
ApexInteger marks = 30;
if (marks >= 40) {
System.debug('Student Passed');
} else {
System.debug('Student Failed');
}
Condition False.
Student Failed
Multiple Conditions ఉంటే?
ఇప్పుడు ఒక Student కి Grade ఇవ్వాలి అనుకుందాం. Rules:
- 90 లేదా అంతకంటే ఎక్కువ → A Grade
- 75 లేదా అంతకంటే ఎక్కువ → B Grade
- 60 లేదా అంతకంటే ఎక్కువ → C Grade
- 40 లేదా అంతకంటే ఎక్కువ → Pass
- 40 కంటే తక్కువ → Fail
ఇక్కడ రెండు Conditions మాత్రమే కాదు. Multiple Conditions ఉన్నాయి. అప్పుడు else if ఉపయోగిస్తాం.
Else If అంటే ఏమిటి?
if (condition1) {
} else if (condition2) {
} else if (condition3) {
} else {
}
Grade Example
ApexInteger marks = 85;
if (marks >= 90) {
System.debug('A Grade');
} else if (marks >= 75) {
System.debug('B Grade');
} else if (marks >= 60) {
System.debug('C Grade');
} else if (marks >= 40) {
System.debug('Pass');
} else {
System.debug('Fail');
}
మన Marks 85. మొదటి Condition 85 >= 90 False. తర్వాత 85 >= 75 True.
B Grade
ఒక ముఖ్యమైన Point
if మరియు else if లో చాలా Conditions True అయినా, మొదట True అయిన Condition యొక్క Block మాత్రమే Execute అవుతుంది. అందుకే Conditions ను సరైన Order లో రాయడం చాలా ముఖ్యం.
If-Else లో Operators కూడా ఉపయోగిస్తాం
మనము గత Blog లో Operators నేర్చుకున్నాం కదా? అవి ఇప్పుడు ఉపయోగపడుతున్నాయి.
ApexInteger age = 25;
Decimal salary = 50000;
if (age >= 18 && salary >= 30000) {
System.debug('Eligible');
} else {
System.debug('Not Eligible');
}
ఇక్కడ రెండు Conditions ఉన్నాయి.
- Age 18 లేదా అంతకంటే ఎక్కువ ఉండాలి.
- Salary ₹30,000 లేదా అంతకంటే ఎక్కువ ఉండాలి.
రెండూ True అయితే మాత్రమే Eligible.
Real Time Salesforce Example
ఒక Company లో Employee Promotion కోసం:
- Experience 3 Years లేదా ఎక్కువ ఉండాలి.
- Performance Rating 4 లేదా ఎక్కువ ఉండాలి.
Integer experience = 5;
Integer rating = 4;
if (experience >= 3 && rating >= 4) {
System.debug('Employee is eligible for promotion');
} else {
System.debug('Employee is not eligible for promotion');
}
రెండు Conditions True.
Employee is eligible for promotion
If-Else ఎందుకు చాలా Important?
Salesforce లో మనం చాలా Business Rules రాస్తాం. ఉదాహరణకు...
- Account Revenue ఎక్కువైతే Manager Approval కావాలి.
- Customer Active అయితే Email పంపాలి.
- Opportunity Stage Closed Won అయితే కొన్ని Actions చేయాలి.
- Student Marks ఎక్కువైతే Pass చేయాలి.
ఇలాంటి Business Logic కోసం Conditions చాలా అవసరం. అందుకే Apex లో If-Else చాలా ముఖ్యమైన Concept.
Beginners చేసే Common Mistake
చాలామంది = మరియు == మధ్య Difference మర్చిపోతారు.
=
Value Assign చేయడానికి.
ApexInteger age = 25;
==
రెండు Values Compare చేయడానికి.
Apexage == 25
మరో Common Mistake
Condition తర్వాత { } మర్చిపోవద్దు.
సరైన విధానం...
Correctif (age >= 18) {
System.debug('Eligible');
}
Code ను neat గా రాయడం అలవాటు చేసుకోండి.
Practice Task
ఇప్పుడు మీరు స్వయంగా Practice చేయండి.
Practice 1 – Age
Integer age = 20;
if (age >= 18) {
System.debug('Major');
} else {
System.debug('Minor');
}
Practice 2 – Marks (35 వస్తే ఏమి వస్తుంది?)
Integer marks = 35;
if (marks >= 40) {
System.debug('Pass');
} else {
System.debug('Fail');
}
Practice 3 – Salary
Integer salary = 60000;
if (salary >= 50000) {
System.debug('Eligible for Bonus');
} else {
System.debug('Not Eligible for Bonus');
}
Practice 4 – Multiple Conditions
Integer age = 25;
Integer salary = 45000;
if (age >= 18 && salary >= 30000) {
System.debug('Eligible');
} else {
System.debug('Not Eligible');
}
ఇవి అన్ని Developer Console లో Run చేసి Debug Logs లో Output చూడండి.
Interview Questions
If Statement అంటే ఏమిటి?
Answerఒక Condition True అయినప్పుడు మాత్రమే Code Execute చేయడానికి If Statement ఉపయోగిస్తారు.
Else ఎందుకు ఉపయోగిస్తారు?
AnswerIf Condition False అయినప్పుడు Execute చేయాల్సిన Code ను Else Block లో రాస్తాము.
Multiple Conditions కోసం ఏమి ఉపయోగిస్తారు?
Answerelse if ఉపయోగిస్తాము.
If-Else లో Operators ఉపయోగించవచ్చా?
Answerఅవును. Comparison మరియు Logical Operators ను Conditions లో ఉపయోగించవచ్చు.
If Condition False అయితే ఏ Block Execute అవుతుంది?
AnswerElse Block ఉంటే Else Block Execute అవుతుంది.
Conclusion
ఈ రోజు మనం Apex లో If, Else మరియు Else If Statements గురించి తెలుసుకున్నాం.
ఇవి మన Program కి Decision Making Power ఇస్తాయి. అంటే "ఈ పరిస్థితి ఉంటే ఈ పని చేయి, లేకపోతే మరో పని చేయి" అని Salesforce కి చెప్పడానికి If-Else ఉపయోగిస్తాం.
ఈ రోజు Examples అన్నీ మీరు స్వయంగా Type చేసి Practice చేయండి. Code చదవడం కంటే స్వయంగా Code రాయడం చాలా ముఖ్యం.
🎯 ఇంకా మరింత లోతుగా Apex నేర్చుకోవాలనుకుంటున్నారా?
ఈ Blog ద్వారా మీరు Apex Concepts ను సులభంగా అర్థం చేసుకోవచ్చు. కానీ చదవడం మాత్రమే కాకుండా, Practice చేస్తూ నేర్చుకుంటేనే నిజమైన Coding Skills వస్తాయి.
అందుకే నేను SFDC Telugu లో పూర్తి Salesforce Apex Course (తెలుగులో) రూపొందించాను. ఈ Course లో ప్రతి Concept ను Step by Step గా, Real Time Examples తో, Coding రాని వారికీ కూడా అర్థమయ్యే విధంగా వివరించాను.
ఈ Course ను Regular గా Follow చేస్తూ Practice చేస్తే, మీరు Apex Code ను చదివి అర్థం చేసుకోవడమే కాకుండా, స్వంతంగా Code రాయడం కూడా నేర్చుకుంటారు.
ఈ Course ద్వారా మీరు...
- Apex Code ఎలా రాయాలో నేర్చుకుంటారు.
- Code ను చూసి ఏమి జరుగుతుందో అర్థం చేసుకోగలుగుతారు.
- ప్రతి Concept ను Practice చేస్తారు.
- Real Time Scenarios పై Coding చేయడం నేర్చుకుంటారు.
- Interview లో వచ్చే Coding Questions కి ధైర్యంగా Answer చెప్పగలుగుతారు.
- చివరికి మీ స్వంతంగా Apex Code రాయగల స్థాయికి చేరుకుంటారు.
Course కి Instant Access వస్తుంది. వెంటనే Learning ప్రారంభించవచ్చు.
మీరు Regular గా Practice చేస్తే, Coding రాని మీరు కూడా చాలా సులభంగా Apex Code నేర్చుకోగలరు. 🚀
ఈ Topic మీకు Easy గా అర్థమైందా?
ఈ Blog ద్వారా విషయం మీకు సులభంగా అర్థమైతే, Comment చేసి మీ అభిప్రాయాన్ని తెలియజేయండి.
ఇలాంటి Salesforce Tutorials, Updates మరియు Useful Content కోసం SFDC Telugu Channel ని Subscribe చేయండి.
అలాగే కొత్త Blog Post మరియు Salesforce Updates కోసం WhatsApp Channel లో Join అవ్వండి.
Comment లో మీరు ఏ Place నుండి ఈ Blog చదువుతున్నారో చెప్పండి.
Thank You! ❤️
0 Comments