Conditional Statements అంటే ఏమిటి?
Conditional Statements అంటే ఏమిటి?
Program నడుస్తున్నప్పుడు ఒక condition ఆధారంగా ఏ పని చేయాలి, ఏ పని చేయకూడదు అని నిర్ణయించడానికి ఉపయోగించే statementsను Conditional Statements అంటారు.
మన programలో ప్రతి instruction ఎప్పుడూ ఒకే విధంగా execute కావాల్సిన అవసరం ఉండదు. ఒక condition True అయితే ఒక పని చేయాలి. Condition False అయితే ఆ పని చేయకూడదు లేదా వేరే పని చేయాలి. ఈ decision-making కోసం conditional statements ఉపయోగిస్తాము.
ఎందుకు ఉపయోగిస్తాము?
- ఒక number positiveనా లేదా negativeనా తెలుసుకోవడానికి.
- Student pass అయ్యాడా లేదా తెలుసుకోవడానికి.
- Age ఆధారంగా eligibility నిర్ణయించడానికి.
- Password సరైనదా కాదా check చేయడానికి.
- Weather లేదా marks ఆధారంగా వేర్వేరు actions చేయడానికి.
Conditional Statement ఎలా పనిచేస్తుంది?
- మొదట ఒక condition తయారు చేస్తాము.
- Python ఆ conditionను evaluate చేస్తుంది.
- Condition సరైనదైతే True వస్తుంది.
- Condition సరైనది కాకపోతే False వస్తుంది.
- ఈ True లేదా False ఆధారంగా program flow నిర్ణయించబడుతుంది.
Pythonలో conditional statements రాయడానికి సాధారణంగా if, if-else, if-elif-else వంటి forms ఉంటాయి. ఈ lessonలో వాటి basic idea మాత్రమే తెలుసుకుందాం. if Statement syntaxను తరువాతి lessonలో వివరంగా నేర్చుకుంటాం.
Condition అంటే ఏమిటి?
ఒక ప్రశ్నకు True లేదా False అనే సమాధానం ఇచ్చే expressionను condition అంటారు.
ఉదాహరణకు marks >= 35 అనే condition, marks 35 లేదా అంతకంటే ఎక్కువగా ఉంటే True ఇస్తుంది. లేకపోతే False ఇస్తుంది.
Real-lifeలో ఉపయోగం
మన రోజువారీ జీవితంలో కూడా decisions conditions ఆధారంగానే ఉంటాయి. ఉదాహరణకు, వర్షం పడితే umbrella తీసుకెళ్తాము. వర్షం పడకపోతే umbrella తీసుకెళ్లము. ఇదే ఆలోచనను Python programలో conditions ద్వారా రాస్తాము.
Pythonలో ఉపయోగించే విధానం
ముందుగా comparison operator ఉపయోగించి ఒక condition రాయాలి. ఆ condition ఫలితాన్ని ఒక variableలో store చేయవచ్చు లేదా నేరుగా print చేయవచ్చు. తరువాత conditional statement ఆ True లేదా False ఫలితాన్ని ఉపయోగించి నిర్ణయం తీసుకుంటుంది.
Simple Explanation
సులభంగా చెప్పాలంటే, Conditional Statement అనేది programలో decision తీసుకునే విధానం.
ఉదాహరణకు, marks >= 35 అనేది ఒక condition. Marks 35 లేదా అంతకంటే ఎక్కువగా ఉంటే result True. Marks 35 కంటే తక్కువగా ఉంటే result False.
ఈ True లేదా False ఆధారంగా program ఏ codeను execute చేయాలో నిర్ణయించవచ్చు. అందువల్ల conditional statementsను decision-making statements అని కూడా అంటారు.
Real-Life Example
Traffic Signal ఉదాహరణ
Traffic signal దగ్గర ఒక rule ఉంటుంది. Signal green అయితే vehicles ముందుకు వెళ్తాయి. Signal red అయితే vehicles ఆగాలి.
ఇక్కడ signal color ఒక condition లాంటిది. Condition ఆధారంగా action మారుతుంది. Pythonలో కూడా ఒక condition True లేదా False అవుతుంది. ఆ ఫలితాన్ని బట్టి program వేర్వేరు పనులు చేస్తుంది.
Code Examples
Comparison Condition యొక్క ఫలితం
marks = 72
passed = marks >= 35
print(passed)
ఈ programలో marks 35 కంటే ఎక్కువా లేదా సమానమా అని check చేస్తున్నాము. Condition True కావడంతో passed variableలో True store అవుతుంది.
True
Temperature ఆధారంగా Condition తయారు చేయడం
temperature = 30
is_hot = temperature > 25
print(is_hot)
ఈ programలో temperature 25 కంటే ఎక్కువగా ఉందా అని check చేస్తున్నాము. 30, 25 కంటే ఎక్కువ కాబట్టి condition True అవుతుంది.
True
Line-by-Line Explanation
ఈ lineలో 72 అనే valueను marks అనే variableలో store చేస్తున్నాము. Python ఈ valueను తరువాతి conditionలో ఉపయోగిస్తుంది.
ఈ lineలో marks value 35 కంటే ఎక్కువా లేదా సమానమా అని check చేస్తున్నాము. 72, 35 కంటే ఎక్కువ కాబట్టి condition True అవుతుంది. ఆ True value passed variableలో store అవుతుంది.
ఈ line passed variableలో ఉన్న valueను screenపై చూపిస్తుంది. అందువల్ల outputగా True కనిపిస్తుంది.
ఈ lineలో temperature అనే variableకు 30 value ఇస్తున్నాము. ఈ valueను తరువాతి comparisonలో ఉపయోగిస్తాము.
ఈ line temperature 25 కంటే ఎక్కువగా ఉందా అని check చేస్తుంది. 30, 25 కంటే ఎక్కువ కాబట్టి result True అవుతుంది. ఆ result is_hot variableలో store అవుతుంది.
ఈ line is_hot variableలో ఉన్న True valueను outputగా చూపిస్తుంది.
Try It
Try this Python code:
number = 8
is_positive = number > 0
print(is_positive)
Practice
Practice: ఒక programలో number = 12 అని తీసుకుని, ఆ number 10 కంటే ఎక్కువా అని condition ద్వారా check చేయండి. Condition ఫలితాన్ని print చేయండి.
Hint: Comparison కోసం > operator ఉపయోగించండి. Conditionను ఒక variableలో store చేసి print చేయవచ్చు.
Practice: marks = 28 అనే value తీసుకుని, marks 35 కంటే ఎక్కువా లేదా సమానమా అని check చేయండి.
Hint: Pass condition కోసం >= operator ఉపయోగించండి.
Common Mistakes
ఒక valueతో మరొక valueను compare చేయడానికి ==, >=, , <, != వంటి comparison operators ఉపయోగించాలి. = అనేది assignment కోసం ఉపయోగిస్తారు.
Comparison expressionను Python evaluate చేసినప్పుడు దాని result Boolean value అయిన True లేదా False అవుతుంది.
True మరియు False Python Boolean values. వాటిని quotes లేకుండా రాయాలి. Quotes ఉపయోగిస్తే అవి string అవుతాయి.
Comparison operator conditionను తయారు చేస్తుంది. Conditional statement ఆ condition ఆధారంగా decision తీసుకుంటుంది.
Interview Questions
Conditional Statement అంటే ఏమిటి?
Answer: ఒక condition ఆధారంగా program ఏ పని చేయాలో నిర్ణయించే statementను Conditional Statement అంటారు.
Conditional Statements ఎందుకు ఉపయోగిస్తాము?
Answer: Programలో decision-making చేయడానికి, అంటే condition True లేదా False ఆధారంగా వేర్వేరు codeను execute చేయడానికి ఉపయోగిస్తాము.
Condition యొక్క result ఏ రూపంలో ఉంటుంది?
Answer: Condition యొక్క result సాధారణంగా Boolean valueగా ఉంటుంది. అది True లేదా False అవుతుంది.
Pythonలో సాధారణంగా ఉపయోగించే conditional statement forms ఏవి?
Answer: if, if-else, if-elif-else forms సాధారణంగా ఉపయోగిస్తారు.
Exam Questions
Conditional Statementsను నిర్వచించండి.
Answer:
ఒక condition ఆధారంగా program executionను నియంత్రించి decision తీసుకునే statementsను Conditional Statements అంటారు.
marks = 50; marks >= 35 అనే condition result ఏమిటి?
Answer:
True. ఎందుకంటే 50, 35 కంటే ఎక్కువగా ఉంది.
Conditional Statementలో condition False అయితే ఏమి జరుగుతుంది?
Answer:
ఆ conditionకు సంబంధించిన action execute కాకపోవచ్చు లేదా వేరే action execute కావచ్చు. ఇది ఉపయోగించిన conditional formపై ఆధారపడి ఉంటుంది.
Comparison operator మరియు Conditional Statement మధ్య తేడా ఏమిటి?
Answer:
Comparison operator రెండు valuesను compare చేసి True లేదా False result ఇస్తుంది. Conditional Statement ఆ result ఆధారంగా programలో decision తీసుకుంటుంది.
Quick Revision
- Conditional Statements programలో decisions తీసుకోవడానికి ఉపయోగిస్తారు.
- Condition result True లేదా Falseగా ఉంటుంది.
- Comparison operators ఉపయోగించి conditions తయారు చేయవచ్చు.
- Conditional Statements program flowను నియంత్రిస్తాయి.
- Pythonలో if, if-else, if-elif-else forms ఉన్నాయి.
- ఈ lessonలో conditional statements యొక్క basic idea నేర్చుకున్నాం.
