if/else គឺជា statement តំរូវអោយអនុវត្ត block នៃ statement នៅក្នុង statement if និងតំរូវអោយរំលងចោល block នៃ statement នៅក្នុង statement else ក្នុងករណី expression មួយផ្តល់លទ្ធផលជា True ឬសមមូលនឹង True ។ ហើយបើ expression នោះផ្តល់លទ្ធផលជា False ឬសមមូលនឹង False វិញ statement នៅក្នុង block ស្ថិតនៅក្នុង statement if នឹងត្រូវរំលងចោល ហើយ statement ស្ថិតនៅក្នុង block នៅក្នុង statement else នឹងត្រូវយកទៅអនុវត្ត។ ពិនិត្យកម្មវិធីខាងក្រោមនេះ៖
1 2 3 4 5 6 7 8 9 | sale = 900 buy = 900 if sale > buy: profit = sale - buy print (profit) else : print ( 'sale is not greater than buy.' ) print ( 'There is no profit' ) |
sale is not greater than buy.
There is no profit
There is no profit