βͺοΈ Conditionals
Conditionals take an expression, which is code that evaluates to determine a value, and checks if it is π
or π
. If itβs π
, we can tell our program to do one thing β we can even account for π
to do another.
As we write more complex programs, conditionals allow us to address multiple scenarios and make our programs more robust.
If Statement
The βͺοΈ statement is very important. It allows for conditional execution of a code block. The whole syntax is:
- If the condition evaluates to
π
, the code block will be executed. - And if it evaluates to
π
, itβll be ignored.
This example will display βa is greater than bβ if the content for variable a
is greater than b
:
βͺοΈ a βΆοΈ b ππ π€a is greater than bπ€βοΈπ
Else Statement
The π
extends an βͺοΈ
statement to execute an additional code block in case the expression in the if
statement evaluates to false.
For example, the following code would display βa is greater than bβ if a
is greater than b
, and a is not greater than b
otherwise:
βͺοΈ a βΆοΈ b ππ π€a is greater than bπ€βοΈππ ππ π€a is not greater than bπ€βοΈπ
Note: The π
statements is only executed if the βͺοΈ
statement evaluated to π
, and if all π
βͺοΈ
statements evaluated to π
too.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Emojicode on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Free course
Learn Emojicode
π Learn about Emojicode, a programming language that consists of emojis! πBeginner Friendly1 hour