Conditional programmingOne of the biggies in programming is to tell the computer to do something in a given situation.
This is called using an IF ... THEN ... ELSE statement. Quite simply, IF a condition is met, THEN do X, ELSE do Y. A good way of representing it is graphically. Please see below. |
Learning objectives and outcomesI can use an IF statement to tell the computer what to do in different situations.
Level 4: I can use an IF statement to make the computer say something given a particular variable. Level 5: I can use an IF ... THEN ...ELSE statement to make the computer do something that I want it to. Level 6: I can use a nested IF .... THEN ...ELSE statement to make the computer do something given 3 choices. I can declare numerical values as variables. Level 4: with help I can declare a number as a variable. Level 5: with help I can use numerical variables to make calculations. Level 6: I can use numerical variables to make calculations with little or no help. |
TRY IT OUT
Load up Small Basic and watch the video below to try out an IF ... THEN .... ELSE statement. The video shows a very simple condition - is it before 12? if so, say "Good morning youtube" else "Good evening youtube". Once you have tried it out, you can play around with your own conditions. You can make your program more sophisticated by declaring text variables as you saw in the last lesson. Don't forget to use ENDIF to close the IF statement. If you want to make your program use more than 1 question you need to use the command ELSEIF instead of ELSE and then write the statement. Take screeenshots of your code and be prepared to explain them in your blog.
declaring variables II
In the second half of the video you saw how to declare numbers as variables and performing calculations with them. Have a go at it yourself using different numbers. Take screeenshots of your code and be prepared to explain them in your blog.
take it further!
You've now been taught how to declare variables (both text and integer) and to do simple IF ... THEN .... ELSE statements. See if you can produce a little program which checks the gender of a person and tells them whether they should go to Helenswood or William Parker. Take screeenshots of your code and be prepared to explain them in your blog.
blog it!
Blog your images of your code together with a short explanation of what you did.
Explain: IF... THEN .... ELSE statements and what you did. Try to explain what a variable is.
Explain: IF... THEN .... ELSE statements and what you did. Try to explain what a variable is.