Summary of how to write if statements (Scala, Java, Rust, C language, C ++, Go language, PHP, Perl, Python, Ruby)

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

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

C language, C ++

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)

Go language

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

Summary of how to write if statements (Scala, Java, Rust, C language, C ++, Go language, PHP, Perl, Python, Ruby)
Summary of how to write increment / decrement (Scala, Java, Rust, C, C ++, Go, PHP, Perl, Python, Ruby, JavaScript)
Behavior of division operators between integers (C, C ++, Scala, Java, Rust, Go, PHP, JavaScript, Perl, Python, Ruby)
[Introduction to Python] How to write conditional branches using if statements
Difference in how to write if statement between ruby ​​and python
[Python] Summary of how to use pandas
[Python2.7] Summary of how to use unittest
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
How to write Ruby to_s in Python
Summary of how to write AWS Lambda
How to write the correct shebang in Perl, Python and Ruby scripts
Sorting AtCoder ARC 086 C hashes to solve in Ruby, Perl, Java and Python
Summary of how to import files in Python 3
Summary of how to use MNIST in Python
[Python] Summary of eval / exec functions + How to write character strings with line breaks
From the initial state of CentOS8 to running php python perl ruby with nginx
2014 Web Application Framework Trends (PHP / Java / Ruby / Python / Perl)
How to write a list / dictionary type of Python3
How to use Python Kivy ① ~ Basics of Kv Language ~
[Python] Summary of how to specify the color of the figure
python, php, ruby How to convert decimal numbers to n-ary numbers
Let's write Python, Ruby, PHP, Java, JavaScript side respectively
[Introduction to Python] How to write repetitive statements using for statements
How to handle JSON in Ruby, Python, JavaScript, PHP
Let's see how to count the number of elements in an array in some languages [Go, JavaScript, PHP, Python, Ruby, Swift]
Offline real-time how to write Python implementation example of E14
[python] Summary of how to retrieve lists and dictionary elements
[Python] How to write an if statement in one sentence.
[Linux] [C / C ++] Summary of how to get pid, ppid, tid
[Python] Summary of how to use split and join functions
Summary of how to write .proto files used in gRPC
Hello World in various languages [Python / PHP / Java / Perl / Ruby]
Java VS PHP VS Python VS Ruby
About Perl, Python, PHP, Ruby
Summary of Chapter 2 of Introduction to Design Patterns Learned in Java Language
Offline real-time how to write E11 ruby and python implementation example
Chapter 4 Summary of Introduction to Design Patterns Learned in Java Language
Summary of Chapter 3 of Introduction to Design Patterns Learned in Java Language
Offline real-time how to write Python implementation example of E15 problem
How to use Python Kivy (reference) -I translated Kivy Language of API reference-
Solving with Ruby, Perl, Java, and Python AtCoder ABC 065 C factorial
How to write a Python class
Multi-stage selection (Go / C # / Ruby / Python)
Summary of how to use pandas.DataFrame.loc
Summary of how to use pyenv-virtualenv
Introduction to Protobuf-c (C language ⇔ Python)
How to wrap C in Python
Summary of how to use csvkit
[Scraping Summary] | Python Node.js PHP Ruby Go VBA | Scraping Yahoo! Top in 6 languages
Solving with Ruby, Perl, Java, and Python AtCoder ARC 098 C Cumulative sum
Solving with Ruby, Perl, Java and Python AtCoder CADDi 2018 C Prime Factorization
Solve with Ruby, Perl, Java and Python AtCoder ABC 047 C Regular Expression
Summary of how to set up major Python Lint (pep8, pylint, flake8)