Codesters FAQ is the official FAQ site for codesters.com. We will continue to update this blog with the most common questions we receive form teachers using Codesters.

How do I use the "if" and "if-else" blocks?

If statements, or conditionals, start with a test that compares two values. If the test statement is true, the code indented underneath runs. If the test statement is not true, the block of code underneath is skipped and the rest of the program runs.

  1. Click on the if-else toolkit
  2. Drag the If block into the editor
  3. Notice the == This is called a comparision operator. == compares a variable to a value. In this example if the variable "choice" contains the value "yes" then the code under the "if" will run
  4. The code to run as part of the "if" needs to be indented 4 spaces (same as a tab)
  5. The "if" code ends once the next line underneath is un-indented.
if choice == "yes":
    # add your code here
    
    

With the If-Else statement the comparison test has two results. If the test statement is true, the code indented underneath the "if" runs. If the test statement is not true, the block of code underneath the "else" runs.

  1. Click on the if-else toolkit
  2. Drag the If else block into the editor
  3. The code to run as part of the "if" needs to be indented 4 spaces (same as a tab) as well as the code under the "else"
  4. The "if" and the "else" work as a pair so the code to run following the "if-else" pair would be un-indented folllowing the code under the "else"
if choice == "yes":
    # add your code here
    
else:
    # add your code here

#the code to run after the if-else would be here