Lec07 CSE105 Structured Programming Syed Maruful Huq Conditions

  • Slides: 17
Download presentation
Lec-07 CSE-105 Structured Programming Syed Maruful Huq

Lec-07 CSE-105 Structured Programming Syed Maruful Huq

Conditions statements A conditional statement is a set of actions performed if a certain

Conditions statements A conditional statement is a set of actions performed if a certain condition is met. It is sometimes referred to as an IF-THEN statement, because IF a condition is met, THEN an action is performed. Example 1: If age is greater than 18 Then can get a driver’s license.

Conditions statements Example 2: If job experience is more then 5 years Then salary

Conditions statements Example 2: If job experience is more then 5 years Then salary offered is 20000 taka. Else Salary offered is 15000 taka.

Conditions statements Example 3: If marks obtained is greater than or equal to 80

Conditions statements Example 3: If marks obtained is greater than or equal to 80 Then grade is A+ Else if mark obtained is greater than or equal to 75 and less than 80 Then grade is A Else Grade is A-

Python Comparison Operators Comparison operators are used to compare two values. These operators compare

Python Comparison Operators Comparison operators are used to compare two values. These operators compare the values on either sides of them and decide the relation among them. They are also called Relational operators. Operator Description Example == If the values of two operands are equal, then the condition becomes true. (a == b) is not true. != If values of two operands are not equal, then condition becomes true. (a != b) is true. <> If values of two operands are not equal, then condition becomes true. (a <> b) is true. This is similar to != operator. > If the value of left operand is greater than the value of right operand, then condition becomes true. (a > b) is not true. < If the value of left operand is less than the value of right operand, then condition becomes true. (a < b) is true. >= If the value of left operand is greater than or equal to the value of right operand, then condition becomes true. (a >= b) is not true. <= If the value of left operand is less than or equal to the value of right operand, then condition becomes true. (a <= b) is true.

Python Logical Operators Logical operators are used to combine conditional statements Operator Description Example

Python Logical Operators Logical operators are used to combine conditional statements Operator Description Example and Returns True if both statements are true x < 5 and x < 10 or Returns True if one of the statements is true x < 5 or x < 4 not Reverse the result, returns False if the result is true not(x < 5 and x < 10)

The if statement if The if statement is executed when a desired condition is

The if statement if The if statement is executed when a desired condition is met.

A small program a = 33 b = 200 If statement Condition is b

A small program a = 33 b = 200 If statement Condition is b > a Colon marks the end of the if b > a: statement print("b is greater than a") Notice the indentation, it’s a must! Output: b is greater than a

A small program a = 40 b = 315 if b < a: #You

A small program a = 40 b = 315 if b < a: #You can also write if(b<a): print("b is greater than a") What will be the output of the program?

Let’s code Example 1: If age is greater than 18 Then can get a

Let’s code Example 1: If age is greater than 18 Then can get a driver’s license. Code: print("Please enter age") user_age = input() if (user_age >= 18): print("You can get a driver’s license") Output: ERROR Please enter age 12 Traceback (most recent call last): File "/home/main. py", line 12, in <module> if (user_age > 18): Type. Error: unorderable types: str() > int() Can you identify the error? Yes, you need to typecast the input using int()

Let’s code Example 1 Code: print("Please enter age") user_age = int(input()) if (user_age >=

Let’s code Example 1 Code: print("Please enter age") user_age = int(input()) if (user_age >= 18): print("You can get a driver’s license") Output: Please enter age 12 Output: Please enter age 18 You can get a driver’s license Output: Please enter age 19 You can get a driver’s license

The else statement else The else statement is executed when all the above if

The else statement else The else statement is executed when all the above if condition fails. That means else cannot exist without if or elif.

Let’s modify Example 1 Code: print("Please enter age") user_age = int(input()) if (user_age >=

Let’s modify Example 1 Code: print("Please enter age") user_age = int(input()) if (user_age >= 18): print("You can get a driver’s license") else: print("Sorry you need to be at least 18 years old. ") Output: Please enter age 12 Sorry you need to be at least 18 years old. Output: Please enter age 18 You can get a driver’s license

The else if statement elif The elif statement is executed when all the above

The else if statement elif The elif statement is executed when all the above if condition fails. That means else cannot exist without if or elif.

Let’s further modify Example 1 Code: print("Please enter age") user_age = int(input()) if (user_age

Let’s further modify Example 1 Code: print("Please enter age") user_age = int(input()) if (user_age < 18): print("You cannot get a driver’s license") elif (user_age >= 18 and user_age <=100 ): print("You can get a driver’s license") else: print("Sorry you are too old to drive!") How will this program act based on the following inputs: 10, 18, 20, 99, 100, 120, -45?

Practice! • Code Example 2. Impress me!

Practice! • Code Example 2. Impress me!

Practice!! • Code Example 3. Show me some magic!

Practice!! • Code Example 3. Show me some magic!