[Note] What I learned in half a year from inexperienced (Java) (3)

Themes to be taken up this time

Last time, I wrote what I learned from conditional branching, if statements, and switch statements. I'm glad to hear your comments.

It's been less than a year since I joined the company, but I think this is also an experience. The themes we will discuss this time are *** repetitive statements and repetitive control statements ***.

What is a repetitive sentence?

*** Iterative statement *** is a statement that iterates. There are two types of repetitive statements. ・ While statement ・ For statement

while statement

Let's see what a while statement is in a java program. Format while (conditional expression) { processing; }

test1.java


class test1{
  public static void main(String[] args){
    int a=0;  //I assigned 0 to the int type variable a
    while(a<10){ //Repeats the processing of the while statement block while the value of variable a is less than 10.
     System.out.println(a); //Outputs the value of variable a
     a++;  //a++Is a= a+Same as 1
    }
 }
}

See the comments after // what they are doing. The () after while is a conditional expression. While this conditional expression determines True Process in the block of while statement. In this case, the value of the variable a is output and 1 is added to that value.

do-while statement

Let's see what a do-while statement is in a java program. Format do { processing; } while (conditional expression)

test2.java


class test2{
  public static void main(String[] args){
    int a = 0; //I assigned 0 to the int type variable a.
    do{
     System.out.println(a); //Outputs the value of variable a
     a++; //Increase the value of variable a by one
    }while(a<10)//Determine if the value of variable a is less than 10
  }
}

What is the difference between the while statement and the do-while statement this time? The difference is that the (conditional expression) comes last. If the conditional expression is behind, Process the block of the do-while statement first. After that, the conditional expression is judged. *** I want to process it once! *** Use this do-while statement when you want to write such a repeating statement.

for statement

The for statement is a little confusing with the while statement and do-while statement. Format for (Equation 1; Equation 2; Equation 3) { processing; }

test3.java


class test3{
  public static void main(String[] args){
    int a = 0;  //Assign 0 to the int type variable a.
     for(int i = 0; i<10; i++){
     //The variable i of type int is 0. While the variable i is less than 10
   //Process the block of the for statement.
      System.out.println(a);
      a++;
    }
 }
}

Equation 1 is int i = 0, Equation 2 is i <10, and Equation 3 is i ++. Expression 1 is called an "initialization expression" in books and sites, and expresses how many times a block of for statements is repeated. Contains the formula. Equation 2 sets the upper limit of Equation 1. And Equation 3 processes the block After that, I am changing the value of the variable defined in Equation 1.

Summary

Last but not least, the iterative statement is used when you want to write a statement that repeats processing. I often hear the terms "loop processing" and "infinite loop", but I think that "loop processing" means repeating processing, and "infinite loop" means continuing the same processing all the time. You can use while to create an infinite loop. (You can also use a for sentence ...)

I still have to study, but I will continue to write articles. The most important thing to say in this series is that you can talk about the program so far *** even if you are inexperienced and continue whatever happens.

Let's do our best and continue! !!

Next time, I would like to talk about repetitive control that I did not use this time, and python that I am doing by myself.

Thank you for reading this far.

Recommended Posts

[Note] What I learned in half a year from inexperienced (Java)
[Note] What I learned in half a year from inexperienced (Java) (1)
[Note] What I learned in half a year from inexperienced (Java) (3)
What I learned when building a server in Java
What I learned from Java monetary calculation
What I learned in Java (Part 2) What are variables?
What I learned in Java (Part 3) Instruction execution statement
What an inexperienced engineer who took a leave of absence from college learned in 2020
What I learned in Java (Part 4) Conditional branching and repetition
What I pointed out as a reviewer for half a year
What I learned from studying Rails
I created a PDF in Java.
What I learned with Java Gold
What I learned with Java Silver
What I have learned in Java (Part 1) Java development flow and overview
What I learned from doing Java work with Visual Studio Code
What is a class in Java language (3 /?)
What I investigated in Wagby development Note 1
What is a class in Java language (1 /?)
What is a class in Java language (2 /?)
A note of what I stumbled upon and noticed in catching up with Laravel from Rails
[Rails] What I learned from a little stumbling block when using ancestry
[JAVA] Project Euler, I got stuck in Q8, so make a note
What I don't like when using interface of a function with default arguments in Kotlin from Java
What i learned
I made a primality test program in Java
GetInstance () from a @Singleton class in Groovy from Java
I tried hitting a Java method from ABCL
A note when you want Tuple in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
I wrote a primality test program in Java
A quick review of Java learned in class
I wrote a prime factorization program in Java
Summary of what I learned in Spring Batch
I made a simple calculation problem game in Java
A quick review of Java learned in class part4
# 2 [Note] I tried to calculate multiplication tables in Java.
I tried to create a Clova skill in Java
What I learned ② ~ Mock ~
A note for Initializing Fields in the Java tutorial
I tried to make a login function in Java
What I learned ① ~ DJUnit ~
[Note] Create a java environment from scratch with docker
A quick review of Java learned in class part3
I made a Wrapper that calls KNP from Java
[Rilas] What I learned in implementing the pagination function.
A quick review of Java learned in class part2
Call a program written in Swift from Processing (Java)
I tried to find out what changed in Java 9
How a liberal arts engineer passed Java Silver in half a year after joining the company
I tried to make an application in 3 months from inexperienced
[Beginner] I made a program to sell cakes in Java
I just wanted to make a Reactive Property in Java
How to store a string from ArrayList to String in Java (Personal)
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
Easily get an integer from a system property in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
I tried to make a client of RESAS-API in Java
Get a non-empty collection from an Optional stream in java
What I researched about Java 8