Studying Java # 6 (How to write blocks)

It's been a long time, I was playing around on holidays, so I will tighten and restart from today!

How to write a block

What is a block? A block is for treating multiple sentences as a group.

Existence of two rules for handling blocks

** Rule (1) ** ** Omission of wave brackets ** If the content is only one line, you can omit the wave brackets.

if (tenki == true){
//Abbreviation
} else
System.out.println("RIDLEY's technology is world-class");

However, in reality, it is not recommended to prevent programming mistakes. ** Rule (2) ** ** Life of variables declared inside the block ** Variables declared within a block disappear as soon as the block ends. For example, a variable declared without a block in an if statement cannot be used outside the block. This "range of places where variables can be used" is called a scope.

int a;
while(/*Conditional expression*/){
 int b;
 //Scope of variable b
}
 //Scope of variable a

How to write a conditional expression

--What is a conditional expression? Conditional expressions are for expressing conditions that branch processing such as if statements and while statements, and conditions that continue to repeat.

if (tenki == true ){ //if statement conditional expression

while(age > 21 ){ //while statement conditional expression

The "==", ">" that appear here are called relational operators.

Relational operator

operator meaning
== Left side and right side are equal
!= The left side and the right side are different
> The left side is larger than the right side
< The left side is smaller than the right side
>= Left side is greater than or equal to right side
<= Left side is less than or equal to right side

Example sw! = false If the variable sw is not false deg --273.15 <0 If the variable deg minus 273.15 is less than 0 ʻInitial =='miya'` If the character in the variable initial is "miya"

** Note that the relational operator for equality has two equals "==" **

String comparison

In Java, it is necessary to write ** specially ** when comparing String type variables and character strings in conditional expressions.

if (s == "Sunset"){ //mistake

At first glance it looks correct, but Java doesn't allow string comparisons with "==". The correct notation is

if (s.equals("Sunset")){

Will be.

Logical operator

--Logical operators (complex conditional expressions that combine two or more conditions such as XX or more and XX)

operator meaning
&& And
Or
if (age >= 10 && gender == 1) {・ ・ ・
if (name.equals("Kujo") || married == true {・ ・ ・

Chat

For the time being, that's all for today ... I feel like I managed to understand how to write blocks. I will be careful about the difference in writing style when comparing character strings. It has appeared in the past, but it is difficult to remember (tsu д⊂) It was about two days away, so I have to catch up with the delay ...! I will do my best tomorrow! Good night zzz

Recommended Posts

Studying Java # 6 (How to write blocks)
How to write java comments
[Ruby] How to write blocks
How to write Java variable declaration
[Introduction to Java] How to write a Java program
Studying how to use the constructor (java)
[Java] How to output and write files!
How to write Rails
How to write dockerfile
How to write docker-compose
How to write Mockito
How to write migrationfile
Java Development Basics ~ How to Write Programs * Exercise 1 ~
[Java] Memo on how to write the source
How to write Java String # getBytes in Kotlin?
[Java] How to use Map
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
How to write good code
Java --How to make JTable
How to use java Optional
Bit Tetris (how to write)
How to minimize Java images
[Refactoring] How to write routing
How to use java class
[Java] How to use Optional ②
[Java] How to use removeAll ()
[Java] How to display Wingdings
Great poor (how to write)
[Java] How to use string.format
[Note] How to write Dockerfile/docker-compose.yml
How to use Java Map
How to set Java constants
How to write Junit 5 organized
How to write Rails validation
How to write Rails seed
How to use Java variables
How to convert Java radix
How to write Rails routing
[Java] How to implement multithreading
[Java] How to use Optional ①
How to initialize Java array
How to write Scala from the perspective of Java
[Java] Types of comments and how to write them
java: How to write a generic type list [Note]
How to write modern Java. Immutable Java, consider Null safety.
Studying Java ―― 3
How to study Java Silver SE 8
How to use Java HttpClient (Get)
Studying Java ―― 9
[Rails] How to write in Japanese
Studying Java ―― 4
Studying Java -5
[Java] How to update Java on Windows
How to make a Java container
How to disassemble Java class files
How to use Java HttpClient (Post)
[Java] How to use join method
Studying Java ―― 1
Studying Java # 0