It's been a long time, I was playing around on holidays, so I will tighten and restart from today!
What is a block? A block is for treating multiple sentences as a group.
** 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
--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.
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 "==" **
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 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 {・ ・ ・
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