# The instruction (statement) if. Definition. Examples

### Contents

- 1. What is the
**if statement**? - 2. What
**forms**(variations) does an if instruction have in Python? - 3. The complete form of the
**if**statement (**if-elif-else**). Principle of implementation - 4. Shortened form
**if**. Principle of implementation - 5. Shortened form
**if-else**. Principle of implementation - 6. Shortened form
**if-elif**. Principle of implementation - 7. An
**example**that demonstrates the use of the**if-elif-else**construct (full form) - 8. An
**example**of solving a quadratic equation using**if**instruction in the interactive mode - 9. An
**example**of solving a quadratic equation using**if**instruction in program mode - 10. An
**example**of using the**if-elif**instruction. Determining the number of days in a month and the presence of a leap year - 11. An
**example**of using the nested**if-elif**instruction. Modified version of the program for determining the number of days in a month, taking into account the leap-year - 12. An
**example**that demonstrates the use of the shortened**if**statement. Search for a maximum value between three values **Related topics**

##### 1. What is the if statement?

Often, when implementing algorithms, it is necessary to make a choice of the course of the program execution depending on the given condition. To ensure this, modern programming languages (Java, Java Script, C #, C ++, and others) contain appropriate control statements, one of which is the if branching operator. The Python programming language also has an if statement in its arsenal, whose work is no different from the same instructions from other programming languages.

### ⇑

##### 2. What forms (variations) does an if instruction have in Python?

In Python, an if statement has the following four forms:

- full form if-elif-else;
- shortened form if;
- shortened form if-else;
- shortened form if-elif.

### ⇑

##### 3. The complete form of the if statement (if-elif-else). Principle of implementation

In the Python programming language, the general form of if statement is:

if expression1 : suite1 elif expression2 : suite2 ... elif expressionN : suiteN else: suiteN+1

here *expression1*, *expression2*, *expressionN* – corresponding conditional expression which is checked for validity. If the conditional expression is true, then its value is True. In this case, the corresponding suite of operators *suitet1*, *suite2*, *suiteN*, *suiteN+1* is executed.

The if statement works as follows. If *expression1 *is True, then a set of operators is executed; otherwise, the transition to checking the condition of *expression2* occurs. By the same principle, *expression2*, …, *expressionN* branches are processed. If none of the expressions *expression1*, *expression2*, …, *expressionN* is executed (equal to False), then *suiteN+1* is executed, which follows the keyword else.

### ⇑

##### 4. Shortened form if. Principle of implementation

The short form of the if statement does not contain elif and else blocks and has the following form.

`if expression : suite`

In this case, the *suite* of statements is executed if the *expression* is true, that is, it is equal True. Otherwise, the next after the if statement is executed.

### ⇑

##### 5. Shortened form if-else. Principle of implementation

Shortened form can have a general view if-else

if expression : suite1 else: suite2

The above form of the if statement works according to the following principle. First, the value of the *expression* is checked. If the *expression* is true (equal to True), then a *suite1* of operators is executed. Otherwise (the expression is False), a *suite2* of operators is executed.

### ⇑

##### 6. Shortened form if-elif. Principle of implementation

The shortened form if-elif can be:

if expression1 : suite1 elif expression2 : suite2 ... elif expressionN : suiteN

In this form, the if statement is similar to the switch statement in other programming languages.

The if-elif operator works according to the following principle. If the value of *expression1* is True, then *suite1* is performed and the if-elif operator is exited. Otherwise (the value of *expression1* is False), the transition to the next branch of the operator to check *expression2* occurs. Thus, that branch of the if-elif operator is executed, the value of the expression in which is True. If none of the expressions of *expression1*, *expression2*, …, *expressionN* is executed, then the transition to the next statement after if-elif occurs.

### ⇑

##### 7. An example that demonstrates the use of the if-elif-else construct (full form)

Given an integer value ** n** = 1..3 and radius

**. For a given value of**

*r***, calculate:**

*n*- 1 – circumference;
- 2 – area of a circle;
- 3 – the volume of a sphere.

The text of the program in program mode is as follows

n=2 # input data r=5.5 pi=3.1415 # calculation, if statement if n==1: s=2*pi*r print("Circumference = ",s) elif n==2: s=pi*r*r print("Area of a circle = ",s) elif n==3: s=4.0/3*pi*r*r*r print("Volume of a sphere = ",s) else: print("Incorrect data entered ") print("----------------------")

As a result of executing the above code, the following result will be obtained

Area of a circle = 95.03037499999999 ----------------------

### ⇑

##### 8. An example of solving a quadratic equation using if instruction in the interactive mode

Below is an example of solving a quadratic equation for given coefficients **a**, **b**, **c**. The example demonstrates the abbreviated form of the if statement.

>>> a = 8 # set initial values of a, b, c >>> b = 3 >>> c = -2 >>> d = b*b-4*a*c # calculate discriminant >>> if d>=0: x1=(-b-(d**0.5))/(2*a) x2=(-b+(d**0.5))/(2*a) >>> x1,x2 (-0.7215002340823456, 0.34650023408234565) >>>

Using the following code, you can check the values of the discriminant **d** and the correctness of the result

>>> d 73 >>> a*x1*x1+b*x1+c 0.0 >>> a*x2*x2+b*x2+c -4.440892098500626e-16 >>>

As can be seen from the result, the value of x1, x2 is close to zero, that is, the solution is correct.

### ⇑

##### 9. An example of solving a quadratic equation using if instruction in program mode

The example demonstrates the use of the if operator to find a solution to a quadratic equation. The code is for program mode. Using the print() function, the result of the calculation and the result of the check are displayed. If the discriminant value d<0, then the message “Equation has no roots” is displayed.

a=2 # set values a,b,c b=5 c=-4 d=b*b-4*a*c # calculate d if d>=0: # if statement x1=(-b-(d**0.5))/(2*a) x2=(-b+(d**0.5))/(2*a) print("d = ",d) # result output print("x1 = ", x1) print("x2 = ", x2) print print("Checking:") print("a*x1*x1+b*x1+c = ", a*x1*x1+b*x1+c) print("a*x2*x2+b*x2+c = ", a*x2*x2+b*x2+c) else: print("equation has no solution")

As you can see from the example, if in the if statement you need to execute several operators in a row, then they are highlighted with indents.

After starting execution, the following result will be obtained in the Python environment:

d = 57 x1 = -3.1374586088176875 x2 = 0.6374586088176875 Checking: a*x1*x1+b*x1+c = 0.0 a*x2*x2+b*x2+c = 0.0 >>>

### ⇑

##### 10. An example of using the if-elif instruction. Determining the number of days in a month and the presence of a leap year

The year and month are given. Develop a program that defines:

- whether the given year is leap-year;
- the number of days in a given month of a given year. You need to calculate the leap year.

The text of the program is as follows

year=2100 # set a year month=2 # set a month # # definition of leap-year f_leap_year=True # at the beginning accept that year is a leap-year if year%400==0: print("leap year") elif year%100==0: print("not a leap year") f_leap_year=False elif year%4==0: print("leap year") else: print("not a leap year") f_leap_year=False print("---------------------") # # determining the number of days in a month print("Number of days in month:") nDays=31 # at the beginning the number of days is 31 if (month==2)and(f_leap_year==True): nDays=29 elif (month==2)and(f_leap_year==False): nDays=28 elif (month==4)or(month==6)or(month==9)or(month==11): nDays=30 print(nDays)

In the above code, the logical operations “and” and “or” are additionally used.

As a result of code execution, the following result will be obtained.

not a leap year --------------------- Number of days in month: 28 >>>

### ⇑

##### 11. An example of using the nested if-elif instruction. Modified version of the program for determining the number of days in a month, taking into account the leap-year

This example demonstrates the use of nested if-elif instructions for calculating the number of days in a month based on the following input data:

- year;
- number of the month of the year.

It is important to calculate whether a given year is a leap-year.

The text of the program is as follows:

year=2108 # set a year month=2 # set a month # # calculation of leap-year f_leap_year=True # at the beginning accept that year is a leap year if year%400==0: print(year,"- leap-year") elif year%100==0: print(year,"- not leap-year") f_leap_year=False elif year%4==0: print(year,"- leap-year") else: print(year,"- not leap-year") f_leap_year=False print("---------------------") # # determining the number of days in a month if month==2: if f_leap_year==True: # nested if-else statement nDays=29 else: nDays=28 elif (month==4)or(month==6)or(month==9)or(month==11): nDays=30 else: nDays=31 print("Number of days in the month:", month) print("nDays=", nDays)

When determining the number of days for month 2 (February), the nested if-else statement is used. Nesting is determined by indentation.

... if month==2: if f_leap_year==True: # nested if-else statement nDays=29 else: nDays=28 elif ...

After starting the program, the program will produce the following result:

2108 - leap year --------------------- Number of days in the month: 2 nDays= 29 >>>

### ⇑

##### 12. An example that demonstrates the use of the shortened if statement. Search for a maximum value between three values

The example demonstrates the use of an shortened if statement to find the maximum value between three values **a**, **b**, **c**. The text of the program is as follows:

# search for the maximum between the values a, b, c # set arbitrary values a,b,c = 20,30,17 # search for the maximum mx mx=a; if mx<b: mx=b if mx<c: mx=c print("Max = ", mx)

After running, the following result will be obtained.

`Max = 30`