When you touch various languages, apart from the important features of the language, the detailed grammar and the usage of symbols gradually become confused. How do you write this in this language? It often happens. Sometimes I'm shocked that I used to use a language a lot, but I've forgotten it just a short distance away. So, I will make a memo like this.
Scala
if (x >= 30) {
...
} else if (x >= 0) {
...
} else {
...
}
//Expressions, not sentences
val max = if (a > b) a else b
--The conditional expression must be a logical value
--Similar to C language, {}
can be omitted if there is one expression in {}
(dangling else problem ) Yes)
--Expressions, not sentences
{...}
part in the above example, and the curly braces are part of the block expression syntax, not the if expression specification. If you put an expression other than the block expression, it will look the same as if the curly braces in C language were omitted.Conditional Expressions - Expressions | Scala 2.13
Java
if (x >= 30) {
...;
} else if (x >= 0) {
...;
} else {
...;
}
--The conditional expression must be a logical value
--Similar to C language, {}
can be omitted if there is only one sentence in {}
(dangling else problem ) Yes)
The if Statement - Java Language Specification
Rust
if x >= 30 {
...
} else if x >= 0 {
...
} else {
...
}
//Expressions, not sentences
let max = if a > b { a } else { b };
--The conditional expression must be a logical value
--Unlike C language, {}
cannot be omitted
--Expressions, not sentences
if Expressions - Control Flow - The Rust Programming Language
if (x >= 30) {
...;
} else if (x >= 0) {
...;
} else {
...;
}
// { }If there is one sentence in{ }Can be omitted
if (x >= 30)
...;
else if (x >= 0)
...;
else
...;
--Conditional expressions are properly interpreted as true or false depending on the type
--{}
can be omitted if there is only one statement in {}
(dangling else problem available)
if x >= 30 {
...
} else if x >= 0 {
...
} else {
...
}
//This is also possible
if x := f(); x >= 0 {
...
}
--The conditional expression must be a logical value
--Unlike C language, {}
cannot be omitted
If statements - The Go Programming Language Specification - The Go Programming Language
PHP
if ($x >= 30) {
...;
} else if ($x >= 0) {
...;
} elseif ($x >= 0) { //either else if or elseif is OK
...;
} else {
...;
}
//Another syntax
if ($x >= 30):
...;
elseif ($x >= 0): //else if is not possible
...;
else:
...;
endif;
--Conditional expressions are properly interpreted as true or false depending on the type
--In the first syntax, as in C language, {}
can be omitted if there is only one statement in {}
(dangling else problem ? q = dangling + else) Yes)
PHP: if - Manual PHP: Another Syntax for Control Structures-Manual
Perl
if ($x >= 30) {
...;
} elsif ($x >= 0) {
...;
} else {
...;
}
#Opposition
unless ($x >= 0) {
...;
}
#Statement modifier(Postfix if syntax)
... if $x >= 0;
... unless $x >= 0;
--Conditional expressions are properly interpreted as true or false depending on the type
--Unlike C language, {}
cannot be omitted
Complex sentence --perlsyn --Perl grammar --perldoc.jp Sentence modifiers --perlsyn --Perl grammar --perldoc.jp
Python
if x >= 30:
...
elif x >= 0:
...
else:
...
--Conditional expressions are properly interpreted as true or false depending on the type --Indent!
if Statements - More Control Flow Tools — Python 3.8.0 documentation
Ruby
if x >= 30
...
elsif x >= 0
...
else
...
end
#Opposition
unless x >= 0
...
end
#Expressions, not sentences
max = if a > b then a else b end
#if modifier(Postfix if syntax)
... if x >= 0;
... unless x >= 0;
--Conditional expressions are properly interpreted as true or false depending on the type
--Expressions, not sentences
--Write then
at the end of ʻif and ʻelsif
, but it can be omitted if line breaks continue.
Conditional branch --Control structure (Ruby 2.6.0)
Recommended Posts