Java, for statement / while statement starting from beginner

Introduction

This article is a memorandum. Although it is a reference book level content, the code posted in this article is about ** Wrong ** are the main things. This is for the purpose of posting the part that was actually mistaken during coding and posting it for self-reflection. In addition, I will not touch on the deep part here because I will review it later while also studying the Java Silver exam questions.

environment

Language: Java11, JDK13.0.2 Operating environment: Windows 10

for statement

The ** for statement ** is one of the iterative processing statements for ** iterative processing ** until the loop is exited, based on the ** conditional statement **. It is suitable for repeating by specifying the number of times.

Basic form of for statement


for (Initialization statement; Conditional statement; Update statement){
     //Iterative processing;
}

Click here for each role.

Since the for statement can be described in a nested manner, more complicated repetition can be performed, but this time it is omitted. ## for sentence practice

amount10Years.java



double amount = 100000;

for (int year = 1; year < 10; year++){
  amount *= 1.02;
}
System.out.println("10 years later" + amount + "It becomes a circle.");

//Something is missing ...

In this case, the first year is the second year, and only nine compound interest calculations can be made. If you make a mistake in setting the initial value, it will affect the number of times.

while statement

Along with the for statement, it is a statement that repeats processing. It is suitable for repeating until the expected condition is reached, but be careful because it is easier to create ** infinite loop ** than for statement.

Basic form of while statement


while (Conditional statement){
     //Iterative processing;
}

For the conditional statement, see the one in the explanation of the for statement.

As another form, ** do ~ while statement ** also exists. Since the process is repeated at least once, it is recommended to use it when the essential process is included. Pay close attention to the position of the semicolon ;.

do~Basic form of while statement


do{
 //Iterative processing;
} while (Conditional statement);

while statement practice

whileAmount150k


double amount = 100000;
int year = 0;

//This time I properly set the first year to 0

while (amount <= 150000) {
    amount *= 1.02;
}
System.out.println(year + "It will exceed 150,000 yen over the years.");

//The target amount was reached in less than a year ...

This time, I didn't include year ++ in the processing of the while statement, so the final output became strange.

At the end

It's a very useful sentence, but it also carries the risk of ridiculous results if left misunderstood. I want to understand it well because it is used in areas such as interest rate calculation and algorithms that can never be mistaken. For this reference, I put a Java qualification test book, but it doesn't have {} (curly braces), the nesting structure is deep and it takes time to understand, increment decrement Is spread in large quantities. There are good problems that require both comprehension and concentration.

reference

I write variables and expressions as much as possible and compile them, so if I want to quote them completely, I will describe that.

Easy Java 7th Edition

Java SE11 Silver Problem Collection (commonly known as Kuromoto)

Recommended Posts

Java, for statement / while statement starting from beginner
[Java] for statement, while statement
Java, if statement / switch statement starting from beginner
Java starting from beginner, override
Java starting from beginner, inheritance
Java overload constructor starting from beginner
Java for statement
Java starting from beginner, variables and types
Java starting from beginner, nested / break / continue
[Java] for statement / extended for statement
(Memo) Java for statement
Java starting from beginner, class declaration / object generation
For Java engineers starting Kotlin from now on
[Java] Basic statement for beginners
Java life starting from scratch
[Beginner] Java variable / logical operator / extended for statement [Note 22]
[Java] Platforms to choose from for Java development starting now (2020)
Java while and for statements
Java, abstract classes starting from beginners
Java learning memo (while statement, do-while statement)
Java, interface to start from beginner
java beginner 4
java beginner 3
java beginner
Java starting from beginners, logical operators / conditional operators
Getting Started with Java Starting from 0 Part 1
Memo for migration from java to kotlin
[Beginner] A story about starting studying Java for job hunting ~ 2nd month ~
[Beginner] A story about starting studying Java for job hunting ~ 3rd month ~
[Beginner] A story about starting studying Java for job hunting ~ 1st month ~
[Beginner] A story about starting studying Java for job hunting ~ 5th month ~
[Beginner] A story about starting studying Java for job hunting ~ 4th month ~
IntelliJ starting from 1
For JAVA learning (2018-03-16-01)
Use Java7 try-with-resources statement for Cursor close processing
2017 IDE for Java
Java switch statement
Java Exercise "Beginner"
[Java basics] Let's make a triangle with a for statement
Java development for beginners to start from 1-Vol.1-eclipse setup
Java (while statement) basic problem that beginners did first
I tried using an extended for statement in Java
How to loop Java Map (for Each / extended for statement)
Call Java from JRuby
Complex for statement (basic)
Changes from Java 8 to Java 11
Sum from Java_1 to 100
[Java] Package for management
Eval Java source from Java
Countermeasures for Java OutOfMemoryError
Access API.AI from Java
NLP for Java (NLP4J) (2)
From Java to Ruby !!
NLP for Java (NLP4J) (1)
# 3 [Note] do ~ while statement
Store in Java 2D map and turn with for statement
[Java] Make variables in extended for statement and for Each statement immutable
[Java] Get Map key / value pairs using extended for statement
Java review ③ (control syntax [if / switch / while / for], conditional expression)