# Introducing if statements

If statements provide a mechanism to determine the result of a condition, which can then be acted upon. They are used in all kinds of scenarios. Here's an example that demonstrates using an if statement.

    score as integer = 100

    do
    	if score = 100
    		print ( "the score is 100" )
    	endif

    	sync ( )
    loop

The variable score is declared and assigned a value of 100. This is followed by a do loop where we'll encounter our if statement.

    if score = 100
    	print ( "the score is 100" )
    endif

The if statement uses an expression to check whether the score is equal to 100. If this expression is true then any code following it, at least up to the endif line, will be acted upon. When you run the program the string "the score is 100" will be displayed on screen. If the score is any value other than 100 then this string will not get displayed on screen as the condition that the if statements requires will be false.

An if statement can be used to evaulate any kind of expression. Checking whether the score is 100 can be written in any number of ways.

    score as integer = 100
    target as integer = 100

    do
    	if score = 50 + 50
    		print ( "1. The score is 100" )
    	endif

    	if score = 10 * 10
    		print ( "2. The score is 100" )
    	endif

    	if score = 70 + 10 * 5 - 20
    		print ( "3. The score is 100" )
    	endif

    	if score = target
    		print ( "4. The score is 100" )
    	endif

    	sync ( )
    loop

Each if statement checks whether the expression is true and then runs the code within its block.

The next example declares a variable score and assigns it a value of 50. It then performs a do loop. Within this loop a call to the print command is made, displaying the string "a" on screen. Next an if statement is used to determine whether the score is equal to 100. If this is the case two strings will be displayed on screen. The if statement block ends by using the keyword endif. This is followed by a call to the print command, which will display the string "b" on screen. What do you expect to see on screen when this program runs?

    score as integer = 50

    do
    	print ( "a" )

    	if score = 100
    		print ( "the score is 100" )
    		print ( "the if statement is true" )
    	endif

    	print ( "b" )

    	sync ( )
    loop

When you run the program notice how the two lines saying "the score is 100" and "the if statement is true" are not displayed on screen. This is because the score variable has been set to 50, therefore the condition for the if statement is false and the code associated with it is not executed. Try changing the value of score to 100 and run the program again. This time the condition is true, so you'll see the extra strings displayed on screen.