Hey folks.
So I'm just messing around with the Language tutorial in BGT.
And My question is:
What is better.
While Loops, Do While Loops, or For Loops?
Do you use all three?
If so, why?
I find that that it's not the While Loops that give me trouble, it's the Do While Loops. And yes I have read the tutorial.
I am making a silly project that doesn't really mean anything. It's just for practice.
I'm calling it the BGT Sleep Test since I am programming it in BGT.
Basically here is how it goes.
It uses all sound files.
When the program launches, it plays the intro, Then the music continues looping and it loops, it plays a prompt telling you what to do.
And a timer begins going up to 15 minutes.
So this music is looping and if the timer reaches 5 minutes, you'll here me take a bite of something to eat.
If ten minutes elapsed, then I take a drink of water.
Then if 15 minutes elapsed, I snore.
Then the Music stops and my headphones hit the keyboard.
And then the alert box comes up and explains what happen.
Then you hit OK and the program exits.
I know this is silly, but I'm just practicing and trying things out.

Thumbs up

All 3. If you can know how long a loop will last, or if you're performing an action on every item in an array, use a for loop. Otherwise, use a while loop. Do-while isn't needed that often. The example you described doesn't sound like it'd have as much use for for loops, but would use at least one while loop. Most BGT games will use a while loop to keep the game running until it is closed. You'd use for loops if, for instance, you had an arbitrary number of enemies or items you needed to update at once.

Thumbs up

I forgot the do while loop even existed. While loop is fine for every purpose I've used it for, I've never needed the do while loop. The do while loop is just a while loop, but it makes sure the code runs at least once which is something you can do anyway if you code it right.

Thumbs up

Hi,
basic difference between while loop and do while loop is, that while while loop first checks if it should to run, do while loop first run and after it checks, if it should to run again. You will not use probably this very often, but there are situations where it is useful, for example I used it to generate prime numbers for RSA crypting algorithm, there was possibility that given number is prime already, so while loop not changed it while do while yes, because it ran generation first and after it checked, if result is prime or not.

Hope it helps you, difference between while and for cycle Cae_Jones explayned clearly I think.

Best regards

Rastislav

Thumbs up