Let's put together a basic if statement that displaus Hello, World!.
if True:
print("Hello, World!")
Hello, World!
Correct
Yes! Because the condition is True, th computer executes the line.
To make practical sense, an if statement needs a boolean value that's only True if a condition is fulfilled.
Conditions in if statements work with all the comparisons we've already explored.
password = "Swordfish"
if password == "Swordfish":
print("Welcome!")
Welcome!
Correct
What a beautiful if statement!
Syntax Highlighting: Syntax highlighting is a feature of text editors that are used for programming, scripting, or markup languages, such as HTML. The feature displays text, especially source code, in different colors and fonts according to the category of terms. This feature facilitates writing in structured langugage such as a programming language or a markup language as both structures and syntax errors are visually distinct. Highlighting does not affect the meaning of the text itself; it is intended only for human readers.
we can also build if statements that depend on numbers.
Let's check if we gathered enough points to win a video game.
score = 100
if score == 100:
print("You won!")
You won!
Correct
Great! Comparing numbers is very useful for things like programs that monitor the weather or keep track of scores.
We can also use the < sign to check if score is less than 100.
score = 50
if score < 100:
print("You need more points!")
You need more points!
Correct
Nice work! This code prints "You need more points!" to the console when the score is less than 100.