Introducing Small Basic

• Easy to learn
• Invented in 2008 especially for beginner programmers
• User-friendly programming environment for writing and testing code

## Red

Red challenges are the hardest to do and will take some serious thinking, we are not expecting everyone to be able to do these straight away.  You should not attempt red challenges unless you have completed all of the Amber challenges.

## Amber

Amber challenges are a bit of a step up from Green and will often involve some maths. Do not attempt Amber challenges until you have completed all of the green challenges.

## Green

Green challenges are suitable for everyone.  Do not think you can just skip them as they are full of useful tips and skills that you will need in order to complete the harder challenges.

## Variables

What is a variable?

• A Variable is a location in memory, that stores a value, that can change as the program is running.
• Think of it like a shoe box, that you give a label and put data inside.

Rules for naming variables

• A variable name should reflect what is stored in it e.g. name, score, answer etc
• I would suggest using lowercase for all of your variables as it will avoid errors
• Variables cannot contain spaces but _ could be used instead

01) Create the same program shown in the video link above

02) Create a program that will ask the user to INPUT a number and then OUTPUTS its square

03) Create a program that will ask the user to INPUT 2 numbers, store them in variables, add them up, and OUTPUT the answer.

04) Write a program that will convert Litres to Gallons (1 litre = 0.22 Gallons). The program should prompt the user to INPUT the amount of litres they wish to convert and then OUTPUT this in Gallons.

05) Maths round program. Edit your program to produce the following OUTPUT. NOTE: You will need to use the math.round function

06) Currency converter

Create a program that will convert Pounds (£) to Dollars (\$). The user should INPUT the amount of pounds to be converted and then program should OUTPUT this in dollars. Note: You will need to search the latest exchange rate!

The recipe for 1.25 litres of lemonade is:

• 3 unwaxed lemons (price ? each) (Ask the user to enter a price)
• 100g caster sugar (price ? per kg) (Ask the user to enter a price)
• 1 litre cold water (free!)

Write a program that will:

• Calculate the cost of making 1.25 litres of lemonade.
• Calculate the cost price per cup, assuming that 1 cup of lemonade = 0.25 litres
• Round the result to the nearest penny. (Math.Round function)
• Output the result

## Selection

What is Selection?

“The pathway through a program is selected by using a condition to decide on what instructions to execute next”

08) Can you create a program like the one shown below and test that it works?

09) Can you edit the program above so that it OUTPUTS either; “Good Morning”, “Good Afternoon” or “Good Evening”?

10) Can you create a program that does the following:

11) Elseif: Make your own version of the program shown below and test that it works as intended…

12) Now can you this to your program. HINT: You will need to use an “Elseif” statement!

13) Write a program to allow the user to INPUT the length and width of a rectangle and calculate the area. If the length and width are equal it should OUTPUT “This is a square of area” + area. Otherwise it should OUTPUT “This is a rectangle of area” + area.

14) Write a program to display a menu of options:

The user then INPUTS a choice and the program OUTPUTS a message such as “You chose Computer Science”. If they choose option 4, the program OUTPUTS “Goodbye”.

16) Write a program to INPUT the value of goods purchased in a shop. A discount is subtracted from the value to find the amount owed. If the total is greater than or equal to £200, 10% discount is given. If the total is between £100 and £199.99, 5% discount is given. Otherwise, no discount is given. Your program should OUTPUT a receipt that shows the original total and the new total with the correct discount applied. For example:

17) Login 2.0: Can you now improve your login system so that it will OUTPUT to the user which, if any, of their details are incorrect. It should OUTPUT something like this…

This slideshow requires JavaScript.

18) Write a program to calculate car park charges. Up to 2 hours costs costs £3.50, up to 4 hours £5.00, up to 12 hours £10.00. The driver enters the number of hours they require and their registration number and the machine prints the current time, expiry time, registration number and charge on a ticket. For example;

19) Can you create a vending machine program. Your program MUST have at least 8 options, like the example shown below. The user should input their choice and money. They should then be given the correct change depending on how much money they input and their food selection.

## For Loops

Iteration means the repetition of a process.

In computer programming ‘iteration’ is used to loop around and around a piece of code until a condition is met that ends the iteration.
For example:
FOR counter = 1 TO 10
PRINT counter
NEXT
The computer code above will increase counter by 1 with every iteration. On each iteration the value of counter is printed. When counter reaches 10, the iteration stops.

Watch Mark Zuckerberg explain why loops are so useful.

Now try these tasks using For Counter loops.

20) Recreate the program shown below and run it.

21) Copy the program above.  Change the ’10’ to a ’12’ and see how it changes the output.

22) Edit your simple For loop program so it produces the following output:

