Program Flow And Control Statements:

Contents: Click for quick access

1. If Statement:

It is used in decision making operations. The program runs only until the given condition is true and the program will terminate if the condition gets false. As a real world example: A program to check either pass or fail.

Program:

``````marks = 44
if marks > 40:
print("Pass")``````

Output:

``Pass``

In the above program, the student status of either pass or fail is checked. The mark is 44 which is greater than 40. So, the pass statement is printed.

2. If else statement:

It is used in decision-making operations. The program checks whether a given condition is true or false. If the condition is true, it will run the block of statements and if the condition is false, the else part statement will be run. As a real-world example: Checking eligibility for applying for citizenship!

Program:

``````age = int(input("Enter your age: "))

if age >= 16:
print("You can apply for citizenship.")
else:
print("Candidates must be 16 or above age to apply.")``````

Output:

``````Enter your age: 12
Candidates must be 16 or above age to apply.``````

In the above program, the eligibility criteria of whether the candidate can apply or not for citizenship is checked. If the candidate age is 16 or more, the “You can apply for citizenship.” The statement is printed and if the candidate age is below 16 years, the “Candidates must be 16 or above age to apply.” statement is printed.

3. If elif else statement:

It is quite similar to the if else statement. But the only difference in the if-else-elif statement is it allows checking multiple conditions. Eg; A program to check two numbers either which are greater where two conditions are checked either they are greater or  equal to each other.

Program:

``````a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a > b:
print("First number is greater than second number.")
elif a == b:
print("Both numbers are equal")
else:
print("Second number is greater than the first number.")``````

Output:

``````Enter first number: 3
Enter second number: 4
Second number is greater than the first number.``````

In the above program two numbers were asked from the user. After that,

The numbers were checked to see which one is the greater number. Also, by elif statement either they were equal or not is checked.