# 3 [Note] do ~ while statement

Introduction

Today's Tokyo was cloudy, so it was cooler than usual during the day (I think it was 32 degrees Celsius, but ...) Today I'm going to write about a do ~ while statement that I'm not used to yet!

Development environment

This time I also tried it in a comprehensive development environment with "Eclipse". Please refer to this article to build a development environment.

Creating a Java development environment with Eclipse (Mac version)

Implementation


class Main {
   public static void main(String[] args) {
		int number = 1;
        do {
            number ++;
            System.out.println("number = " + number);
        } while (number < 5);
 
        number = 4;
        do {
            number ++;
            System.out.println("number = " + number);
        } while (number < 5);
 
        number = 5;
        do {
            number ++;
            System.out.println("number = " + number);
        } while (number < 5);
    }
}

I will explain the above code in order from the top!

Define the main method in the Main class.

class Main {
  public static void main(String[] args) {

Define the variable number and substitute 1 as the initial value.

int number = 1;

Add 1 (++) to the number until number becomes 5 (number <5), route and output. I want the output to be number = number, so it looks like this:


do {
            number ++;
            System.out.println("number = " + number);
        } while (number < 5);

Assign 4 to the variable number.

number = 4;

Add 1 (++) to the number until number becomes 5 again (number <5), route and output.


do {
            number ++;
            System.out.println("number = " + number);
        } while (number < 5);

Assign 5 to the variable number.

number = 5;

The last do ~ while statement also adds 1 (++) to the number until number becomes 5 (number <5), routes it, and outputs it.


do {
            number ++;
            System.out.println("number = " + number);
        } while (number < 5);

Don't forget the closing brackets for the Main class and main methods! ]

 }
}

Run

Execution result


number = 2
number = 3
number = 4
number = 5
number = 5
number = 6

If it is displayed, it is successful!

If you get an error

There is a possibility that ; is missing, so please check it!

Finally

In the last article of this month, I wrote about the do ~ while statement that I had left learning ... sweat As for the recent status report, recently the company has been creating a website with consideration for monetization. I'm not good at designing myself, and I'm just about to implement it while asking myself "Is this okay?" This time I've been touching it for the first time because I wanted to use WordPress, but it's getting fun little by little. I want to make it fun as it is ~ lol I would like to update the article in September! Then! !! !!

Recommended Posts

# 3 [Note] do ~ while statement
[Java] for statement, while statement
12 Corresponds to the while statement
Note
Note
Java learning memo (while statement, do-while statement)
Java, for statement / while statement starting from beginner
Do you use the for statement after all? Do you use a while statement? Proper use of for statement and while statement