[JAVA] Isn't the While statement evil?

What about a While statement?

Perhaps almost every language in programming has a while statement, and anyone who has touched a program will have heard or seen it.

Let's check what the while statement is.

while (condition) { processing }

It looks like this

The point to note here is that the while statement continues the loop while the condition is true. In other words, when it becomes false, it escapes from the loop.

Is that so? Is that a while statement? You would only think. Let's take a look at some programs from this.

Let's start with a simple program

Code1.java


public class Code1{
    public static void main(String[]args){
      int num = 0;

      while(num++ < 5){
        System.out.println(num);
        
      }  
 
    }
}

*** It's a for statement, isn't it? ?? ?? ?? *** ***

Yes, such a program can be a for statement.

Then there is another place to play an active part

Let's take a look at this program.

Code2.java


import java.util.Scanner;
public class Code2{
    public static void main(String[]args){
      Scanner sc = new Scanner(System.in);
      int num = sc.nextInt();
      while(num != 0){
        System.out.println("Loop until you press 0");
        num = sc.nextInt();
      }
      System.out.println("End");
    }
}

This is not beautiful because the first process is written outside, so let's rewrite it as do-while.

Code3.java


import java.util.Scanner;
public class Code3{
    public static void main(String[]args){
      Scanner sc = new Scanner(System.in);
      int num;
      do{
        System.out.println("Loop until you press 0");
        num = sc.nextInt();
      }while(num != 0);
      System.out.println("End");
    }
}

This is better than before, but the do-while statement tends to make a lot of mistakes, such as the need for [;] after the while.

And, in order to realize the condition that it ends when num becomes 0, the while statement loops while the condition is true, so when it becomes ~ ~, it is incompatible with the end, and in this case num! = It must be a condition that denies the condition that you want to end in this case, such as 0 or! (Num == 0).

*** The extreme of overwhelming complexity! !! !! !! !! !! !! !! !! !! *** ***

Then what should I do? ** "break in infinite loop with if statement" ** Let's rewrite it by "breaking in an infinite loop with an if statement".

Code4.java


import java.util.Scanner;
public class Code4{
    public static void main(String[]args){
      Scanner sc = new Scanner(System.in);
      int num;
      while(true){
        System.out.println("Loop until you press 0");
        num = sc.nextInt();

        if(num == 0) break;
      }
      System.out.println("End");
    }
}

Isn't it easier to understand the conditions for escaping from the loop visually than before? Let's look at the case where the loop condition is simply complicated, instead of the pattern like this do-while.

Code5.java


import java.util.Scanner;
public class Code5{
    public static void main(String[]args){
      Scanner sc = new Scanner(System.in);
      int a = 0,b = 0,c = 0,count = 0;

      System.out.print("Please enter an integer:");
      a = sc.nextInt();
      System.out.print("Please enter an integer:");
      b = sc.nextInt();
      System.out.print("Please enter an integer:");
      c = sc.nextInt();
      while(a*b>c*c){
        // (a*b is c*Ends when (greater than c) becomes false
        System.out.print("Please enter an integer:");
        a = sc.nextInt();
        System.out.print("Please enter an integer:");
        b = sc.nextInt();
        System.out.print("Please enter an integer:");
        System.out.println("==========================");
        c = sc.nextInt();
        count ++;
      }
      System.out.printf("Until the end%d times",count);
    }
}

This is a program that inputs three integers and displays how many times a condition is less than b or c is less than a or a is less than 0, looping for true and looping out of the loop.

Code6.java


import java.util.Scanner;
public class Code6{
    public static void main(String[]args){
      Scanner sc = new Scanner(System.in);
      int a = 0,b = 0,c = 0,count = 0;

      while(true){
        System.out.print("Please enter an integer:");
        a = sc.nextInt();
        System.out.print("Please enter an integer:");
        b = sc.nextInt();
        System.out.print("Please enter an integer:");
        System.out.println("==========================");
        c = sc.nextInt();
        count ++;
        //a*b is c*Ends below c
        if(a*b <= c*c) break;
      }
      System.out.printf("Until the end%d times",count);
    }
}

When there are multiple conditions like these two programs and it becomes a complicated condition, it escapes from the loop when all of them are not satisfied. When looping under such complicated conditions, it will be more readable to end when Code6 is true than to end the loop if Code5 is false.

Summary

The more complicated the loop condition of the while statement becomes, the less readable the while statement of the loop escape becomes. If it is a break in an if statement in an infinite loop, it can be terminated when it becomes true, so it can be said that it is excellent in readability. Furthermore, it can replace the while statement or the do-while statement at the position where the if statement is placed. Furthermore, if you put it in the middle, the operation of ~ ~ is guaranteed once (do-while), and depending on the conditions, even a little tricky process such as never performing it can be easily implemented.

Conclusion

*** Break an infinite loop with an if statement This! *** ***

Recommended Posts

Isn't the While statement evil?
12 Corresponds to the while statement
[Java] for statement, while statement
# 3 [Note] do ~ while statement
Let's understand the if statement!
Let's understand the guard statement!
Let's understand the for-in statement!
Is the ternary operator evil?
Let's understand the switch statement!
Java learning memo (while statement, do-while statement)
[Ruby basics] About the role of true and break in the while statement
[Java] Nowadays, the extended for statement is not exclusively for List, isn't it?