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.
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.
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.
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