What do we use in code to allow us to repeat tasks? For instance, let's go back to our multilinguage "Hello World" method, which is now patched up to look like this ...
Now, say we have a pressing business need to print out "Hello world" 10 times. I guess we could do the following function using "the power of cut and paste"...
It's kind of messy. And what if someone changed requirements on us, and it's now got to be 20 times ... and in French. That's a lot to change. [Hint - in code we want to make software where we can make such modifications with as few changes as possible]
The solution to this is today's topic - the use of loops. Loops allow us to repeat an action a number of times until an exit condition is reached - we can even have it so the action modifies according to the number of loops.
The for loop
The for loop is by far the most commonly used in Java, it has the following syntax ....
for ( initialise ; condition ; action2 )
{
action1;
}
- initialise - this is an action undertaken when you first enter the loop. It is not repeated on subsequent loops. Typically you have something like "int counter = 0" where you both declare and initialise a counter variable.
- action1 - this can actually be a whole series of statements, which is performed each time a loop occurs
- condition - this reduces to a boolean true or false statement. As long as it's true, the loop will keep performing action1 and action2. For instance "counter < 10" will keep looping until counter variable has exceeded 10. You can chain together complicated statements such as "(counter < 10) && (valX > 0)" which will repeat as long as counter is below 10 and valX is greater than 0.
- action2 - this is an action which is performed at the end of every loop. In truth you could probably put this at the end of action1 if needed. However typically it's to do with changing something to do with the condition under evaluation, and making it clear and visible. An example would be "counter++" which increases the value of counter - something which is often called "stepping".
Here's the code we use to create a for loop for our problem. You can find it here,
The while loop
The while loop has the following syntax,
while (condition)
{
action1;
}
Basically it keeps performing action1, as long as condition is true. It may loop simpler syntactically than for-loop, but generally for-loops are the preference - mainly because you can tie up the initialisation and stepping etc inside the for-declaration.
To create a while loop, it looks like this ...
Notice how here we have to have have any initialisation before our loop statement and our stepping inside the loop.
The do ... while loop
This is very similar to the while loop above
do
{
action1;
} while (condition)
The only major difference is the condition is checked at the end, not start of the loop.
Here's our example,
So what's the difference? A do ... while-loop, because it's evaluated at the end, will always run action1 at least once. If the condition is false from the start with a while-loop, you won't run action1 even once.
Here's our example ... out while-loop here ...
Doesn't print anything out even once,
Meanwhile our do....while-loop ...
Does produce a single print out ...
The break command
Sometimes you just want to break our of your current loop. You want to break free ...
The break command gives you that ability - although really it should be used sparingly. Much better to use the relevant condition defined in the for/while to stop the looping. It's a bit of an ugly way to stop the carousel.
But here's an example ...
This will only print out Hello World 3 times ... then break out of the loop
Loops ... inside a loop
Yes - it's even possible to create a loop inside another loop. An example is here, where we're trying to print Hello World in multiple languages, multiple times ...
The output to which is here,
Notice I put a break command in there which limits it to printing out 3 times in any one language. This is to show you in loops inside loops, your break only gets you out of your innermost loop! It doesn't affect the outermost loop.
Forever loops
Going back to the George Orwell 1984 theme ...
"If you want a vision of the future,
imagine a boot stamping on a human face - forever"
If you have a condition inside your loop which can never be met, then you're trapped inside what's known as a forever loop. Such loops are very, very bad!
I have an example in Github here. Here's my method to print ...
Here's my loop, which will never be satisfied ...
Hit run - and this will run ... and run. It can never escape - you have to ...
Because I'm a tester, I should point out you have an off by one error. You state your loop breaks after printing "Hello World!" 3 times, but it actually prints it out 4 times.
ReplyDeleteAnd that is why testers need testers. 8-)
DeleteI simply want to say I’m very new to blogs and actually loved you’re blog site. Almost certainly I’m going to bookmark your blog post . You absolutely come with great well written articles. Thanks a lot for sharing your blog.
ReplyDeletejava training in chennai | chennai's no.1 java training in chennai | best java institute in chennai
Asian Popular Sports Betting
ReplyDeleteviva3388 Online casino investment services. Betting Online Gambling Lots of online gambling can help you make the most of online gambling and online gambling online slots. To make you a gambler, all online gambler betting.
In addition, the gambler who used to play online gambling with us, the gambler can make a bet on it. Just bet the player to register with us. Without downloading the application, you can join the online investment with us unknowingly. Just have the internet, you can invest anytime, anywhere. Gambling games are very valuable. We also have jackpots that will give you a lot of money if you are interested in becoming part of our online betting site. And today, the player who is investing in the service of betting on the web, you will receive a lot of interest that you invest here. viva9988