23) This program introduces the Graphics Window and Turtle Graphics.  In the program below, GraphicsWindow.Height and GraphicsWindow.Width set the height and width of the graphics window. Turtle.Move tells the Turtle how far to move and Turtle.Turn tells the Turtle how far to turn (clockwise) in degrees.  The Turtle starts in the middle facing upwards. Recreate the program below and see what it does. Note that we are now introducing the variable ‘sides’  into the For loop.

24) Copy and edit your last program so it will output the following in the graphics window.  You will need to turn your Turtle before the loop starts to get it to look exactly like this:

25) Copy and edit your last program so it will output an octagon in the graphics window:

26) And again so it outputs a star like below.  The angles on this one are a bit more tricky and like the triangle, you’ll need to turn the Turtle before it starts drawing if you want to match the output below:

27) How many times will you have to loop to create a circle?  Have a go and see, experiment!

28) You can create a spiral by increasing the distance the Turtle moves each iteration of the loop but how do you do this? Try using the ‘counter’ variable in the Turtle.Move value.  See if you can create something like this:

29) Back to the TextWindow.  Can you use iteration flow control to output the following.  You’re going to need selection too (If… Then… Else… EndIf). You can use something like Math.Remainder(counter,2) = 1 to work out if a number is odd or not:

30) Write a program that will output the following using iteration.  There are two ways to do this, see if you can find both.  One way involves a ‘Step’ in the For loop and the other is done by adding something to ‘counter’ i.e. counter = counter + 1 inside the iteration:

31) A prime number is a number that is only divisible by itself and 1. Can you use the selection and iteration programming skills that you have learnt to write a program that asks for a number between 1 and 1000 and then, depending on whether it is a prime number or not, outputs one of the two outputs below:

32) Welcome to the world of nested loops!  This is a very common technique used in programming.  See if you can write a program that gives and output similar to a 24 hour clock i.e. starting at 00:00, 00:01 through to 23:58 , 23:59.  You will need two lots of iteration to achieve this, one For loop that deals with the hours and a second For loop that deals with the minutes but inside the hour loop.  Good luck!

33) Recycle your circle program from earlier and see if you can get your Turtle to make the pattern below. There are 18 overlapping circles so you are also going to need a For loop inside a For loop to achieve this:

34) Write a program that will output a simple options menu and allow the user to select one of three shapes. Then get your Turtle to draw the requested shape.  Here’s the challenge though; you can only use one For loop in your program!  Your output should look something like this:

35) Recreate the program below and work out what it does and how it does it.  Here’s a tip; once you’ve typed out one of the ‘If… Then… Else… EndIf’ segments, you can copy it, paste it three times and edit the numbers.  This saves lots of typing but also requires a high attention to detail if you don’t want to inadvertently introduce a logic error.

36) If I told you the program above is a 4-bit converter, turn it into an 8-bit converter. If you succeed, ask Sir for a medal!

#### While loops

While loops are controlled by a condition at the start of the loop and they keep on going while the condition is True.  A while loop will never run the code inside it if the condition is initially False.  You can end up with an infinite loop if the condition is always True, be careful!

Watch this video for an explanation of how While loops work in Small Basic.

Now try these tasks using While Condition loops.

37) Type out this program and run it.  As the condition is always True, you will be stuck in an infinite loop and will need to click on the ‘X’ in the top right of the Text window to kill the program.  Try changing number = 1 to number = 2 on line 1 and see what happens.

38) Edit the program above so it looks like the one below.  Run it and see what happens.  Does it count to 10?  Are you still stuck in an infinite loop?  Play around with the condition on line 2 so you understand exactly how it controls the While loop.

39) Adapt the program below to ask a different question and require a different answer to stop the While loop.

Look at the operator in the condition answer <> “yes”.

‘<>’ (less than or greater than) basically means ‘not equal to’.

40) Write a program that asks for a number to be entered and then uses a While loop to repeatedly divide that number by 2. The While loop should print the number but stop before the number is less than 1. The output should look similar to the output shown below. (Click for Help)

41) While loops are really useful for data validation or put another way, checking that the user has entered what you asked them to. Take the program you wrote for Task 40 and adapt it so that the user has to enter a number between 10 and 1000 in order to continue to the main program. (Click for Help)

42)  Write a password validation program that checks the user’s input to see if it matches the set password.  Your output should look similar to the output shown below.

43) Create a random Arithmetic Quiz. Your program should output questions at random and output whether the user answers the question correctly or incorrectly. When the user inputs the string “end”, it should exit the loop and then output the users final score and percentage score.

The following line of code will help you generate a random number…

You will need to generate at least two random numbers and store them in logically named variables. If you want to make your questions randomly choose between +, -, / * you will need to generate a third random number. I suggest making all of your questions + to begin with. (Click for Help)

## Subroutines/Functions (***UNDER DEVELOPMENT***)

Very often while writing programs we’ll run into cases where we’ll have to execute the same set of steps, over and over again. In those cases, it probably wouldn’t make sense to rewrite the same statements multiple times. That’s when Subroutines come in handy.

Small Basic Guide