How to resolve the algorithm Logical operations step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Logical operations step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

Write a function that takes two logical (boolean) values, and outputs the result of "and" and "or" on both arguments as well as "not" on the first arguments. If the programming language doesn't provide a separate type for logical values, use the type most commonly used for that purpose. If the language supports additional logical operations on booleans such as XOR, list them as well.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Logical operations step by step in the UNIX Shell programming language

Source code in the unix programming language

function boolVal {
    if (( ! $? )); then
        echo true
    else
        echo false
    fi
}
       
a=true
b=false
printf '%s and %s = %s\n' "$a" "$b" "$("$a" && "$b"; boolVal)"
printf '%s or %s = %s\n' "$a" "$b" "$("$a" || "$b"; boolVal)"
printf 'not %s = %s\n' "$a" "$(! "$a"; boolVal)"

a=1
b=0
printf '%d and %d = %d\n' "$a" "$b" "$(( a && b ))"
printf '%d or %d = %d\n' "$a" "$b" "$(( a || b ))"
printf 'not %d = %d\n' "$a" "$(( ! a ))"

  

You may also check:How to resolve the algorithm Pythagorean triples step by step in the Haskell programming language
You may also check:How to resolve the algorithm Permutations step by step in the Curry programming language
You may also check:How to resolve the algorithm Sort using a custom comparator step by step in the ooRexx programming language
You may also check:How to resolve the algorithm File input/output step by step in the zkl programming language
You may also check:How to resolve the algorithm Sum of squares step by step in the Golfscript programming language