DarkBASIC Programming/Loops

From Wikibooks, open books for an open world
Jump to: navigation, search

Day 10[edit]

Trust me loops are useful, they do tedious tasks and they're easy to work with.

Take for example a lottery program

Dim lottery(100)
lottery(1) = 1
lottery(2) = 2
lottery(3) = 3
lottery(4) = 4
etc...
Print Lottery(1)
Print lottery(2)
Print Lottery(3)
etc...

The above way is for squares, and people without loop knowledge.

Done with a loop

`Let's image the dim command above is down here
for a = 1 to 100
     Print lottery(a)
next a

Note from reader: You can't print "lottery(a)" as it does not exist, instead write this:

dim lottery(100) for a = 1 to 100 print a next a wait key


Using loops remember to indent it makes the program more human readable for others. With loops we can finally have more advanced input, input without variables, well some variables. Let's look at some loops then.

Repeat/Until

Repeat
   `what the loop does here
until conditionmet

While/Endwhile

While conditionmet
   `What the loop does
endwhile

Do/Loop

do
    `task here
loop

Remember the only way to exit a loop is by quitting which is a no|no, the exit command which is the best way, or a gosub command. In some loops, the loop ends once a condition is met, and in others after a task is done it exits. If you paid attention you should know what I'm talking about. Day's over go eat, sleep, take a walk, whatever come back tomorrow!

Day 11[edit]

This is the advanced input section, if we want a system key we can easily use commands like spacekey(), etc. Go have a look at them, they are the easiest to understand, find out how to disable them from being able to be pressed in DarkBASIC also while you are there. For other keys there's two ways basically scancode(value) which is different per country's keyboard, a Spanish keyboard is different then a British one, and you have to find a value! Or you can use ASCII values, which has a humorous pronunciation(it's pronounced ass key :P). Find out what ASCII stands for at Wikipedia, just type it in the search box and there you go. You can find Wikipedia atCLICK HERE TO GO TO WIKIPEDIA in case your just curious because I forget I believe it's American Standard Character Something Something. We will be using the ASCII because it's the same for every computer and no need to use numeric values.

Example:

`Loops are needed for input BTW
do
   if inkey$() = "charhere" then exit
loop

Here's a key example

do
  if inkey$() = "a" then print "Lowercase A"
  if inkey$() = "A" then Print "Uppercase A"
loop

Remember A is different from a when using inkey$(). Now the lesson's over with, for an assignment find out what ASCII stands for and experiment with the inkey$().