Studying Java ―― 7

if statement

It is used when there is a process that you want to execute only when the conditions are met.

if (Conditional expression){
What to do when the conditions are met
} else {
What to do when the conditions are not met
}

The basic shape is probably like ↑.

If you don't have to do anything when the conditions are not met The "else" part can be omitted ↓.

if (Conditional expression){
What to do when the conditions are met
} 

On the contrary, if you want to divide the process according to multiple conditions, use "else if" ↓.

if(Conditional expression 1){
What to do when condition 1 is met
} else if (Conditional expression 2) {
What to do when condition 2 is met
} else if (Conditional expression 3) {
What to do when condition 3 is met

<Abbreviation>
		
} else {
What to do when all conditions are not met
}

Is this the place to remember as a shape?

Be careful ** Do not put a conditional expression after "else" ** If you want to add a condition, use "else if". I wonder if that mean.

Operators used in conditional expressions

The one used in the conditional expressions of "if" and "else if".

・ ">" ⇒ When the value on the left side is larger than the value on the right side ・ "> =" ⇒ When the value on the left side is the same as or larger than the value on the right side ・ "<" ⇒ When the value on the right side is larger than the value on the left side ・ "<=" ⇒ When the value on the right side is the same as or larger than the value on the left side ・ "==" ⇒ When the left and right values are the same ・ "! =" ⇒ When the left and right values are not equal

Well, it feels general.

It is necessary to write "=" in a row with two "==" It seems that you should be careful about the order of "> =", "<=", and "! =".

For example "When the variable ** a ** is greater than or equal to ** 10 **, add ** 2 ** to the variable ** b **. When the variable ** a ** is greater than or equal to ** 5 ** and less than 10, add ** 1 ** to the variable ** b **. At other times, do nothing. 』
When you want to make something like

if ( a >= 10 ){
	b = b + 2;
} else if ( a >= 5 ) {
	b = b + 1;
}

Is it such a place?

・ If is judged in order from the top. -Execute only the process that first became positive.

So, when judging a number of 10 or more, In the above case, only the "if" that is positive first is executed.

Set multiple conditions in if

You can combine multiple conditions using logical operators.

・ "&&" ⇒ and (so-called and) ・ "||⇒ or (so-called or)

"When variable a is 5 or more and variable b is 3 or less" If you want to do it ↓

if ( a >= 5 && b <= 3 ){
	//Write the processing content
}

This summarizes the two conditions. Of course, two or more conditions may be set.

I don't know what to write Conditions with "()" can also be used.

"When the variable ** b ** is ** 3 ** or ** 2 ** and the variable ** a ** is ** 5 **"

if ( a == 5 && (b == 3 || b == 2) ){
	//Write the processing content
}

It's hard to explain, but it's like that.

When putting negation in a conditional expression

Therefore, the conditional expression can be ** negated **.

・ "!" ⇒ When it is not 〇〇 (so-called not)

if ( !(a >= 5 && b <= 3) ){
	//Write the processing content
}

Enclose the part you want to deny with "()" and add "!" In front of it.

in this case "When other than (variable a is 5 or more and variable b is 3 or less)" It turns out that.

If is often remembered. .. ..

Recommended Posts

Studying Java ―― 3
Studying Java ―― 9
Studying Java ―― 4
Studying Java -5
Studying Java ―― 1
Studying Java # 0
Studying Java ―― 8
Studying Java ②
Studying Java ―― 7
Studying Java ―― 2
Studying Java ①
Studying Java -10
Studying java9 (jShell)
Studying Java 8 (Collector / Collectors)
Studying Java 8 (see constructor)
Java
Studying Java ~ Part 8 ~ Cast
Studying Java 8 (lambda expression)
Java
Java learning (0)
[Java] array
Java protected
[Java] Annotation
[Java] Module
Java scratch scratch
Java tips, tips
Java methods
Java method
java (constructor)
Java array
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
Java serialization
java beginner 4
JAVA paid
Java (set)
java shellsort
[Java] compareTo
java reflexes
java (interface)
Java memorandum
Java array
[Java] Array
[Java] Polymorphism
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
[Java] Overload
Decompile Java
[Java] Annotation