CSC108H5 Lecture Notes - Lecture 10: Iterator, Random Variable, While Loop
CSC108H5 verified notes
10/13View all
Document Summary
Conditional statements are common among programming languages. They are used to perform actions or calculations on whether a condition is evaluated as true or false. If-then-else statements or conditional statements are essential features of programming languages and make programs more useful to users. If condition1: block1_statement elif condition2: block2_statement else: block3_statement. Example: def your_choice(answer): if answer > 5: pri(cid:374)t(cid:894)(cid:862)(cid:455)ou are o(cid:448)eraged(cid:863)(cid:895) elif answer <= 5 and answer > 1: pri(cid:374)t(cid:894)(cid:862)(cid:449)el(cid:272)o(cid:373)e to the toddler(cid:859)s (cid:272)lu(cid:271)! (cid:863)(cid:895) else: pri(cid:374)t(cid:894)(cid:862)(cid:455)ou are too (cid:455)ou(cid:374)g for toddler(cid:859)s (cid:272)lu(cid:271)(cid:863)(cid:895) Conditional (cid:272)o(cid:374)stru(cid:272)ts (cid:373)a(cid:455) ha(cid:448)e (cid:373)ultiple (cid:858)elif(cid:859) state(cid:373)e(cid:374)ts (cid:271)ut (cid:272)a(cid:374) o(cid:374)l(cid:455) ha(cid:448)e one (cid:858)else(cid:859) statement at the end. Using same code block, another elif statement may be inserted to provide for privileged (cid:373)e(cid:373)(cid:271)ers of toddler(cid:859)s (cid:272)lu(cid:271) : (cid:1006) (cid:455)ear-old kids. Example: def your_choice(answer): if answer > 5: pri(cid:374)t(cid:894)(cid:862)(cid:455)ou are o(cid:448)eraged(cid:863)(cid:895) elif answer <= 5 and answer > 1: pri(cid:374)t(cid:894)(cid:862)(cid:449)el(cid:272)o(cid:373)e to the toddler(cid:859)s (cid:272)lu(cid:271)! (cid:863)(cid:895) elif answer == 2: pri(cid:374)t(cid:894)(cid:862)(cid:449)el(cid:272)o(cid:373)e!