bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Programming/Programming Concepts
Programming•Programming Concepts

If Statements in Programming

If statements allow your program to make decisions, so it can do different things depending on the situation.

What is an If Statement?

An if statement runs a block of code if the condition is true.

We do similar things in real life, like using an umbrella if it's raining, or wearing a coat if it's cold.

See how an if statement works in the simple game below. The goal is to get 6 when you roll the dice.

To make the game do something different depending on the dice result, we use if with a condition that checks if the dice result is 6.

In case the condition is true, we print "You got 6!" and launch the confetti. And in case the condition is not true, we print "Try again".

Here is the flow chart and Python code for the game:

We must use else in the code above, to handle the case when the dice is not 6, so that we can write "Try again".

Click the "Run Example" button below to see the dice rolling code in action.

Runnable example

dice = random.randint(1,6)
print('You rolled a ' + str(dice))

if dice == 6:
  print('You got 6!🥳')
else:
  print('Try again')

When Should I Use an If Statement?

When you want your program to do something different depending on the situation, you should use an if statement.

For example, in case you want your program to print "Welcome!" when the user enters the correct password, and "Access denied" when the user enters the wrong password, you should use an if statement.

If, Else, and Else If

An if-statement always starts with an if .

An if-statement can contain zero or many else if , and zero or one else .

When else is present, it has to come last, after all the else if .

The else statement ensures that one (and only one) of the code blocks will execute.

Sometimes it is enough to just use a single if , like this:

Runnable example

age = 32
print('Age: ' + str(age))

if age > 17:
  print('You are an adult!')

But usually, we also want to handle the case when the condition is not true, so we use an else statement for that.

Runnable example

age = 10
print('Age: ' + str(age))

if age > 17:
  print('You are an adult!')
else:
  print('You are not an adult yet.')

The code block that belongs to the else will only be executed in case the condition in the if is false.

We can also use else if to check more than one condition, so that we get more than two outcomes.

Runnable example

age = 15
print('Age: ' + str(age))

if age < 13:
  print('You are a child')
elif age < 20:
  print('You are a teenager')
else:
  print('You are an adult')

You can only have one if statement, and only one else statement, but you can have as many else if statements as you want.

Also, the if is always first, the else is always last, and the else if statements are in between.

Nested If Statements

A nested if statement is an if statement inside another if statement.

Nested if statements are useful in cases where you want to check a condition, only if another condition is true.

Runnable example

age = 19
print('Age: ' + str(age))

if age < 13:
  print('You are a child')
elif age < 20:
  print('You are a teenager')
  if age > 17:
    print('and an adult!')
  else:
    print('You are an adult')

In the code above, the nested if statement allows us to filter out the special case of ages 18 and 19, when you are both a teenager and an adult.

In the flowchart above, we can see that the code block for age > 17 is only executed if the age is 18 or 19.

Previous

Constants in Programming

Next

Arrays in Programming

This chapter

Overview
25

Lessons

105m

Read time

1. Understanding The Concepts of Programming2. What is Programming ?3. Variables in Programming4. Constants in Programming5. If Statements in Programming6. Arrays in Programming7. Loops in Programming8. Functions in Programming9. Recursion in Programming10. Scope in Programming11. Strings in Programming12. Data Types in Programming13. Type Casting in Programming14. Operators in Programming15. Arithmetic Operators in Programming16. Assignment Operators in Programming17. Comparison Operators in Programming18. Logical Operators in Programming19. Bitwise Operators in Programming20. Comments in Programming21. Input and Output in Programming22. Bits and Bytes in Programming23. Binary Numbers in Programming24. Hexadecimal Numbers in Programming25. Boolean Algebra

On this page

What is an If Statement?When Should I Use an If Statement?If, Else, and Else IfNested If Statements