Python program of decision making by using if & if-else statement
Python program of decision making, There are you will learn how to make control decisions in the Python program.
C program of decision making by using if & if-else statement
C++ program of decision making by using if & if-else statement
By using the if statement:
The syntax of the if the statement is the same C programming:
{
// statement 1 to be executed if the test expression 1 is true
}
Let us understand this example through the Python program:
# Python program of decision making by using if statement
print("Enter an integer value::")
n = int(input())
# true if number is less than 6
if (n < 6):
print("Condition is true.\n")
Output:
Enter an integer value::
5
Condition is true.
By using the if...else statement:
The syntax of the if...else statement is:
// statement 1 to be executed if the test expression 1 is true
} else {
// statement2 to be executed if the test expression 1 is false
}
Example-1: In this example, you will understand how true a condition occurs.
# Python program of decision making by using if-else statement
print("Enter an integer value::")
n = int(input())
# true if number is less than 6 otherwise it will be false
if (n < 6):
print("Condition is true.\n")
else:
print("Condition is false.\n")
Output:
Enter an integer value::
5
Condition is true.
Example-2: In this example, you will understand how false a condition occurs.
# Python program of decision making by using if-else statement
print("Enter an integer value::")
n = int(input())
# true if number is less than 6 otherwise it will be false
if (n < 6):
print("Condition is true.\n")
else:
print("Condition is false.\n")
Output:
Enter an integer value::
7
Condition is false.
By using the if...else ladder:
The syntax of the if...else ladder is:
// statement( s )
} else if( test expression 2 ) {
// statement( s )
} else if ( test expression 3 ) {
// statement( s )
}
.
.
else {
// statement( s )
}
Also, Learn:
Python program to find the ASCII value of a character
Java program of decision making by using if & if-else statement
Let us understand this example through the Python program:
# Python program of decision making by using the if-elif-else ladder
print("Enter an integer value::")
n = int(input())
if (n == 0):
print("Fisrt condition is true.\n")
elif (n == 1):
print("Second condition is true.\n")
elif (n == 2):
print("Third condition is true.\n")
else:
print("Condition failed!\n")
Output:
Enter an integer value::
1
Third condition is true.
By using the nested if...else statement:
The syntax of the nested if...else statement is:
else if( test expression 2 ) {
// statement(s)
} else if ( test expression 3 ) {
// statement(s)
}
.
.
else {
// statement(s)
}
} else if ( test expression m ) {
// statement(s)
}
.
.
else {
// statement(s)
}
Example-1: In this example, you will understand the first input is less than the second input.
# Python program of decision making by using the nested if-else statement
print("Enter the two integer values::\n")
n, m = int(input()), int(input())
if (n >= m):
if (n == m):
print("Result: ", n, " = ", m, "\n")
else:
print("Result: ", n, " > ", m, "\n")
else:
print("Result: ", n, " < ", m, "\n")
Output:
Enter the two integer values::
5
7
Result: 5 < 7
Example-2: In this example, you will understand the first input is greater than the second input.
# Python program of decision making by using the nested if-else statement
print("Enter the two integer values::\n")
n, m = int(input()), int(input())
if (n >= m):
if (n == m):
print("Result: ", n, " = ", m, "\n")
else:
print("Result: ", n, " > ", m, "\n")
else:
print("Result: ", n, " < ", m, "\n")
Output:
Enter the two integer values::
7
5
Result: 7 > 5