Saturday, July 2, 2022

What is the if-else statement in Python?

The second form of if statement is called the alternative execution and in this type there are two possibilities and as before condition determines which one gets executed. The syntax of alternative execution in general form can be written as:
if condition:
indented statement
else:
indented statement
In the 1. code line after the if the condition is tested. If the condition is True then an indented statemement is executed. If the condition is False then the indented statement is not executed and the program goes to the else command and executes the indented statement underneath the else command. In the following example, the user will type two numbers (integers or floats) and decide between addition or subtraction.
Example 1 Create the program for addition or subtraction of two numbers using input command and using if-else statement.
Solution: The numbers will be entered by the user and each value will be assigned to specific variable (\(a\), and \(b\)), respectively. Then the user is going to type the mathematical operation that he wants to be applied to those two numbers. In this example, two mathematical operations are available: addition and subtraction. The type of mathematical operation will be saved as a string under the variable name "cond". The Alternative execution or if-else statement will start by checking if the cond is equal to "add" or not. If the condition is True then the indented block underneath if statement will be executed. In this indented block, the variable res will be created that is equal to a + b. Then the "res" will be printed out. On the other hand, if the condition is False then the indented block will be skipped and the program will continue to execute the indented statements underneath the else command. In this block the "res" is created which is equal to a -b and the result will be printed out.
a = float(input("The first number is:"))
b = float(input("The second number is:"))
cond = str(input("Enter the math operation (For addition type add,
for subtraction type sub): "))
if cond == "add":
res = a + b
print("a + b = ", res)
else:
res = a-b
print("a - b = ", res)
In the 1. code line the user has to write the first number and the number will be converted to float type and assigned to variable \(a\). In the 2. code line the user has to write the second number that will be converted to float type and assigned to variable \(b\). In the 3. and 4. lines the user must choose math operation either "addition" or "subtraction" by writing "add" or "sub". The typed string is saved as string type and assigned to the variable "cond". Then the condition is tested if it is equal to "add" or not. If the cond is equal to "add" than the indented statements under if cond == "add": are executed. In this case, a new variable named res is created and the addition of two variables (a and b) is performed and the result is assigned to the variable res. The "res" value is printed using print function print("a + b = ", res). If the cond is not equal to "add" i.e. the value is False then the if code block is skipped and indented statements under the else command are executed. In this indented block the two values are subtracted a-b and assigned to res variable. Then the result is printed out using print("a - b = ", res) When the code is executed and user provides necessary information, the following output is generated.
The first number is:50
The second number is:60
Enter the math operation (For addition type add, for subtraction type sub): sub
a - b = -10.0
In the next example we are going to write the code that is cheking if the nubmer is even or odd.
Example 2 Write the program which takes the number entered by the user and checks if the number is even or odd.
Solution: The program will ask the user to type in the number. This number will be transformed to integer type and assigned to variable named "number".
number = int(input("Enter the number: "))
The condition in if statement starts by dividing the "number" by 2 and obtaining remainder. If the remainder is 0 then the condition is True and the indented block under the if statemetn will be executed which in this case is to print out the string "The number is even!". The if statement and the indented block is given below.
if number%2 == 0:
print("The number is even!")
In case the division of the "number" by 2 gives the remainder other than 0 this means that number is odd which will automatically skip the "if block" and execute the "else block". The indented statement in the else block will print out that the number is odd.
else:
print("The number is odd!")
The entire code is given below.
number = int(input("Enter the number: "))
if number%2 == 0:
print("The number is even!")
else:
print("The number is odd!")
The two scenarios from previous code block is shown below. In the first scenario the user typed in the even number.
Enter the number: 10
The number is even!
In the second scenario the user typed in the odd number.
Enter the number: 3
The number is odd!

No comments:

Post a Comment