Infinite loop, controlling loops and loops with while statement


Use looping statements often can be a dangerous task.
This is because of a thing called an infinite loop.

It happens unintentionally, but sometimes it happens on purpose ... mainly by bad intentioned people.
In this Java article we study more about this type of loop, how they occur, and how  have more control over the loopings statements.


The infinite looping with while control statement in Java


In our study of the while loop in Java, we saw that it is only executed if the condition that it tests every iteration is true, and that within the code execution generally occurs a change in this condition for a sometime the looping stops.

But what if the looping does not stop? It will run infinitely?
Yes, it will.
You are the programmer. You say, Java does. He will not question you.
And would anyone want a program that did not stop?

And if you want to build a server that will work 24h/day forever (until it stops working)?
Now, will use an endless loop, will always work.
In many programs of scientific research, computers simply do not stop to calculate. You program the tasks, and leave it to do all the work.

Controlling a loop and leave the computer in a loop is a way to have more control over your machine.


How create a simple infinite loop



public class boring {
    public static void main(String[] args) {
        while(true){
            System.out.println("Infinite Loop!");
        }
    }
}


This is pretty simple to understand.
What is the condition tested? 'true'? Yes, it always returns the logical value 'true', then the code is always executed in the while statement.
In other words, you can understand the code like: while 'true' -> run code

Compile, runde the code and take a look.
To stop the looping, click the red button, the universal sign for 'stop', which appears in the picture:



Infinite Loop, in portuguese

What does this while loop is simply to show, infinitely, a message on the screen.
Even something so simple, it's boring. And worst, harmful to your computer.
Although our current machines are very powerful, there is a considerable decrease in RAM and generates a system slowdown.

In older systems, could do some damage.
Loops more 'sophisticated' can easily crash your machine, especially if you use Windows OS.
A simple virus to be created is to create a loop that generates text files, or adding lines to get the string that text file.
Within minutes your hard drive is clogged.


Java code example: Controlling looping  control statements

import java.util.Scanner;

public class Matrix {

    public static void main(String[] args) {
        boolean continue = true;
        char option;
        Scanner input = new Scanner(System.in);
       
        while(continue){
            System.out.println("You are in Java matrix;");
            System.out.print("Type the special character to leave the Java Matrix: ");
            option = input.next().charAt(0);
           
            if(option=='j'){
                continue=false;
                System.out.println("Congratulations, you are out of Java Matrix");
            }
            else{
                System.out.println("You are not allowed to leave the Matrix. Learn Java.");
            };
        }
    }
}



In the simple example of looping, the condition is always true. There is not much to do. It was well defined and will remain so.
In Java, as in other languages, do not use this Boolean operator directly.
Instead, declare a variable of type 'boolean' that will determine whether the loop should continue or not.
And how will this be decided? For you, during program execution.

In the following example, I created a variable 'boolean' called 'continue'. Initially it is 'true', for the control statement while start
.
If I had started it as 'false' the loop would not begin and there would be no program.
Declared a char type 'option' that character will receive a user. It will be like a password. Only will escape the while loop if the user enters the correct password, or character.

Very well, the program starts and tells that in order to you leave the Matrix, is necessary to type a special character.
As a Java programmer you are, you see that the conditional test the password is 'j', of Java.

If you guess a letter, the app will changes the value of 'continue' to false, then the next iteration of the while looping will not run and exits the loop. So, the Java application.

But if the user type wrong pass - which is likely, since there are 26 letters and 10 numbers on the keypad - the variable 'continue' remains true (because nothing has changed).

It is the first program of its 'security' in Java, it requires a password. You can transform into executable or disable for the user does not see this password, so only the programmer knows the password.
You can change the 'j' for the initial of your name.

When studying Strings, we will use words or phrases, in order of caractares. But wait, we will get there. For now testing this program and understand how it works, we'll use this a lot to control the flux of our Java applications.



No comments: