In this part of our online Java Course we will see the break and continue commands, Java, which serve to provide us with greater control over the flow of applications, especially the iterations of loops.
Generally, in our programs in Java, we will use while, for, and the do while loops to search for any item, number or check some condition.
For example, imagine that you were hired to do a program, in Java of course, for a bank.
In the time of application, the customer enters his account number and the program will fetch that number in the database system, which has millions of registered customers.
It will do this through a looping.
But imagine that the program find the customer data at the beginning, in the first few positions.
And then everything else will check database?
Of course not, that would be a waste of time.
That's where the break and continue commands. This would be a good example where we would give a 'break' in the loop, because I find what we wanted.
The break statement
Break means just stop. And that's what the comman do.
When Java encounters this command ahead, it stops the loop / control structure, like while, for, do ... while and switch (which we will see in the next article in the course).
Let us show an example of using the break through a mathematical example.
Example of use:
Suppose you are a scientist and want to know if the numbers between 1 and a million is a number that is a multiple of 17 and 19, at the same time.That is, we want to know if there is a number between one and one million which leaves 0 rest on division by 17 and 19.
If there is, then print it. And print only the smallest number.
We could do a loop from 1 to 1 million, and test it through a go.
Ok, it is a solution. The main working of a programmer lfe is solve the problem.
We will use a method called 'currentTimeMillis ()', which returns a type 'long' with the current system time in milli seconds. Let us declare such at the beginning and end of the loop, and then subtract these values, so we will have time to implement the program.
After printing the lowest number, boolean becomes false, so only the first number is printed. See:
public class breakTest {
public static void main(String[] args) {
long i = System.currentTimeMillis();
boolean print = true;
for(int count=1 ; count <=1000000 ; count++){
if((count % 17 == 0) && (count % 19 == 0))
if(print){
System.out.println(count);
imprimir=false;
}
}
System.out.println("Execution time, in milliseconds: "+ (System.currentTimeMillis() -i));
}
}
On my machine gave 26 milli seconds result.
But if you want to be a good professional, not just only know how to solve. Have to solve and in the best possible way.
Note that finding the smallest number is 323, and the loop goes up to 1 million! That is, it runs from 324 to 1 million for nothing because you already have the number you want!
Now if he liked the number, 323, we will make the loop stop using the break command:
public class break {
public static void main(String[] args) {
long i = System.currentTimeMillis();
for(int count=1 ; count <=1000000 ; count++){
if((count % 17 == 0) && (count % 19 == 0)){
System.out.println(count);
break;
}
}
System.out.println("Execution time, in milliseconds: "+ (System.currentTimeMillis() -i));
}
}
8 milli seconds!
Less than one third of the last time!
You may think "Oh, of 26 milli milli seconds to 8 seconds difference is insignificant."
I agree with you.
But in the future you will make applications more complex that will take longer, and these small details will make the difference.
For example, there are methods that during a game that are easily called million times.
Multiply that difference 'insignificant' a few milli seconds for million times and have a hell of a 'lag', or slowness. This is called optimization: making the most efficient manner possible.
We will rather focus on the optimizations in our Java course, as you'll see in the following example.
The command continue
As the name says, it 'continues' the loop. The break statement stops the loop, since the continue only stops the current iteration.
But not enough to be a good professional. You are in Progressive Java course, you have to be one of the best!
We will optimize the code passed.
Note one thing, we want to find a number that is a multiple of 17 and 19. Such a number can not be even, because 17 and 19 are odd.
For each number 'count', we are doing two tests: one is multiple of 17 and is a multiple of 19.
We will optimize the following way, let's do one test: let's see if it is a multiple of 2. If it is, test whether advance or a multiple of 17 and 19 can skip this iteration.
And how it will skip an iteration? With the command continues!
See how it looks:
public class continueTest {
public static void main(String[] args) {
long i = System.currentTimeMillis();
for(int count=1 ; count <=1000000 ; count++){
if(count % 2 == 0){
continue;
}
if((count % 17 == 0) && (count % 19 == 0)){
System.out.println(count);
break;
}
}
System.out.println("Execution time, in milliseconds: "+ (System.currentTimeMillis() -i));
}
}
And we have the amazing time of 1 milli second!
Of 26 milli seconds went to 1 milli second! Impressive that Java does, not?
No comments:
Post a Comment