Presentation is loading. Please wait.

Presentation is loading. Please wait.

IF statements.

Similar presentations


Presentation on theme: "IF statements."— Presentation transcript:

1 IF statements

2 Program with Arcade Games
This presentation has been directly lifted from the website above as will other future lessons. If you are keen to get ahead or need more support please feel free to use this website as it contain many suitable resources that are aimed at the correct level for your exam

3 Why do we need an IF statement?
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? 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.

4 First example of IF 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.

5 Basic Structure of IF The highlighted section above is a condition. This condition can be either true or false. IF it is true it will do whatever is indented (Can be more than one line).

6 Greater/less and equal to
The prior example checked for greater than or less than. Numbers that were equal would not pass the test. To check for a values greater than or equal, the following examples show how to do this: The <= and >= symbols must used in order, and there may not be a space between them. For example, =< will not work, nor will < =. When writing these statements out on a test, some students like to use the ≤ symbol. This ≤ symbol doesn't actually work in a program. Plus most people don't know how to easily type it on the keyboard. (Just in case you are curious, to type it hold down the 'alt' key while typing 243 on the number pad.) So when writing out code, remember that it is <= and not ≤. Many people lose points on tests for this reason; don't be that person.

7 Not equal to The next set of code checks to see if two items are equal or not. The operator for equal is == and the operator for not equal is !=. Here they are in action.

8 == Vs = It is very easy to mix up when to use == and =. Use == if you are asking if they are equal, use = if you are assigning a value The two most common mistakes in mixing the = and == operators are demonstrated below:

9 Indentation matters Indentation matters. Each line under the if statement that is indented will only be executed if the statement is true: Indentation must be the same. This code doesn't work:

10 Once an if statement has been finished, it is not possible to re-indent to go back to it. The test has to be performed again.

11 Using AND/OR An if statement can check multiple conditions by chaining together comparisons with and and or. These are also considered to be operators just like + or - are.

12 Common mistakes with AND/OR
A common mistake is to omit a variable when checking it against multiple conditions. The code below does not work because the computer does not know what to check against the variable c. It will not assume to check it against a.

13 Boolean Variables An if statement needs an expression to evaluate to True or False. What may seem odd is that it does not actually need to do any comparisons if a variable already evaluates to True or False The following code uses the not to flip the value of a between true and false.

14 It is also possible to use Boolean variables with and and or operators
It is also possible to assign a variable to the result of a comparison. In the code below, the variables a and b are compared. If they are equal, c will be True, otherwise c will be False.

15 ELSE and ELSEIF Below is code that will get the temperature from the user and print if it is hot. If the programmer wants code to be executed if it is not hot, she can use the else statement. Notice how the else is lined up with the i in the if statement, and how it is followed by a colon just like the if statement. In the case of an if...else statement, one block of code will always be executed. The first block will be executed if the statement evaluates to True, the second block if it evaluates to False.

16 It is possible to chain several if statements together using the else
It is possible to chain several if statements together using the else...if statement. Python abbreviates this as elseif.

17 Only the first block that is true will run
In the code below, the program will output “It is hot outside” even if the user types in degrees. Why? How can the code be fixed?

18 Comparing Text It is possible to use an if statement to check text.
A common mistake is to forget the quotes around the string being compared. In the example below, the computer will think that Paul is a variable that stores a value. It will flag an error because it has no idea what is stored in the variable Paul.

19 Case Sensitive comparisons
If the program needs to match regardless as to the case of the text entered, the easiest way to do that is to convert everything to lower case. This can be done with the lower command. The example below will take whatever the user enters, convert it to lower case, and then do the comparison. Important: Don't compare it against a string that has uppercase. If the user input is converted to lowercase, then compared against uppercase letters, there is no way a match can occur.


Download ppt "IF statements."

Similar presentations


Ads by Google