if..else construction

C

C++

C#

Common Lisp

Dart

F#

Go

Java

JavaScript

Kotlin

Python

Rust

C
int n = 21;

if (n > 22) 
{
    printf("n > 22 \n");
} 
else if (n < 22) 
{
    printf("n < 22 \n");
} 
else 
{
    printf("n == 22 \n");
}

C++

#include <iostream>

int main()
{
    int n {21};
    if (n > 22)
    {
        std::cout << "n > 22" << std::endl;
    }
    else if (n < 22)
    {
        std::cout << "n < 22" << std::endl;
    }
    else
    {
        std::cout << "n == 22" << std::endl;
    }

    // if-else with variable initialization
    int a {5};
    int b {3};
    if(int c {a - b}; a > b)
    {
        std::cout << "a + b = " << c << std::endl;
    }
    else
    {
        std::cout << "a - b = " << c << std::endl;
    }
}

C#

C#

int n = 21;
     
if(n > 22)
{
    Console.WriteLine("n > 22");
}
else if (n < 22)
{
    Console.WriteLine("n < 22");
}
else
{
    Console.WriteLine("n = 22");
}

Common Lisp

Lisp

(let ((n 21))
    (if (> n 22)
        (format t "n > 22")
        (if (< n 22)
            (format t "n < 22")
            (format t "n = 22"))))

If the if block must have several expressions, which are formatted using the progn operator:

Lisp

(let ((n 21))
    (if (< n 22)
        (progn
            (format t "n < 22~%")
            (format t "n != 22~%"))))

To test the truth of a condition, you can also use the when macro, which is similar to if..then:

Lisp

(let ((n 21))
    (when (< n 22)
        (format t "n < 22~%")
        (format t "n != 22~%")))

The unless macro is equivalent to an else expression and tests the opposite condition:

Lisp

(let ((n 22))
    (when (< n 22)
        (format t "n < 22~%")
        (format t "n != 22~%"))
    (unless (< n 22)
        (format t "n >= 22~%")))

Dart

Dart

void main() {
    int n = 21;
    if(n > 22){
        print("n > 22");
    }
    else if(n < 22){
        print("n < 22");
    }
    else{
        print("n = 22");
    }
}

F#

F#

let n = 21
     
if n > 22 then
    printfn "n > 22"
elif n < 22 then
    printfn "n < 22"
else
    printfn "n = 22"

Getting the result from if..then

F#

let a = 21
let b = 22
let res = if(a > b) then 1 elif(a < b) then -1 else 0
printfn "%d" res

Go

Go

package main
import "fmt"
 
func main() {
    n := 21
     
    if n > 22 {
        fmt.Println("n > 22")
    } else if n < 22{
        fmt.Println("n < 22")
    } else{
        fmt.Println("n == 22")
    }
}

Java

Java

class Program {
    public static void main(String[] args) {  
        int n = 21;
     
        if(n > 22)
        {
            System.out.println("n > 22");
        }
        else if (n < 22)
        {
            System.out.println("n < 22");
        }
        else
        {
            System.out.println("n = 22");
        }
    }
}

JavaScript

JavaScript

let n = 21;
     
if(n > 22) {
    console.log("n > 22");
}
else if (n < 22){
    console.log("n < 22");
}
else{
    console.log("n == 22");
}

Kotlin

Kotlin

fun main() {
    val n = 21
    if(n > 22) {
        println("n > 22")
    }
    else if(n < 22){
        println("n < 22")
    }
    else{
        println("n = 22")
    }
}

Getting the result from if..else

Kotlin

fun main() {
    val a = 21
    val b = 22
    val res = if(a > b) 1 else if(a < b) -1 else 0
    println(res) // -1
}

Python

Python

n = 21
if n > 22:
    print("n > 22")
elif n < 22:
    print("n < 22")
else:
    print("n = 22")

Getting the result from if..else

Python

a = 21
b = 22
res = 1 if(a > b) else -1
print(res)

Rust

Rust

fn main(){
    let n = 21;
    if n > 22 {
        println!("n > 22")
    }
    else if n < 22 {
        println!("n < 22")
    }
    else{
        println!("n = 22")
    }
}

Getting the result from if..else

Rust       

fn main(){
    let a = 21;
    let b = 22;
    let res = if a > b {1} else {-1};
    println!("{}", res);
}



Next

Explore More IT Terms

Leave a Reply

Your email address will not be published. Required fields are marked *