Control Statements ================== Sequential Execution: ===================== a = int(input("Enter a number:")) # statement is an instruction b = int(input("Enter a number:")) c = a < b print("a = ",a) print("b = ",b) print("c = ",c) ==> statement wise execution is called "sequential execution". block of code: ============== -> group of statements called as "block" -> block of code can represent with an indentation. Ex: block1: line1 line2 line3 Why Control Statements? ======================== 1) when we want to make execute the particular block of code within the program(application) out of multiple blocks, then we required "control statements". 2) When we want to make execute the particular block of code repeatedly for several number of times. In this situation also we need control statements. Ex: X-App: feature1: line1 line2 line3 feature2: line1 line2 line3 feature3: line1 line2 line3 -> three types of control statements: 1) Conditional Statements 2) Loop Statements 3) Iterative Statements 1) Conditional Statements ========================= -> also called as "selection statements" -> when we need to execute the specific block of code from multiple blocks, then we can use "conditional statements". -> Several types: 1) simple if statement 2) if else statement 3) nested if else statement 4) if elif else statement simple if statement: ==================== Syntax: if condition_for_selection: statement-1 statement-2 statement-3 # WAP TO CONVERT A NUMBER TO POSITIVE IF IT IS ENTERED AS NEGATIVE. # number = -16 # compile time variable definition number = int(input("Enter an Integer value:")) # run-time variable definition if number < 0: number = -number # unary minus (-) print("The Number after convertion = ",number) print("Program is completed.") ================================================= 2) if else statement: ==================== Syntax: if condition_for_selection: if block statements else: else block statements # WAP TO CONVERT A NUMBER INTO POSITIVE IF IT IS ENTERED AS NEGATIVE OTHERWISE CONVERT INTO NEGATIVE. number = int(input("Enter some integer value:")) if number < 0: number = -number else: number = -number print("The Number after the conversion = ", number) ======================================================= 3) Nested If else: ================== a b and c are three numbers big? a > b: a > c: "a is bigger" otherwise: "c is bigger" b > c: "b is bigger" otherwise: "c is bigger" Syntax: if condition1: outer if block if condition2: inner if block else: inner else block else: if condition3: inner if block of else else: inner else block of else # WAP TO FIND THE BIGGEST NUMBER AMONG THREE INTEGERS. a = int(input("Enter a:")) b = int(input("Enter b:")) c = int(input("Enter c:")) if a > b: if a > c: print("a is biggest number among three.") else: print("c is biggest number among three.") else: if b > c: print("b is biggest number among three.") else: print("c is biggest number among three.") ================================================= if elif else ============ Syntax: if condition1: block1 elif condition2: block2 elif condition3: block3 elif condition4: block4 ...... else: default block # WAP TO ACCEPT A DAY NAME AND PRINT ITS CORRESPONDING DAY NUMBER ACCORDING TO THE GIVEN DAY NAME. dayName = input("Enter day name:") dayNumber = 0 if (dayName == "Sunday" or dayName == "sunday" or dayName == "SUNDAY"): dayNumber = 1 elif (dayName == "Monday" or dayName == "monday" or dayName == "MONDAY"): dayNumber = 2 elif (dayName == "Tuesday" or dayName == "tuesday" or dayName == "TUESDAY"): dayNumber = 3 elif (dayName == "Wednesday" or dayName == "wednesday" or dayName == "WEDNESDAY"): dayNumber = 4 elif (dayName == "Thursday" or dayName == "thursday" or dayName == "THURSDAY"): dayNumber = 5 elif (dayName == "Fridday" or dayName == "friday" or dayName == "FRIDDAY"): dayNumber = 6 elif (dayName == "Saturday" or dayName == "saturday" or dayName == "SATURDAY"): dayNumber = 7 else: print("Invalid Entry") print("The Day Number = ",dayNumber) ==================================================== Assignment: =========== 1) WAP TO ACCEPT A NUMBER AND CHECK WHETHER THE NUMBER IS EVEN NUMBER OR ODD NUMBER. 2) WAP TO ACCEPT 5 SUBJECT MARKS AND CALCULATE THE TOTAL SCORE OF THE STUDENT AND ALSO CALCULATE THE GRADE OF THE STUDENT BASED ON THE PERCENTAGE. 3) WAP TO CHECK WHETHER THE GIVEN YEAR IS LEAP YEAR OR NOT. 4) WAP TO CONVERT THE TEMPRETAURE FROM CELCIUS TO FARANHEIT OR VICEVERSA BASED ON THE TEMPERATURE TYPE. 5) DEVELOP A SYSTEM OF SIMPLE EXPENSE TRACKER. (MINI PROJECT)