Conditional Statements.pptx
Document Details
Uploaded by Deleted User
Full Transcript
COMPUTER PROGRAMMING AST-134 CONDITIONAL STATEMENTS CONDITIONAL STATEMENTS A statement that makes the program smarter, it makes the program decides on what to do in certain condition. CONDITIONAL STATEMENTS IF statement (1 condition) IF-Else statement (2 condition) IF ELIF-ELSE (3 o...
COMPUTER PROGRAMMING AST-134 CONDITIONAL STATEMENTS CONDITIONAL STATEMENTS A statement that makes the program smarter, it makes the program decides on what to do in certain condition. CONDITIONAL STATEMENTS IF statement (1 condition) IF-Else statement (2 condition) IF ELIF-ELSE (3 or more conditions) Nested conditional statements (condition after a condition) INDENTATION It is used to indicate what statements are included inside the conditional statement. CONDITIONAL OPERATORS Are used to compare values inside a conditional statements. IF STATEMENT Used when dealing with one condition. Syntax If (condition): #Do something Example: If age >=18: IF-ELSE STATEMENT Used when dealing with two conditions Syntax if (condition): #Do Something Example: else: if age >= 18: #Do Something print(“Legal Age”) else: print(“Minor”) IF-ELIF-ELSE STATEMENT Used when dealing with THREE or MORE conditions. Syntax Example: if (condition): If age >= 18: #Do Something print(“Legal Age”) elif (condition): elif age >= 13: #Do Something print(“Teenager”) else: else: #Do Something print(“Too Young”) NESTED CONDITIONAL STATEMENT Used when dealing with conditions inside a condition. Syntax Example if (condition): if age >= 18: if (condition): if height >= 170: #Do Something print(“Legal Age and Tall”) else: elif height >= 156: #Do Something print(Legal Age and Average”) else: print(“Legal Age and Short”)) LOGICAL OPERATORS Used to include 2 or more conditions in one line Logical Operators AND – Both condition must be true OR – Either condition must be true SAMPLE PROBLEMS 1. Make a program that will determine whether the difference of two numbers is positive and negative. Display the difference and remarks. 2. Make a program that will determine and display the largest number among 3 numbers being inputted. Create a program that makes the user input 3 grades, so that the system could average it then check if the grade is: Exercise >100 or