if/elif/else គឺជា statement តំរូវអោយអនុវត្ត block នៃ statement នៅក្នុង statement if ឬ statement elif ក្នុងករណី expression នៅជាប់នឹង statement if ឬ elif ផ្តល់លទ្ធផលជា True ឬសមមូលនឹង True ។ តែបើគ្មាន expression ណាមួយផ្តល់លទ្ធផលជា True ឬសមមូលនឹង True ទេ block នៃ statement នៅក្នុង statement else នឹងត្រូវយកទៅអនុវត្ត។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | a = 15 b = 15 if a < b: print ( 'a < b' ) print ( 'This block of statements in if were executed' ) elif a = = b: print ( 'a == b' ) print ( 'This block of statements in first elif were executed' ) elif a < b < 100 : print ( 'a < b < 100' ) print ( 'This block of statements is second elif were executed' ) else : print ( 'No expression resulting True' ) print ( 'The block of statements in else were executed' ) |
a == b
This block of statements in first elif were executed
This block of statements in first elif were executed
នៅក្នុងភាសា Python ដែលហៅថា ternary operator គឺជា expression ម៉្យាងដែលនៅក្នុងនោះ មានការប្រើប្រាស់ statement if/else ដើម្បីផ្តល់លទ្ធផលជាវត្ថុណាមួយ។
1 2 3 4 5 | sale = 1000 buy = 900 result = 'ចំណេញ' if (sale - buy > 0 ) else 'ខាត' print (result) |
ចំណេញ