[PYTHON] [Programming learning] Logic comparison by language part.3 (conditional branching)

Touch of conditional branch.

3. Conditional branch

Ruby

sample.rb


val = 1
flg = true

#int type and string type
if val == 1
    puts "val = 1"
elsif val == 2
    puts "val = 2"
else 
    puts "val != (1,2)"
end

#boolean type
if flg
    puts "flg = true"
else
    puts "flg = false"
end

#For int type and string type, only the variable name is described.
# 0/Non-zero, null/Not null can be determined
if val
   puts "val != 0"
else 
   puts "val = 0"
end
$ ruby sample.rb
val = 1
flg = true
val != 0

Python3

sample.py


val = 1
flg = True

#int type and string type
if val == 1:
    print("val = 1")
elif val == 2:
    print("val = 2")
else:
    print("val != (1,2)")

#boolean type
if flg:
    print("flg = true")
else:
    print("flg = false")

#For int type and string type, only the variable name is described.
# 0/Non-zero, null/Not null can be determined
if val:
   print("val != 0")
else:
   print("val = 0")
$ python3 sample.py
val = 1
flg = true
val != 0

Swift

sample.swift


import Foundation
let val = 1
let flg = true

//int type and string type
if (val == 1) {
    print("val = 1")
} else if (val == 2) {
    print("val = 2")
} else {
    print("val != (1,2)")
}

//boolean type
if (flg) {
    print("flg = true")
} else {
    print("flg = false")
}

//In swift, whether int or String contains a value is
//You need to write the conditional expression properly.
if (val > 0) {
    print("val != 0")
} else {
    print("val = 0")
}
(playground)
val = 1
flg = true
val != 0

Java

sample.java


package sample;

public class Main {
    public static void main(String[] args) {
    	int val = 1;
    	boolean flg = true;

    	//int type and string type
    	if (val == 1) {
    	    System.out.println("val = 1");
    	} else if (val == 2) {
    	    System.out.println("val = 2");
    	} else {
    	    System.out.println("val != (1,2)");
    	}

    	//boolean type
    	if (flg) {
    	    System.out.println("flg = true");
    	} else {
    	    System.out.println("flg = false");
    	}

    	//Whether int or String contains a value
    	//You need to write the conditional expression properly.
    	if (val > 0) {
    	    System.out.println("val != 0");
    	} else {
    	    System.out.println("val = 0");
    	}
    }
}
(eclipse console)
val = 1
flg = true
val != 0

C#

sample.cs


using System;
public class HelloWorld {
    static public void Main () {
    	int val = 1;
    	bool flg = true;

    	//int type and string type
    	if (val == 1) {
    	    Console.WriteLine("val = 1");
    	} else if (val == 2) {
    	    Console.WriteLine("val = 2");
    	} else {
    	    Console.WriteLine("val != (1,2)");
    	}

    	//boolean type
    	if (flg) {
    	    Console.WriteLine("flg = true");
    	} else {
    	    Console.WriteLine("flg = false");
    	}

    	//Whether int or String contains a value
    	//You need to write the conditional expression properly.
    	if (val > 0) {
    	    Console.WriteLine("val != 0");
    	} else {
    	    Console.WriteLine("val = 0");
    	}
    }
}

(Execute using'mono'in the execution environment at hand)

$ mcs sample.cs
(Compile successful)
$ mono sample.exe
val = 1
flg = true
val != 0

Recommended Posts

[Programming learning] Logic comparison by language part.3 (conditional branching)
[Programming learning] Logic comparison by language part.1 ("Hello World")
[Programming learning] Logic comparison by language part.2 (variables: numbers, character strings)
[2019 latest version] Programming language comparison
Making sound by programming Part 2
Stack processing speed comparison by language
Conditional branching of Python learned by chemoinformatics
Learn math and English by programming (Part 2)
Video frame interpolation by deep learning Part1 [Python]
Classification of guitar images by machine learning Part 2