Quiz Games and If Statements

How do we tell if a player has beat the high score? How can we tell if he has run out of lives? How can we tell if she has the key required to open the locked door?

Video: Introduction to the if statement

What we need is the if statement. The if statement is also known as a conditional statement. (You can use the term ``conditional statement'' when you want to impress everyone how smart you are.) The if statement allows a computer to make a decision. Is it hot outside? Has the spaceship reached the edge of the screen? Has too much money been withdrawn from the account? A program can test for these conditions with the if statement.

Basic Comparisons

Here are a few examples of if statements. The first section sets up two variables (a and b) for use in the if statements. Then two if statements show how to compare the variables to see if one is greater than the other. Press the ``Step'' button and see how the computer evaluates the code.
# Variables used in the example if statements
a = 4
b = 5

# Basic comparisons
if a < b:
    print("a is less than b")

if a > b:
    print("a is greater than b")

print("Done")
Variables:
a=4
b=5
Output:
a is less than b
Done

Since a is less than b, the first statement will print out if this code is run. If the variables a and b were both equal to 4, then neither of the two if statements above would print anything out. The number 4 is not greater than 4, so the if statement would fail.

To show the flow of a program a flowchart may be used. Most people can follow a flowchart even without an introduction to programming. See how well you can understand Figure fig.flowchart1.
Fatal error: Uncaught ArgumentCountError: Too few arguments to function figure(), 4 passed in /var/www/datas/dossiers_personnels/sebastien/Projets/Projets Python/programarcadegames.com/traduction/04_if_statements.php on line 196 and exactly 5 expected in /var/www/datas/dossiers_personnels/sebastien/Projets/Projets Python/programarcadegames.com/traduction/header_test.php:113 Stack trace: #0 /var/www/datas/dossiers_personnels/sebastien/Projets/Projets Python/programarcadegames.com/traduction/04_if_statements.php(196): figure('fig.flowchart1', 'chapters/03_if_...', 'Flowchart', 45) #1 {main} thrown in /var/www/datas/dossiers_personnels/sebastien/Projets/Projets Python/programarcadegames.com/traduction/header_test.php on line 113