Menu
×
   ❮   
     ❯   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING

If Statements

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.

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:

startdice = random(1,6)falsetrueTry againYou got 6!dice == 6?Conditiondice = random.randint(1,6)ifelse:dice == 6:Conditionprint('You got 6!🥳')launchConfetti()Code runsif conditionis trueprint('Try again')Code runsif conditionis false

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.

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

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

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 allways 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:

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

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

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

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

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

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.

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')
Run Example »

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.

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')
Run Example »

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.

age = 19 age < 13true'child'falsefalseage < 20true'teen'age > 17true'and adult''adult'

Age

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



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.