-[Console output](#console output)
--[Variable Declaration](#Variable Declaration)
-[Conditional branch](# Conditional branch)
-[Repeat processing](# Iterative processing)
C#
//System is optional
System.Console.Write("Hello ");
Console.Write("World");
>>> Hello World
Console.WriteLine("Hello ");
Console.WriteLine("Workd");
>>> Hello
World
Java
System.out.print("Hello ");
System.out.print("World");
>>> Hello World
System.out.println("Hello ");
System.out.println("World");
>>> Hello
World
Python
print("Hello")
print("World")
>>> Hello
World
print("Hello ", end="")
print("World")
>>> Hello World
Ruby
print 'Hello '
print 'World'
>>> Hello World
puts 'Hello'
puts 'World'
>>> Hello
World
Kotlin
print("Hello ");
print("World");
>>> Hello World
println("Hello ");
println("Workd");
>>> Hello
World
C#
String stra = "a";
string strb = "b";
int num = 1;
Boolean tr = true;
bool fa = false;
Java
String str = "a";
int num = 1
boolean bool = true
Python
No type declaration required
str = "a" #String
num = 1 #int
tr = True #Boolean
Ruby
No type declaration required
Boolean type does not exist
str = "a" #String
num = 1 #int
Kotlin
val str = "a" //String
//Variables declared with val cannot be changed
var num = 1 //int
//Variables declared with var can be changed
num = 2
var tr = True //Boolean
C#
///if
if(Condition A){
Process A
}
else if(Condition B){
Process B
}
else{
//If the above conditions are not met
Process C
}
//switch
switch (variable){
case Condition A:
Process A
break;
case Condition B:
Process B
break;
default:
//If the above conditions are not met
Process C
break;
}
Java
//if
if(Condition A){
Process A
}
else if(Condition B){
Process B
}
else{
//If the above conditions are not met
Process C
}
//switch
switch (variable){
case Condition A:
Process A
break;
case Condition B:
Process B
break;
default:
//If the above conditions are not met
Process C
break;
}
Python
if condition A:
Process A
elif condition B:
Process B
else:
Process C
#if
if condition A then
Process A
elsif condition B then
Process B
else then
//If the above conditions are not met
Process C
end
#case
case variable
when condition A then
Process A
when condition B then
Process B
else
//If the above conditions are not met
Process C
end
Kotlin
//if
if(Condition A){
Process A
}
else if(Condition B){
Process B
}
else{
//If the above conditions are not met
Process C
}
//when
when (variable) {
Condition A-> {
Process A
}
Condition B-> {
Process B
}
else -> {
Process C
}
//when can be judged by directly entering an expression such as variable == condition.
when {
Conditional expression A-> {
Process A
}
Conditional expression B-> {
Process B
}
else -> {
Process C
}
}
C#
for (int i = 1; i <= 100; i++){
Console.WriteLine(i);
}
Java
for (int i = 1; i <= 100; i++){
System.out.println(i);
}
Python
for i in range(1, 101):
print(i)
Ruby
#3 types
puts [*1..100]
100.times{|n| println n+1}
1.upto(100){|n| p n}
Kotlin
for (i in 1..100) println(i)
C#
int[] array = new int[10]{1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
foreach(int n in array)
{
Console.WriteLine(n);
}
Java
int[] array = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512};
for (int n : array) {
System.out.println(n);
}
Python
array = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512];
for n in array:
print(n)
Ruby
array = Array[1, 2, 4, 8, 16, 32, 64, 128, 256, 512];
for n in array do
puts(n)
end
Kotlin
val arr = arrayOf(1, 2, 4, 8, 16, 32, 64, 128, 256, 512)
for (n in arr) {
println(n)
}
C#
int[] ary = {1,10,100};
Java
int[] ary = {1,10,100};
Python
#In Python, array cannot be used without importing
import array
ary = array.array('i',[1, 10, 100])
#array.array(Mold,[Value 1,Value 2,Value 3])
Ruby
array = [1,10,100]
//Ruby array can be changed
array.push(200)
array[2] = 50
p array
>> [1,10,50,200]
Kotlin
// [1, 2, 3]make
val list = arrayOf(1, 2, 3)
//Create a size 3 array filled with nulls
var arr: Array<Int?> = arrayOfNulls(3)
C#
//Declaration
var list = new List<string>();
//var object name= new List<Mold>(Element count);
//add to
list.Add("Tokyo");
list.Add("Osaka");
list.Add("Nagoya");
//Delete
list.Remove("Osaka");
//output
Console.WriteLine("[{0}]", string.Join(", ", list));
Console.WriteLine(list[1]);
//Output result
>>> [Tokyo, Nagoya]
Nagoya
Java
import java.util.ArrayList;
import java.util.List;
//Declaration
List<String> list = new ArrayList<String>();
//List<Types of things to put in the list>Object name=type of new List<Types of things to put in the list>(Element count)
//add to
list.add("Tokyo");
list.add("Osaka");
list.add("Nagoya");
//Delete
list.remove("Osaka");
//output
System.out.println(list);
System.out.println(list.get(1));
//Output result
>>> [Tokyo, Nagoya]
Nagoya
Python
list = ["Hello", 1, "world", 2, "!"]
#add to
list.append(5)
#Delete
list.remove(1)
#output
print(list)
print(list[3])
#Output result
>>> ['Hello','world', 2, '!',5]
!
Ruby
Ruby doesn't have List because Array plays the same role as List
Kotlin
// List<Int>make. The contents of List cannot be changed.
val list = listOf(1, 2, 3, 4, 5)
// MutableList<Int>make. You can change the contents here
val mlist = mutableListOf(1, 2, 3, 4, 5)
//add to
mlist.add(6)
//Delete
mlist.remove(3)
//output
println(mlist[0])
println(mlist)
>>> 1
[1, 2, 4, 5,6]
C#
Dictionary<int,String> dict = new Dictionary<int,String>();
//Dictionary<Key model name,Value type name>Object name= new Dictionary<Key model name,Value type name>()
//add to
dict.Add(1,"aaa");
dict.Add(2,"bbb");
dict.Add(3"ccc");
//Delete
dict.Remove(2)
//output
Console.WriteLine(dict[3])
>>> ccc
//Output all
foreach (KeyValuePair<int, string> item in dict){
Console.WriteLine("[{0}:{1}]", item.Key, item.Value);
}
>>> [1:aaa]
[2:bbb]
Java
import java.util.HashMap;
import java.util.Map;
//Declaration
Map<Integer,String> map = new HashMap<Integer,String>();
//Map<Key model name,Value type name>Object name=new Map type<Key model name,Value type name>()
//add to
map.put(1,"aaa");
map.put(2,"bbb");
map.put(3,"ccc");
//Delete
map.remove(2);
//output
System.out.println(map.get(3));
>> ccc
//Output all
for (int key : map.keySet()) {
System.out.println(key + ":" + map.get(key));
}
>>> 1:aaa
3:ccc
Python
#Declaration (initialization)
dict = {1:"aaa", 2:"bbb", 3:"ccc", "ddd":4}
#Object name= {key:value , key;value}
#add to
dict["peach"] = 5
#Delete
dict.pop(2)
#output
print(dict[3])
print(dict["ddd"])
>>> ccc
4
#Output all
print(dict)
>>> {1: 'aaa', 3: 'ccc', 'ddd': 4, 'peach': 5}
Ruby
#Use Hash object
hash = {1 =>"aaa", 2 =>"bbb", 3 => "ccc", "ddd" => 4}
#Object name= { key => value}
#add to
hash[5] = "eee"
#Delete
hash.delete[2]
#output
puts hash["ddd"]
>>>4
#Output all
puts hash.inspect
>>> {:1=>aaa, :3=>cccc :ddd=>4 :5=>eee}
Kotlin
//Map<Int,String>Can't be changed
val map = mapOf(1 to "aaa", 2 to "bbb")
//val object name= mapOf(key to value)
// MutableMap<Int, String>Can be made and changed
val mmap = mutableMapOf(1 to "aaa", 2 to "bbb")
mmap[1] = "ccc"
//add to
mmap[3] = "ddd"
//Delete
mmap.remove(2)
//output
println(list[1])
>>> ccc
//Output all
for((key, value) in map)
println("${key} to ${value}")
>>> 1 to ccc
3 to ddd
Recommended Posts